Home > Mobile >  Python script to run a function at a specific time every day does not work when deployed on Heroku
Python script to run a function at a specific time every day does not work when deployed on Heroku

Time:07-22

I am trying to create a telegram bot using python that will run a function every day at a specific time.

from operator import ge
import schedule
import telebot
import time
from threading import Thread
from time import sleep




bot = telebot.TeleBot("bot token")
jay = chat_id

def test_message():
    bot.send_message(jay, "This is test")


def schedule_checker():
    while True:
        schedule.run_pending()
        time.sleep(1)



if __name__ == "__main__":
    # Create the job in schedule.
    schedule.every().day.at("03:58").do(test_message)

    Thread(target=schedule_checker).start() 

It works when I run it via my terminal on VScode, but when I deploy the bot on Heroku, it doesn't work. I am pretty sure I deployed it correctly. Would appreciate any advice, thank you in advance and apologise if this is a dumb question (I am just starting my coding journey).

CodePudding user response:

I realised the bot deployed on Heroku was still sending messages, but it posted at the wrong timings. I realised that it was using UTC time. I edited the code to change it to UTC time and it now posts at the correct timings.

  • Related