【问题标题】:Telegram bot api how to schedule a notification?Telegram bot api如何安排通知?
【发布时间】:2021-03-15 15:06:42
【问题描述】:

我制作了一个机器人,可以获取今天的足球比赛,如果用户想要,他可以在所选比赛前 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)

显然,当循环运行时,我不能使用另一个命令。我是编程新手,我该如何实现它,所以我仍然会收到通知,但是在运行时我可以使用其他命令?

谢谢!

【问题讨论】:

    标签: python telegram telegram-bot python-telegram-bot


    【解决方案1】:

    尝试使用aiogram,您可以使用aiocron 进行计划任务(将想要在数据库或全局字典中获取通知的用户存储)

    【讨论】:

      【解决方案2】:

      您可以安排工作。
      假设您有一个 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")
      

      官方仓库中有更详细的example
      而在官方文档中可以看到run_oncefunction

      【讨论】:

        猜你喜欢
        • 2019-12-14
        • 2018-04-07
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 2021-11-26
        • 2016-05-09
        • 2017-02-17
        相关资源
        最近更新 更多