Telegram bot-如何每天发送消息 [英] Telegram bot- how to send messages Daily

查看:81
本文介绍了Telegram bot-如何每天发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个每天在特定时间发送消息的电报机器人.但这对我不起作用.我认为问题出在时间参数上.我使用了此类的另一种方法,它们运行良好,但 run_daily 不起作用.:(

I am trying to develop a telegram-bot that send a message every day at a specific time. but it's not working for me. I think the problem is in the time parameter. I used another method of this class and they were working well but run_daily is not working. :(

import telegram.ext
from telegram.ext import Updater
from datetime import time

updater = Updater('My Token', use_context=True)
job = updater.job_queue

def callback_minute(context: telegram.ext.CallbackContext):
    context.bot.send_message(chat_id='My Chat ID', text='One message every minute')

# job.run_repeating(callback_minute, interval=5, first=0)
job.run_daily(callback_minute,time = time(hour = 20, minute = 2, second = 00),days=(0, 1, 2, 3, 4, 5, 6))

updater.start_polling()
updater.idle()

推荐答案

您错误地使用了 datetime 对象..

you're using the datetime object wrongly..

首先,请注意您正在创建的 datetime 对象将考虑 UTC 时间和日期,除非您对其进行修改.

first of all, note that the datetime object you're creating will consider the UTC time and date unless you modify it..

至于你的问题,修改你的代码看起来像这样,当你有很多时间处理时它对你来说更清晰,它应该解决主要问题:

as for your problem, modify your code to look like this, it's cleaner to you when you'll have a lot of times to deal with and it should solve the main problem:

import datetime
t = datetime.time(20, 2, 00, 000000)
job.run_daily(callback_minute,t,days=(0, 1, 2, 3, 4, 5, 6),context=None,name=None)

这篇关于Telegram bot-如何每天发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆