简体   繁体   中英

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

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM