How to get button CallbackQuery in conversations of telethon library

Viewed 2088

I am learning about asyncio to use telethon module. I want to use buttons in conversations and get the output of the button selected by user.

@bot.on(events.NewMessage(incoming=True, pattern=r'start'))
async def chatbot(event):
   sender = await event.get_sender(); SENDER = sender.id
   async with bot.conversation(SENDER) as conv:
   await conv.send_message('Select a button :')
   await conv.send_message('Yes or no?', buttons=[
        Button.inline('Yes!', b'yes'),
        Button.inline('Nope', b'no')    ])

   if selected_button == 'yes' :
        # do something
   else:
       # do something

Using callbackquery as given in the docs, jumps to the handler(event) function of events.CallbackQuery(). But does not go back to the conv of chatbot function.

How can I get the selected button info and proceed further in the chatbot function ?

1 Answers

As of Telethon v1.11, you need to use Conversation.wait_event, which is a bit ugly but does the trick:

# Defined somewhere
def press_event(user_id):
    return events.CallbackQuery(func=lambda e: e.sender_id == user_id)

...

# Later in your conversation
press = await conv.wait_event(press_event(SENDER))
Related