Search code examples
telegramtelegram-bottelethonpyrogram

Slow mode wait time - how to get it?


I have the following problem.

I have some code which uses pyrogram to periodically send a message into a few telegram supergroups that I moderate. Some of these groups have slow mode enabled. Some with 15 minutes wait, others with 60 minutes.

In the telegram app, when I send a message into a supergroup which has the slow mode enabled, it shows a little timer counting down the time until it is allowed to send the next message.
Within pyrogram, one can check by evaluating slowmode_enabled if a supergroup generally uses the slow mode. But is there some possibility to get the time one has to wait until the next message, just as in the telegram app?

I observed that when my code violates the wait time enforced by slow mode, it yields

Telegram says: [420 SLOWMODE_WAIT_X] (caused by "messages.ForwardMessages")
Pyrogram 2.3.43 thinks: A wait of 2605 seconds is required to send messages in this chat

Pls help I really don't know how to approach this :(


Solution

  • You can handle this in two different ways.

    1. Get to know the slow mode time from the ChatFull
    2. Or handle the SlowmodeWait error with a try-except

    Solution 1

    
    # Set your chat_id and resolve the chat
    chat_id = -100123456
    peer = await app.resolve_peer(chat_id)
    
    # Get the full chat info
    chat = await app.invoke(
        raw.functions.channels.get_full_channel.GetFullChannel(channel=peer)
    )
    
    # Access the slow mode value
    if chat.full_chat.slowmode_enabled:
        print(chat.full_chat.slowmode_seconds)
    

    The chat.full_chat.slowmode_seconds will give you the slow mode wait time in seconds. Handle your case accordingly.


    Solution 2

    This will probably be a straight forward solution. Just try to send the message. When the SlowmodeWait error occurs you'll retrieve the wait time value from the exception and you'll just retry after the delay.

    try:
        # Your existing send method calls here...
        await app.send_message(chat_id=chat_id, text="Hello World")
    
    except errors.exceptions.flood_420.SlowmodeWait as e:
        print(e.value) # this will be the number of seconds you need to wait
    

    Hope this helps :)