Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract hyperlink from message #672

Open
Dashi321 opened this issue May 10, 2024 · 3 comments
Open

Extract hyperlink from message #672

Dashi321 opened this issue May 10, 2024 · 3 comments

Comments

@Dashi321
Copy link

I couldn't find the way to extract a link from a hyperlink within a text from a message that was sent in a channel.

@polRk
Copy link

polRk commented May 12, 2024

See Message entities

@nghiepdev
Copy link

You can parse the message to HTML and then extract links using regexp or the Cheerio DOM parser.
Example:

import {HTMLParser} from 'telegram/extensions/html';
import * as cherrio from 'cheerio';


const html = HTMLParser.unparse(message, entities);

const $ = cherrio.load(html);
// => Work with DOM like jQuery to extract your links
// => $("a:contains('Your Anchor')").attr("href");

@polRk
Copy link

polRk commented May 13, 2024

switch (history.className) {
    case "messages.ChannelMessages":
        for (const message of history.messages) {
            switch (message.className) {
                case "Message":
                    for (const entity of message.entities || []) {
                        switch (entity.className) {
                            case "MessageEntityUrl":
                                const url = message.rawText.slice(entity.offset, entity.offset + entity.length)
                                console.log(url)
                                break
                        }
                    }
            }
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants