Agum Yudhistira
Agum Yudhistira

Reputation: 31

How do you make the python bot click on the button in the telegram bot

I want to make a bot click for Telegram. The problem is I'm confused about how I clicked it using my code, I tried it but it still failed and the bot didn't click, what data should be posted so that the bot clicks on the telegram bot. I need your help. This is my source code :

from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import GetHistoryRequest, GetBotCallbackAnswerRequest

api_id = 974119
api_hash = 'a483ea002564cdaa0499a08126abe4a3'
client = TelegramClient('session_name', api_id, api_hash)
client.start()
channel_username = 'GOOGLE'
channel_entity = client.get_entity(channel_username)
posts = client(GetHistoryRequest(
    peer=channel_entity,
    limit=1,
    offset_date=None,
    offset_id=0,
    max_id=0,
    min_id=0,
    add_offset=0,
    hash=0))
messageId = posts.messages[0].id

client(GetBotCallbackAnswerRequest(
    channel_username,
    messageId,
    data=posts.messages[0].reply_markup.rows[0].buttons[0]))

client.disconnect()

The button that must be clicked is KeyboardButtonUrl or 🔎 Go to website :

reply_markup=ReplyInlineMarkup(
        rows=[
            KeyboardButtonRow(
                buttons=[
                    KeyboardButtonUrl(
                        text='🔎 Go to website',
                        url='https://www.google.com'
                    ),
                ]
            ),
            KeyboardButtonRow(
                buttons=[
                    KeyboardButtonCallback(
                        text='🛑 Report',
                        data=b'{"name":"ReportClick","id":"120326622"}'
                    ),
                    KeyboardButtonCallback(
                        text='⏩ Skip',
                        data=b'{"name":"SkipClick","id":"120326622"}'
                    ),
                ]
            ),
        ]
    ),

Upvotes: 3

Views: 18365

Answers (2)

Furkan Kamiloğlu
Furkan Kamiloğlu

Reputation: 1

sender = await event.get_sender()
messages = await client.get_messages(sender.username)
await messages[0].click(0)

This will click the first button in the message. You could also click(row, column), using some text such as click(text='👍') or even the data directly click(data=b'payload').

Upvotes: 0

Lonami
Lonami

Reputation: 7086

You should not use client.get_entity() in this case, it's not necessary.

You should use client.get_messages(), not GetHistoryRequest.

You should use message.click(), not GetBotCallbackAnswerRequest.

So your code would be:

from telethon import TelegramClient, sync

api_id = ...
api_hash = ...
client = TelegramClient('session_name', api_id, api_hash)

messages = client.get_messages('GOOGLE')
messages[0].click()

This should be enough to click the first button of the last message in the channel.

Upvotes: 7

Related Questions