python - Telegram bot api如何安排通知?
问题描述
我制作了一个机器人,可以获取今天的足球比赛,如果用户想要,他可以在所选比赛前 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)
显然,当循环运行时,我不能使用另一个命令。我是编程新手,我怎么能实现这个,所以我仍然收到通知,但是在运行时我可以使用其他命令?
谢谢!
解决方案
您可以安排工作。
假设您有一个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")
推荐阅读
- excel - Excel 注释不会停留在单元格附近
- javascript - 这个按位运算的技术名称是什么?
- c# - 在一次上下文调用中加载多个导航属性
- dart - Flutter - 到目前为止还不可能在 Firestore 中使用 Algolia 实现搜索功能吗?
- android-studio - iPhone XS 在 Android Studio for Flutter App 中无法正确识别
- java - InputMismatchException 被捕获后程序崩溃
- javascript - React Native - 增加圆圈大小然后减小
- c# - C# MVC.Net 控制器简化
- akamai - 默认情况下缓存在 Akamai 的响应标头?
- neo4j - 使用密码显示具有超过 1 个关系的节点的所有传出关系