This posting is more simple and general way for getting alarm via discord bot.
This posting is just added discord related code from previous article.
The Discord is provide to WebHook service that is simply bot service.
If using WebHook URL, we can send to message on channel or your server.
Let's go enjoy your diablo world.
1. Create a channel or server on discord.
2. Edit Channel (for example : General)
3. Go to Apps and "create a webhook" click.
4. Edit Bot Name and Click the "Copy webhook URL"
5. We can send to message in channel via just using this URL. This is sample python code :
import requests
WEBHOOK_URL = 'Input your webhook url'
def send_message(message):
requests.post(WEBHOOK_URL, data=message)
message = {'content':'Hello! Discord!!'}
send_message(message)
6. Add to my World Boss track and Notificator code :
#!/opt/homebrew/bin/python3.10
#
# Diablo IV World Boss Notificator with Discord ( for Diablo4.life )
# made by Mirr
# License: GNU Free
############################################################################
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from datetime import datetime, timedelta
import subprocess, time, logging, requests
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
file_handler = logging.FileHandler('Diablo_alert.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
def send_discord(contents):
WEBHOOK_URL = 'Your WebHook URL'
message = {'content': contents}
requests.post(WEBHOOK_URL, data=message)
def schedule_with_at(schedule_time):
command = "python Diablo4_alert.py" # Replace with your desired command
Attime = schedule_time.strftime("%H:%M %m%d%y") # Replace with your desired time in HH:MM mmddyy format
Attime2 = schedule_time.strftime("%a %b %d, %H:%M").split(',')
# Check duplication schedules
Check = subprocess.run(f'atq | grep "{Attime2[0].replace("0", " ")}{Attime2[1]}" | cut -f 2', shell=True, capture_output=True, text=True).stdout
try:
if Check != "":
logger.info(f"Already scheduled {Check}")
else:
result = subprocess.run(f'echo "{command}" | at {Attime}', shell=True, capture_output=True, text=True, check=True)
logger.info(result.stderr)
except subprocess.CalledProcessError as e:
logger.error(f"Error executing 'at' command: {e}")
return False
# Set up the web driver (make sure to specify the path to the driver)
options = Options()
options.add_argument('-headless') # Run Firefox in headless mode (without a visible browser window)
executable_path = './geckodriver'
try:
# Load the webpage
driver = webdriver.Firefox(options=options)
url = 'https://diablo4.life/trackers/overview'
driver.get(url)
# Wait for the page to load and any dynamic content to be generated
# You may need to adjust the sleep duration based on the page's loading time
time.sleep(3) # Wait for 3 seconds (adjust as needed)
except Exception as e:
logger.error(f"Driver and Page Load Failed: {e}")
try:
# Extract the desired content
Boss = driver.find_element('class name', 'EventCountdown_title__MMm_B').text
TimeElement = driver.find_element('class name', 'EventCountdown_countdown__sl85X').text.splitlines()
except Exception as e:
logger.error(f"Element find failed: {e}")
# Quit the browser
driver.quit()
LeftTime = ''.join(TimeElement)
Hours = float(TimeElement[0])
Mins = float(TimeElement[2])
Secs = float(TimeElement[4])
# Get the text from the element
CurrentTime = datetime.now()
GenTime = timedelta(hours=Hours, minutes=Mins, seconds=Secs)
RealGTime = CurrentTime + GenTime
RunTime1 = RealGTime - timedelta(minutes=10)
RunTime2 = RealGTime + timedelta(minutes=10)
try:
#Schedule before 10 Mins
schedule_with_at(RunTime1)
# Re-check new gen boss and time after 10 mins
schedule_with_at(RunTime2)
logger.info(f'Added jobs')
# Define the message content that redirect to dummy from printed result
content = f"{Boss} - left {LeftTime} Gen at " + RealGTime.strftime("%H:%M:%S")
send_discord(content)
except Exception as e:
logger.error(f"Error: {e}")
7. Execution python.
# python Diablo4_alert.py
This point is no need any registering development group for using discord API and no need any token/authorization.
Using Webhook URL service, we can more simply get alarm for World Boss Re-gen :)
'Skills > Other Lang' 카테고리의 다른 글
Diablo4 월드보스 뜨는 시간을 확인하고 imessage 로 알람받자 (0) | 2023.06.28 |
---|---|
리눅스용 editplus 식 웹에디터... (1) | 2007.10.05 |
[PHP] @의 역할... (0) | 2005.11.29 |
[Perl]Perl 관련 사이트 모음 (0) | 2005.10.30 |
루비사용자모임 (0) | 2005.09.04 |