M
M
MaximPPP2021-11-18 17:43:29
Python
MaximPPP, 2021-11-18 17:43:29

Clicking buttons in a message from a Telegram bot using Python and Telethon?

I would like to automate pressing the buttons that the Telegram bot sends with a message. A bunch of Python and Telethon is used, the code is:

import telethon 
client = TelegramClient(StringSession(sessionid), api_id, api_hash)
@client.on(events.NewMessage())
async def handler(event):
  message = await client.get_messages(222222)
  print(message)
  await message.click()
client.start()
client.run_until_disconnected()


But when receiving a message with buttons, an error occurs AttributeError: 'TotalList' object has no attribute 'click'
full output including the received message:
[Message(id=8103, peer_id=PeerUser(user_id=22222), date=datetime.datetime(2021, 11, 18, 14, 13, 11, tzinfo=datetime.timezone.utc), message='Chose one:', out=False, mentioned=False, media_unread=False, silent=False, post=False, from_scheduled=False, legacy=False, edit_hide=False, pinned=False, from_id=None, fwd_from=None, via_bot_id=None, reply_to=None, media=None, reply_markup=ReplyInlineMarkup(rows=[KeyboardButtonRow(buttons=[KeyboardButtonCallback(text='Button1', data=b'\x0cikb:0:0|22', requires_password=False), KeyboardButtonCallback(text='Button2', data=b'\x0cikb:1:0|171', requires_password=False)])]), entities=[], views=None, forwards=None, replies=None, edit_date=None, post_author=None, grouped_id=None, restriction_reason=[], ttl_period=None), total=456]
Unhandled exception on handler
Traceback (most recent call last):
  File "C:\Users\mp\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\updates.py", line 454, in _dispatch_update
    await callback(event)
  File "C:\Users\mp\Documents\clicker.py", line 22, in handler
    await message.click(1,1)
AttributeError: 'TotalList' object has no attribute 'click'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-18
@MaximPPP

A quick google brought up this code: if messages is a collection of messages (and the name of the TotalList type hints at this!), then you really need to make sure that there is at least one message there, and select it. And also indicate the number of the button that we click.
await messages[0].click(0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question