better messages
All checks were successful
Build and Deploy Docker Container / build-and-deploy (push) Successful in 50s
All checks were successful
Build and Deploy Docker Container / build-and-deploy (push) Successful in 50s
This commit is contained in:
68
main.py
68
main.py
@@ -1,6 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
import random
|
||||||
import requests
|
import requests
|
||||||
import schedule
|
import schedule
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@@ -16,10 +17,11 @@ load_dotenv()
|
|||||||
|
|
||||||
WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK_URL')
|
WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK_URL')
|
||||||
|
|
||||||
MSG_FOOTER = "THC - Toke Hash Coordinated"
|
MSG_FOOTER = "THC - Toke Hash Coordinated | GIF via Tenor"
|
||||||
|
|
||||||
MSG_TEST = "Discord 420 timer activated. This is a test notification."
|
MSG_TEST = "Discord 420 timer activated. This is a test notification."
|
||||||
MSG_REMINDER = "5 minute reminder!"
|
MSG_REMINDER = "This is your 5 minute reminder to 420!"
|
||||||
|
MSG_HALFTIME_REMINDER = "Half-time in 5 minutes!"
|
||||||
MSG_HALFTIME = "Half-time!"
|
MSG_HALFTIME = "Half-time!"
|
||||||
MSG_NOTIFICATION = "420! Blaze it!"
|
MSG_NOTIFICATION = "420! Blaze it!"
|
||||||
|
|
||||||
@@ -30,21 +32,70 @@ COL_UNKNOWN = 0x95a5a6
|
|||||||
|
|
||||||
messages = {
|
messages = {
|
||||||
"test": {"text": MSG_TEST, "color": COL_BLUE},
|
"test": {"text": MSG_TEST, "color": COL_BLUE},
|
||||||
"reminder": {"text": MSG_REMINDER, "color": COL_ORANGE},
|
"reminder_halftime": {"text": MSG_HALFTIME_REMINDER, "color": COL_ORANGE},
|
||||||
"halftime": {"text": MSG_HALFTIME, "color": COL_GREEN},
|
"halftime": {"text": MSG_HALFTIME, "color": COL_GREEN},
|
||||||
|
"reminder": {"text": MSG_REMINDER, "color": COL_ORANGE},
|
||||||
"notification": {"text": MSG_NOTIFICATION, "color": COL_GREEN},
|
"notification": {"text": MSG_NOTIFICATION, "color": COL_GREEN},
|
||||||
"unknown": {"text": "Unknown notification type", "color": COL_UNKNOWN}
|
"unknown": {"text": "Unknown notification type", "color": COL_UNKNOWN}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_tenor_img(search_term: str = "420") -> str:
|
||||||
|
results = []
|
||||||
|
try:
|
||||||
|
results = search_tenor(search_term=search_term)
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Error fetching Tenor GIF: {e}")
|
||||||
|
if results:
|
||||||
|
# get a random image from the results
|
||||||
|
r = random.choice(results)
|
||||||
|
formats = r["media_formats"]
|
||||||
|
# try to get the tinygif format
|
||||||
|
tinygif = formats.get("tinygif", {}).get("url")
|
||||||
|
if tinygif:
|
||||||
|
return tinygif
|
||||||
|
|
||||||
|
|
||||||
|
def search_tenor(search_term: str = "420") -> list:
|
||||||
|
"""
|
||||||
|
Fetch a random GIF URL from Tenor based on the provided slug.
|
||||||
|
"""
|
||||||
|
api_key = os.getenv('TENOR_API_KEY')
|
||||||
|
if not api_key:
|
||||||
|
logging.warning("TENOR_API_KEY not set")
|
||||||
|
return []
|
||||||
|
|
||||||
|
lmt = 8
|
||||||
|
ckey = "thc-time"
|
||||||
|
|
||||||
|
url = f"https://tenor.googleapis.com/v2/search?q={search_term}&key={api_key}&client_key={ckey}&limit={lmt}"
|
||||||
|
|
||||||
|
# get the top 8 GIFs for the search term
|
||||||
|
try:
|
||||||
|
response = requests.get(url, timeout=10)
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
if data["results"]:
|
||||||
|
return data["results"]
|
||||||
|
except requests.RequestException as e:
|
||||||
|
logging.error(f"Error fetching Tenor GIF: {e}")
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
def get_message(type: str) -> dict[str, int]:
|
def get_message(type: str) -> dict[str, int]:
|
||||||
"""
|
"""
|
||||||
Get the notification message based on the type.
|
Get the notification message based on the type.
|
||||||
"""
|
"""
|
||||||
|
msg = messages["unknown"]
|
||||||
if type in messages:
|
if type in messages:
|
||||||
return messages[type]
|
msg = messages[type]
|
||||||
else:
|
if type == "notification":
|
||||||
return messages["unknown"]
|
img = get_tenor_img()
|
||||||
|
else:
|
||||||
|
img = get_tenor_img(search_term=type)
|
||||||
|
if img:
|
||||||
|
msg["image"] = {"url": img}
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
def create_embed(message: str) -> dict:
|
def create_embed(message: str) -> dict:
|
||||||
@@ -53,8 +104,9 @@ def create_embed(message: str) -> dict:
|
|||||||
"""
|
"""
|
||||||
msg = get_message(message)
|
msg = get_message(message)
|
||||||
embed = {
|
embed = {
|
||||||
"title": message.capitalize(),
|
"title": message.replace("_", " ").capitalize(),
|
||||||
"description": msg["text"],
|
"description": msg["text"],
|
||||||
|
"image": msg.get("image"),
|
||||||
"color": msg["color"],
|
"color": msg["color"],
|
||||||
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
||||||
"footer": {"text": MSG_FOOTER}
|
"footer": {"text": MSG_FOOTER}
|
||||||
@@ -91,7 +143,7 @@ def main() -> None:
|
|||||||
# Schedule notifications
|
# Schedule notifications
|
||||||
schedule.every().hour.at(":15").do(send_notification, "reminder")
|
schedule.every().hour.at(":15").do(send_notification, "reminder")
|
||||||
schedule.every().hour.at(":20").do(send_notification, "notification")
|
schedule.every().hour.at(":20").do(send_notification, "notification")
|
||||||
schedule.every().hour.at(":45").do(send_notification, "reminder")
|
schedule.every().hour.at(":45").do(send_notification, "reminder_halftime")
|
||||||
schedule.every().hour.at(":50").do(send_notification, "halftime")
|
schedule.every().hour.at(":50").do(send_notification, "halftime")
|
||||||
logging.info("Scheduler started.")
|
logging.info("Scheduler started.")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user