diff --git a/main.mjs b/main.mjs index 0eca207..016eba9 100644 --- a/main.mjs +++ b/main.mjs @@ -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"; } diff --git a/package-lock.json b/package-lock.json index d302886..9597741 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": {