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, polling: true,
}); });
console.log("The telegram bot listens for updates");
bot.on("channel_post", async (msg) => { bot.on("channel_post", async (msg) => {
const link = extractMessageLink(msg); const link = extractMessageLink(msg);
if (!link) return; if (!link) {
console.log("cannot find link in msg:", msg);
return;
}
pipe( pipe(
describeArticle(link.url), describeArticle(link),
rte.map( rte.map(
flow( flow(
readonlyArray.foldMap({ readonlyArray.foldMap({
@ -41,10 +46,21 @@ bot.on("channel_post", async (msg) => {
// TODO: use option instead // TODO: use option instead
function extractMessageLink(msg) { function extractMessageLink(msg) {
const links = (msg.entities ?? []).filter(isLink); return getTextLink(msg) || getMessageUrl(msg);
return links.length ? links[0] : null;
} }
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"; return msgEntity.type === "text_link";
} }

5
package-lock.json generated
View file

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