skip to Main Content

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.

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!

2

Answers


  1. Try to use an aiogram and you can make scheduled tasks with aiocron (store users who wants to get notification in database or in global dict)

    Login or Signup to reply.
  2. 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

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search