1

Example text message I received on telegram:

Listen to best music: Ava Max - My Head & My Heart

My question is, how can my script check the message and eventually open the site via nested link? I tried:

      for entity in event.message.text:
            if isinstance(entity, MessageEntityTextUrl):
                open_url(entity.url)

but no luck - Script doesn't open the link.

//Edit implemented watzon's solution:

async def my_event_handler(event):
            msg = event.message.message
            for _, inner_text in msg.get_entities_text(MessageEntityTextUrl):
                    open_url(inner_text)

and now the error is:

...in my_event_handler
    for _, inner_text in msg.get_entities_text(MessageEntityTextUrl):
AttributeError: 'str' object has no attribute 'get_entities_text'

I must have missed my mistake here, what should I change in the msg?

//Edit2

msg = event.message

fixed the error but I still don't get the link as output.

RektLeft
  • 93
  • 1
  • 8
  • what is an `entity` here ? Can you print it and share that ? Because if .text if a string, then it is only one char – azro Dec 08 '21 at 20:40
  • In your implementation of @watzon's answer, just define `msg` as `msg = event.message`. `event.message.message` is a string representation of the message, that's why it has no attribute `'get_entities_text'`. – druskacik Dec 09 '21 at 13:39
  • @druskacik thank you very much, the error does not appear anymore but the next problem is that the _inner_text_ is actually the nested link *visible text*, not the link I can open. So looking at the example with Ava Max song above, my output is: **"Ava Max - My Head & My Heart"** BUT should be **youtube.com/....** How can I achieve this? is MessageEntityTextUrl used in the correct way here? – RektLeft Dec 09 '21 at 14:30
  • Will you please check whether the variable `_` contains the url? You could rewrite it like `for url, inner_text in msg.get_entities_text(MessageEntityTextUrl): open_url(url)` – druskacik Dec 09 '21 at 14:40
  • 1
    @druskacik so simple yet so good, thank you! '_' contained offset, length, url etc. of the Message entity so I used openURL(`_.url`) and now it does, indeed, open a nested link. There are other issues as far as I checked some cases but I think we are done here on this problem. Thank you so much. – RektLeft Dec 09 '21 at 14:50
  • No problem. If your problem is solved, please accept my or the other answer so that users see this question as solved. – druskacik Dec 09 '21 at 15:05

2 Answers2

3

Correct implementation of @watzon's answer would look something like this:

from telethon.tl.types import MessageEntityTextUrl

async def my_event_handler(event):
    msg = event.message
    for url_entity, inner_text in msg.get_entities_text(MessageEntityTextUrl):
        url = url_entity.url
        open_url(url)
        ...
druskacik
  • 2,176
  • 2
  • 13
  • 26
2

You should be using message.get_entities_text().

Example:

for _, inner_text in message.get_entities_text(MessageEntityTextUrl):
  open_url(inner_text)

Alternatively you can leave out the filter argument and loop over all of the provided entities, but given your example code this should work in place.

watzon
  • 2,401
  • 2
  • 33
  • 65
  • Thank you for a really quick answer. I implemented your solution and got the 'No attribute' error, could you please tell me whats the correct way of defining a message var? (msg in my case). – RektLeft Dec 08 '21 at 21:31
  • @RektLeft what was the error? You should be able to get the message from the event. Normally you'd be using `@client.on(events.NewMessage)` to decorate a method, and that method would have an `event` argument. In this case, `event.message` would give you the message that triggered the event. – watzon Dec 09 '21 at 22:18