[英]Telegram bot api how to schedule a notification?
I've made a bot that gets today football matches and if the user wants he can get a reminder 10 min before a selected match.我制作了一个机器人,可以获取今天的足球比赛,如果用户想要,他可以在所选比赛前 10 分钟收到提醒。
while current_time != new_hour:
now = datetime.now()
current_time = now.strftime("%H:%M")
#return notification
text_caps = "Your match starts in 10 minutes"
context.bot.send_message(chat_id=update.effective_chat.id, text=text_caps)
Obviously while the loop runs i can not use another command.显然,当循环运行时,我不能使用另一个命令。 I am new to programming how could i implement this so i still get the notification but while that runs i can use other commands?我是编程新手,我怎么能实现这个,所以我仍然收到通知,但是在运行时我可以使用其他命令?
Thank you!谢谢!
You can schedule a job.您可以安排工作。
Let's say you have a CommandHandler("watch_match", watch_match)
that listens for a /watch_match
conmmand and 10 minutes later a message is supposed to arrive假设您有一个CommandHandler("watch_match", watch_match)
来侦听/watch_match
命令,并且 10 分钟后应该会收到一条消息
def watch_match(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
ten_minutes = 60 * 10 # 10 minutes in seconds
context.job_queue.run_once(callback=send_match_info, when=ten_minutes, context=chat_id)
# Whatever you pass here as context is available in the job.context variable of the callback
def send_match_info(context: CallbackContext):
chat_id = context.job.context
context.bot.send_message(chat_id=chat_id, text="Yay")
A more detailed example in the official repository官方存储库中的更详细示例
And in the official documentation you can see the run_once
function在官方文档中你可以看到run_once
function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.