gramjs
gramjs copied to clipboard
Extract hyperlink from message
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.
See Message entities
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");
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
}
}
}
}
}
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");
Exactly what I needed to get the full message. Thanks!