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
  I am sending messages using Python to Telegram, but I need to use WhatsApp for it Hendrikb 0 1,082 Apr-29-2025, 09:38 AM
Last Post: Hendrikb
  Python-based telegram spam bot-telegram-bot archer24 1 1,836 Feb-07-2025, 04:16 PM
Last Post: archer24
Shocked async was never was never awaited but the await is still in the code Nietzsche 3 2,003 Jun-04-2024, 02:28 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 3,074 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Problem with importing python-telegram library into the project gandonio 1 6,576 Nov-01-2022, 02:19 AM
Last Post: deanhystad
  Telegram bot python help! wolfdevs 0 1,543 Sep-07-2022, 11:34 AM
Last Post: wolfdevs
  Python multiprocessing Pool apply async wait for process to complete sunny9495 6 13,606 Apr-02-2022, 06:31 AM
Last Post: sunny9495
  get data from 2 async functions korenron 0 2,025 Sep-22-2021, 08:39 AM
Last Post: korenron
  How to get response data from telegram API call in python KanseiDorifto 0 4,895 Oct-22-2020, 12:23 PM
Last Post: KanseiDorifto
  Async requests lukee 0 2,223 Oct-06-2020, 04:40 AM
Last Post: lukee

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.