How to get link, from hyperlink on telegram messages

Viewed 1483

I'm new with telethon (Python3) and I'm trying to take links from messages. If there is an hyperlink, this is shown on the console (after a print of the message).

Processore AMD Ryzen 7 5800X (8C/16T, 36MB di cache, fino a 4,7 GHz max Boost)

[Prrice: € 418.41]

️ Buy Now! ️ [️ Amazon.it]

Buy Now! is an hiperlink. Someone can Help Me?

1 Answers

The message object returned by telethon has an entities member which returns a list of entities and all you have to do is iter all the entries and check if they instance matches MessageEntityTextUrl then get the url member.

from telethon.tl.types import MessageEntityTextUrl
for entity in event.message:
    if isinstance(entity, MessageEntityTextUrl)
        print(entity.url) # your url

The above will return the URL, and if you also want the text you need to get .offset then either split the event.text and get the item at that offset or use any way you find it works for you

Related