queue for async function python telegram.ext - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: queue for async function python telegram.ext (/thread-40156.html) |
queue for async function python telegram.ext - noctious - Jun-11-2023 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() |