skip to Main Content

I am making a script to analyse a telegram channel.

I would like to get the event.id or event.text of the original message when the channel is replying on a previous message.

Of course I looked in into the documentation but I am not able to figure it out.

Clarification of the question

My code:

@telegram_client.on(events.NewMessage(chats=testchannel))
def new_message_testchannel(event):
    message_raw_text = event.raw_text
    if event.is_reply:
        print('Reply')
        # Get original message ID and or content
        message_raw_text_reply = event.get_message_reply()
        # or
        message_raw_text_reply = event.get_message_reply().raw_text
        # or
        message_raw_text_reply = event.get_message_reply().id
        print(message_raw_text_reply)
    else:
        print('Normal message: ', message_raw_text)


telegram_client.start()
telegram_client.run_until_disconnected()

2

Answers


  1. Chosen as BEST ANSWER

    It had indeed something to do with the async here is my code example that is working :-)

    @telegram_client.on(events.NewMessage(chats=testchannel))
    async def new_message_testchannel(event):
        message_raw_text = event.raw_text
        if event.is_reply:
            print(event.id)
            reply = await event.get_reply_message()
            reply_raw_text = reply.raw_text
            reply_id = reply.id
            print('Is reply')
            print(reply_raw_text, reply_id)
            print(type(reply))
    

  2. Your code is partially correct but is missing the asyncio part.
    Telethon is an async library so all network calls need to be awaited.

    @telegram_client.on(events.NewMessage(chats=testchannel))
    async def new_message_testchannel(event):
        message_raw_text = event.raw_text
        if event.is_reply:
            print('Reply')
            # Get original message ID and or content
            message_reply = await event.get_message_reply()
            # or
            message_raw_text_reply = message_reply.raw_text
            # or
            message_id_reply = message_reply.id
            print(message_raw_text_reply)
            print(message_id_reply )
        else:
            print('Normal message: ', message_raw_text)
    
    
    telegram_client.start()
    telegram_client.run_until_disconnected()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search