Search code examples
telegramtelegram-bottelethon

Telegram Bot presses Inline button too long


I made a bot that should click on inline buttons. My problem is that one click on the buttons takes a whole 15 seconds. How can this process be accelerated?

from telethon.sync import TelegramClient
from telethon import functions, types
from telethon import TelegramClient, sync
import time

AllLogs = open('logs.txt')

for logs in AllLogs:
    api_id, api_hash = logs.split(';') 
    client = TelegramClient(f'session_{api_id}', api_id, api_hash)
    client.start()
    messages = client.get_messages(int(ChannelId), ids=int(MessageId))
    start_time1 = time.time()
    messages.click(0)
    print("--- %s seconds ---" % (time.time() - start_time1))
    client.disconnect()


Solution

  • You cannot accelerate this process. The click method sends a request to Telegram, which then sends an update to the bot. The bot must answer the update to Telegram, which will then "forward" the result back to your original click.

    In essence, click must wait for the bot to answer the query, and if it does not answer to it fast enough, it will be slow. The bot may need to be fixed to answer faster (or at all; if it doesn't answer, Telegram times out and click returns None).

    What you can do is spawn a new asyncio task to run the click "in the background", however, it's not good practice, as one should always await all spawned tasks, and if the code depends on the bot's response, you will have to wait for it one way or another. I don't recommend this approach, but it technically "works".

    import asyncio
    ...
    asyncio.create_task(messages.click(0))