58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
import TelegramBot from "node-telegram-bot-api";
|
|
import {
|
|
option,
|
|
readerTaskEither as rte,
|
|
readonlyArray,
|
|
semigroup,
|
|
string,
|
|
taskEither,
|
|
} from "fp-ts";
|
|
import { flow, pipe } from "fp-ts/lib/function.js";
|
|
import { describeArticle } from "./api.mjs";
|
|
import { extractMessageLink } from "./message.mjs";
|
|
import config from "./config.mjs";
|
|
|
|
const bot = new TelegramBot(config.telegramBotToken, {
|
|
polling: true,
|
|
});
|
|
|
|
console.log("The telegram bot listens for updates");
|
|
|
|
bot.on("channel_post", (msg) => {
|
|
const link = extractMessageLink(msg);
|
|
if (option.isNone(link)) {
|
|
console.log("cannot find link in msg:", msg);
|
|
return;
|
|
}
|
|
|
|
pipe(
|
|
describeArticle(link.value),
|
|
rte.map(
|
|
flow(
|
|
readonlyArray.foldMap({
|
|
...string.Monoid,
|
|
...pipe(string.Semigroup, semigroup.intercalate("\n")),
|
|
})((row) => `- ${row}`),
|
|
string.trimLeft,
|
|
),
|
|
),
|
|
rte.flatMapTaskEither(
|
|
(res) =>
|
|
taskEither.tryCatch(
|
|
() =>
|
|
bot.editMessageText(
|
|
msg.text + "\n\nYandexGpt:\n\n" + res,
|
|
{
|
|
chat_id: (msg.forward_from_chat ?? msg.chat).id,
|
|
message_id: msg.forward_from_message_id ?? msg.message_id,
|
|
},
|
|
),
|
|
String,
|
|
),
|
|
),
|
|
rte.match(
|
|
(err) => console.log({ err, msg }),
|
|
(res) => console.log("done", msg.message_id),
|
|
),
|
|
)(config)();
|
|
});
|