Python Forum
queue for async function python telegram.ext
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
queue for async function python telegram.ext
#1
Hi, I have a telegram bot code in which you enter a number, and it counts down from that number to zero.

I need to create a queue to execute the count_to_zero function, so that if another number is recorded during the operation of the function, the bot will send a message that the number has been added to the queue, and the number is in the list, after which it counted the old one, then waited 5 seconds and started counting the new one.
I tried to write using manuals and examples, but I failed because of the asynchrony of the function


import time
from telegram import ForceReply, Update
from telegram.constants import ParseMode
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    await update.message.reply_html("Send me a number and I'll count from it to zero")

async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    msg = update.message.text
    if msg.isdigit():
        msg = int(msg)
        if msg > 0:
            await count_to_zero(msg, update)

async def count_to_zero(num, update):
    for i in range(num, -1, -1):
            await update.message.reply_text(i)
            time.sleep(2)

def main() -> None:
    # Create the Application and pass it your bot's token.
    application = Application.builder().token(TOKEN).build()

    # on different commands - answer in Telegram
    application.add_handler(CommandHandler("start", start))

    # on non command i.e message - echo the message on Telegram
    application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))

    # Run the bot until the user presses Ctrl-C
    application.run_polling()

if __name__ == "__main__":
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,104 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Problem with importing python-telegram library into the project gandonio 1 1,581 Nov-01-2022, 02:19 AM
Last Post: deanhystad
  Telegram bot python help! wolfdevs 0 779 Sep-07-2022, 11:34 AM
Last Post: wolfdevs
  Python multiprocessing Pool apply async wait for process to complete sunny9495 6 6,464 Apr-02-2022, 06:31 AM
Last Post: sunny9495
  get data from 2 async functions korenron 0 1,230 Sep-22-2021, 08:39 AM
Last Post: korenron
  How to get response data from telegram API call in python KanseiDorifto 0 2,617 Oct-22-2020, 12:23 PM
Last Post: KanseiDorifto
  Async requests lukee 0 1,508 Oct-06-2020, 04:40 AM
Last Post: lukee
  task queue Valon1981 8 3,612 Jul-07-2020, 07:41 AM
Last Post: freeman
  Bot coding telegram.ext Kumarkv 1 1,883 May-29-2020, 01:00 AM
Last Post: Kumarkv
  Queue in Pygame constantin01 1 3,696 Jan-07-2020, 04:02 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020