pythontelethon

Telethon - How to trigger a UNIQUE "events.NewMessage" by clicking inline button?


Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32 / telethon '1.38.1'

I'm learning this thing, making a bot. It has three NewMessage handlers:

@client.on(events.NewMessage(pattern='/start')) - which sends a button Button.inline('Click and send a music artist name...', b'send_name_button');

@client.on(events.NewMessage(pattern='/delete)) - which deletes all messages

@client.on(events.NewMessage(pattern=r'^((?!\/start|\/delete).)*$)) - which must handle messages that are not '/start' or '/delete'

I have an inline button data=b'send_name_button', and after it is clicked a unique @client.on(events.NewMessage) that is inside of that callback, must be triggered, and only after clicking this button

This is the whole code:

try:
    @client.on(events.NewMessage(pattern='/start'))
    async def respond_start(event):
        user = await event.get_sender() # name = utils.get_display_name(sender) ?
                    
        msg = await event.respond(
              f'Hello, {user.first_name} ☘️!\nThis bot will send 🎁 you a chosen song ' \
              '(mp3 file) from a songs list of an artist you chose ' \
              '🔊 (from .........)', 
        buttons=[ 
            Button.inline('Click and send a music artist name...', b'send_name_button')
        ]
    )
    append_msg_id(msg_ids, msg.id)

    except Exception as ex:
        print(f'Ecxception: respond_start: {ex}')
            
try:
    @client.on(events.NewMessage(pattern='/delete'))
    async def delete_dialog(event):
        append_msg_id(msg_ids, event.id)
        await client.delete_messages(event.chat_id, msg_ids)
                        
        clean_msg_ids(msg_ids)                  
except Exception as ex:
    print(f'Exception: delete_messages: {ex}')

# not /start and not /delete
try:
    @client.on(events.NewMessage(pattern=r'^((?!\/start|\/delete).)*$'))
    async def respond_else(event):
        append_msg_id(msg_ids, event.id)

        msg = await event.respond('Try [/start](/start)...')                   
        append_msg_id(msg_ids, msg.id)

except Exception as ex:
    print(f'Exception: respond_else: {ex}')

This is the button handler

@client.on(events.CallbackQuery(data=b'send_name_button'))
async def handler_send_name_button(event):
    msg = await event.respond(f'Type the name of a musician. After that the bot will' \
                               ' send the list of tracks of the chosen musician...' )
         
    functions.append_msg_id(msg_ids, event.id)
    functions.append_msg_id(msg_ids, msg.id)

    @client.on(events.NewMessage):
    async def handler_a_name_is_chosen_after_clicking_send_name_button(event):
        pass # THIS HANDLER MUST BE TRIGGERED ONLY AFTER THE BUTTON OF data=b'send_name_button' IS CLICKED

The problem is that clicking the button and sending a message AFTER THAT must trigger the nested handler handler_a_name_is_chosen_after_clicking_send_name_button (THIS IS WHAT I NEED) but it triggers the 3 ones above.

How to make a link between clicking the button data=b'send_name_button' and calling the handler handler_a_name_is_chosen_after_clicking_send_name_button?

the whole code: https://pastebin.com/EVMYPL7J


Solution

  • Ok it's solved by just using 2 bool markers. They act according to their logical names

    send_name_button_event_was_triggered = False
    send_name_button_event_was_finished = False

    code https://pastebin.com/PvgZ8SLN