import TelegramBot from "node-telegram-bot-api"; import { readerTaskEither as rte, readonlyArray, semigroup, string, } from "fp-ts"; import { flow, pipe } from "fp-ts/lib/function.js"; import { describeArticle } from "./api.mjs"; import config from "./config.mjs"; const bot = new TelegramBot(config.telegramBotToken, { polling: true, }); bot.on("channel_post", async (msg) => { const link = extractMessageLink(msg); if (!link) return; pipe( describeArticle(link.url), rte.map( flow( readonlyArray.foldMap({ ...string.Monoid, ...pipe(string.Semigroup, semigroup.intercalate("\n")), })((row) => `- ${row}`), string.trimLeft, ), ), rte.match( (err) => console.log({ err }), (res) => bot.editMessageText(msg.text + "\n\nYandexGPT:\n\n" + res, { chat_id: msg.chat.id, message_id: msg.message_id, }), ), )(config)(); }); // TODO: use option instead function extractMessageLink(msg) { const links = (msg.entities ?? []).filter(isLink); return links.length ? links[0] : null; } function isLink(msgEntity) { return msgEntity.type === "text_link"; }