add support of url entity

This commit is contained in:
Dmitriy Pleshevskiy 2023-07-01 00:32:58 +03:00
parent d96a2d8416
commit 4e738348a5
Signed by: pleshevskiy
GPG key ID: 79C4487B44403985
2 changed files with 26 additions and 5 deletions

View file

@ -13,12 +13,17 @@ const bot = new TelegramBot(config.telegramBotToken, {
polling: true,
});
console.log("The telegram bot listens for updates");
bot.on("channel_post", async (msg) => {
const link = extractMessageLink(msg);
if (!link) return;
if (!link) {
console.log("cannot find link in msg:", msg);
return;
}
pipe(
describeArticle(link.url),
describeArticle(link),
rte.map(
flow(
readonlyArray.foldMap({
@ -41,10 +46,21 @@ bot.on("channel_post", async (msg) => {
// TODO: use option instead
function extractMessageLink(msg) {
const links = (msg.entities ?? []).filter(isLink);
return links.length ? links[0] : null;
return getTextLink(msg) || getMessageUrl(msg);
}
function isLink(msgEntity) {
const RE_URL =
/(http|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])/;
function getMessageUrl(msg) {
return RE_URL.exec(msg.text)[0];
}
function getTextLink(msg) {
const textLinks = (msg.entities ?? []).filter(isTextLink);
return textLinks.length ? links[0].url : null;
}
function isTextLink(msgEntity) {
return msgEntity.type === "text_link";
}

5
package-lock.json generated
View file

@ -1,13 +1,18 @@
{
"name": "yandexgpt_tg_bot",
"version": "0.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "yandexgpt_tg_bot",
"version": "0.1.0",
"dependencies": {
"fp-ts": "^2.16.0",
"node-telegram-bot-api": "^0.61.0"
},
"bin": {
"yandexgpt_tg_bot": "main.mjs"
}
},
"node_modules/ajv": {