Uomolepre
Uomolepre

Reputation: 121

How to get link, from hyperlink on telegram messages

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?

Upvotes: 1

Views: 3207

Answers (1)

nitanmarcel
nitanmarcel

Reputation: 93

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

Upvotes: 1

Related Questions