简体   繁体   English

在 Heroku 上部署时,每天在特定时间运行函数的 Python 脚本不起作用

[英]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.我正在尝试使用 python 创建一个电报机器人,它将每天在特定时间运行一个函数。

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.当我通过终端在 VScode 上运行它时,它可以工作,但是当我在 Heroku 上部署机器人时,它不起作用。 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.我意识到部署在 Heroku 上的机器人仍在发送消息,但它在错误的时间发布。 I realised that it was using UTC time.我意识到它使用的是UTC时间。 I edited the code to change it to UTC time and it now posts at the correct timings.我编辑了代码以将其更改为 UTC 时间,现在它在正确的时间发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何自动化脚本在特定时间每天运行? - Python: How to automate script to run every day at specific time? 如何每天在特定时间运行python函数 - How to run a python function every day at specific time 如何在python每天特定时间运行一个function? - How to run a function every day at a specific time in python? 如何安排python脚本在特定时间每天运行? (Windows计划除外) - How can I Schedule python script to run every day at specific time ? (except Windows schedule) 如何使用 python 让 Discord 机器人每天在特定时间运行 function? - How to make Discord bot run a function every day at a specific time using python? 如何安排生产 python 脚本以触发 function 在每天的指定时间运行 - How to schedule a production python script to trigger a function to run at a specified time every Day Discord.py Bot 每天特定时间运行功能 - Discord.py Bot run function at specific time every day Python脚本每月在特定时间和日期发送邮件 - Python script to send mail at specific time and day every month time.sleep 是每天运行 python 脚本的好方法吗? - Is time.sleep a good way to run a python script every day? 在特定的一天每小时运行一个脚本 - Run a script every hour on a specific day
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM