G
G
GAFOR2020-08-16 12:43:23
Python
GAFOR, 2020-08-16 12:43:23

How to fix "AttributeError: 'KeyboardButton' object has no attribute 'data' PYTHON" error?

Mistake:

button_data = msg.reply_markup.rows[0].buttons[0].data
AttributeError: 'KeyboardButton' object has no attribute 'data'


The code:
from telethon import TelegramClient, sync, events

api_id = 
api_hash = ''
client = TelegramClient('boters', api_id, api_hash)
client.start()

dlgs = client.get_dialogs()
for dlg in dlgs:
  print(dlg.title)
for dlg in dlgs:
  if dlg.title == '':
    client.send_message('', "")
    tegmo = dlg

msgs = client.get_messages(tegmo, limit=30)

for msg in msgs:
  if msg.reply_markup is not None:

    button_data = msg.reply_markup.rows[0].buttons[0].data
    message_id = msg.id
    print(button_data, message_id)

from telethon.tl.functions.messages import GetBotCallbackAnswerRequest

resp = client(GetBotCallbackAnswerRequest(
  tegmo,
  58462,
  data=b'hatgiong;7187'
))
print(resp.message)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alekssamos, 2020-08-16
@alekssamos

There is no such button! Means write down between try except. If it doesn't, nothing will happen, if it does, then it will click.
Or do it print(msg.reply_markup.rows)and see what's there.
Here are some more similar questions:
* How to fix AttributeError: 'KeyboardButtonUrl' object has no attribute 'data' error in telethon?
* python - AttributeError: 'KeyboardButtonUrl' objec...
* Question: How would I interact with a bot's button...

R
Ruslan., 2020-08-16
@LaRN

Try using the built-in function dir() , through it you can find out what attributes an object has.
Like this:
dir(msg.reply_markup.rows[0].buttons[0])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question