Issue
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 ?
Solution
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))
Answered By - Lonami
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.