1

Is it available to wait inline button push or text message simultaneously in Telethon conversation?

async with bot.conversation(chat_id) as conv:
    buttons = [[Button.inline('Yes'), Button.inline('No')]]
    conv.send_message('To be or not no be? Answer yes|no, or write your own opinion', buttons=buttons)
    
    #This way we can wait for button press
    def press_event(user_id):
        return events.CallbackQuery(func=lambda e: e.sender_id == user_id)
    press = await conv.wait_event(press_event(sender_id))

    #...and this way we can wait for text message
    response = await conv.get_response()

    #And how can we wait for press event OR text message simultaneously?
  • 1
    See https://stackoverflow.com/a/62246569/; FSMs are more powerful. However the code you posted should work, simply use `asyncio.wait` or `gather` with both waiting calls. – Lonami Feb 12 '21 at 16:31

1 Answers1

2

So, here is the solution (thanks to Lonami)

@bot.on(events.NewMessage(pattern='/test'))  
async def worklogs(event):        
    chat_id = event.message.chat.id    
    sender = await event.get_sender()
    sender_id = sender.id
    async with bot.conversation(chat_id) as conv:
        
        
        def my_press_event(user_id):
            return events.CallbackQuery(func=lambda e: e.sender_id == user_id)

        buttons = [[Button.inline('Yes'), Button.inline('No')]]
        await conv.send_message('To be or not no be? Answer yes|no, or write your own opinion', buttons=buttons)
    
        tasks = [conv.wait_event(my_press_event(sender_id)), conv.get_response()]
        done, pendind = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
        event = done.pop().result()
        
        if type(event) is events.callbackquery.CallbackQuery.Event:
            selected = event.data.decode('utf-8')
            print(f'Got button: "{selected}"')
        elif type(event) is tl.patched.Message: 
            message = event.text
            print(f'Got text message: "{message}"')