I have a file in my root that I am running on a daily task with the simple command (/home/username/filename.py
). The task runs with exit code 0 and I see both my prints from the file in the task logs. But the discord post does not happen. Everything works as expected though when I run the file locally and from the bash console on python anywhere. Does anyone know why this wouldn't work from the task? Do I maybe need to add some sort of wait/timeout to keep the task running long enough for the API call to execute? Feels hacky but I'm at a loss. Here is the code:
from discordwebhook import Discord
from datetime import datetime, date
import requests
import json
def tenor_search(search_term):
api_key = "{my tenor api key}"
lmt = 1
ckey = "bandonCowntdown"
r = requests.get(
"https://tenor.googleapis.com/v2/search?q=%s&key=%s&client_key=%s&limit=%s" % (search_term, api_key, ckey, lmt))
if r.status_code == 200:
rule_gifs = json.loads(r.content)
return rule_gifs["results"][0]["media_formats"]["gif"]["url"]
else:
return None
def post_to_discord():
try:
discord = Discord(url="https://discord.com/api/webhooks/{my hook key})
heaven = date(2025, 11, 2)
delta = heaven - datetime.now().date()
search_term = f"rule{delta.days}"
gif_url = tenor_search(search_term)
print("bingo", gif_url)
discord.post(
embeds=[
{
"title":f"{delta.days} days.",
"image": {"url": f"{gif_url}"}
}
],
username="Dan_bot",
avatar_url="https://imgur.com/1M0Za0l.png"
)
print("all done")
return
except Exception as e:
print(f'{e}')
discord.post(
embeds=[
{
"title":f"{delta.days} days.",
"description": f"Dan bot is broken. Please help Dan bot!"
}
],
username="Dan_bot",
avatar_url="https://imgur.com/1M0Za0l.png"
)
return
post_to_discord()