initial commit

This commit is contained in:
Dmitriy Pleshevskiy 2023-06-28 14:45:04 +03:00
commit c0a65bbb30
Signed by: pleshevskiy
GPG Key ID: 79C4487B44403985
10 changed files with 1576 additions and 0 deletions

2
.env.example Normal file
View File

@ -0,0 +1,2 @@
TELEGRAM_BOT_TOKEN='xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
YANDEX_GPT_API_TOKEN='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# direnv
/.envrc
/.direnv/
# build
/result
node_modules

85
api.mjs Normal file
View File

@ -0,0 +1,85 @@
import {
reader,
readerTaskEither as rte,
readonlyArray,
taskEither,
} from "fp-ts";
import { flow, pipe } from "fp-ts/lib/function.js";
// parseJsonCont :: string -> string -> TaskEither string string
const parseJsonCont = (attr) => (source) => {
try {
const data = JSON.parse(source)[attr];
if (!data) throw new Error(`Key '${attr}' doesn't exist`);
return taskEither.right(data);
} catch (e) {
return taskEither.left(`Invalid json data: ${e}`);
}
};
const YANDEX_GPT_API_URL = "https://300.ya.ru/api/sharing-url";
// getYandexGptSharedUrl ::
// string ->
// ReaderTaskEither {yandexGptApiToken} string string
const getYandexGptSharedUrl = (articleUrl) =>
pipe(
reader.ask(),
reader.map(({ yandexGptApiToken }) =>
pipe(
taskEither.tryCatch(
() =>
fetch(YANDEX_GPT_API_URL, {
method: "POST",
headers: new Headers({
"Content-Type": "application/json",
"Authorization": `OAuth ${yandexGptApiToken}`,
}),
body: JSON.stringify({ article_url: articleUrl }),
}),
String,
),
/* TODO: fix to many requests error
{
ok: false,
error_code: 429,
description: 'Too Many Requests: retry after 5',
parameters: { retry_after: 5 }
}
*/
taskEither.flatMap((res) =>
taskEither.tryCatch(() => res.json(), String)
),
taskEither.flatMap((data) =>
data.status === "success" && data.sharing_url
? taskEither.right(data.sharing_url)
: taskEither.left(`Invalid response: ${JSON.stringify(data)}`)
),
)
),
);
// getYandexGptTesisesFromSharedUrl :: string -> TaskEither string[] string
const getYandexGptTesisesFromSharedUrl = (sharingUrl) =>
pipe(
taskEither.tryCatch(() => fetch(sharingUrl), String),
taskEither.flatMap((res) => taskEither.tryCatch(() => res.text(), String)),
taskEither.flatMap((html) =>
taskEither.fromNullable("Cannot find data in the shared url")(
/<script.+?type="application\/json".*?>([\s\S]+?)<\/script>/.exec(
html,
)[1],
)
),
taskEither.flatMap(parseJsonCont("body")),
taskEither.flatMap(parseJsonCont("thesis")),
taskEither.map(readonlyArray.map((t) => t.content)),
);
// describeArticle ::
// string ->
// ReaderTaskEither {yandexGptApiToken} string string
export const describeArticle = flow(
getYandexGptSharedUrl,
rte.flatMapTaskEither(getYandexGptTesisesFromSharedUrl),
);

17
bot.nix Normal file
View File

@ -0,0 +1,17 @@
{ pkgs ? import <nixpkgs> { } }:
pkgs.buildNpmPackage {
pname = "yandexgpt_tg_bot";
version = "0.1.0";
src = ./.;
npmDepsHash = "sha256-hkdmHBAXSTrEMzBas1Tz/ucElc1e6Z81wWQG7J0pSBM=";
dontBuild = true;
# The prepack script runs the build script, which we'd rather do in the build phase.
# npmPackFlags = [ "--ignore-scripts" ];
# NODE_OPTIONS = "--openssl-legacy-provider";
}

4
config.mjs Normal file
View File

@ -0,0 +1,4 @@
export default {
telegramBotToken: process.env.TELEGRAM_BOT_TOKEN,
yandexGptApiToken: process.env.YANDEX_GPT_API_TOKEN,
};

61
flake.lock Normal file
View File

@ -0,0 +1,61 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1687709756,
"narHash": "sha256-Y5wKlQSkgEK2weWdOu4J3riRd+kV/VCgHsqLNTTWQ/0=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "dbabf0ca0c0c4bce6ea5eaf65af5cb694d2082c7",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1687793116,
"narHash": "sha256-6xRgZ2E9r/BNam87vMkHJ/0EPTTKzeNwhw3abKilEE4=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9e4e0807d2142d17f463b26a8b796b3fe20a3011",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

20
flake.nix Normal file
View File

@ -0,0 +1,20 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs { inherit system; };
in
{
devShells.default = pkgs.mkShell {
packages = with pkgs; [
nodejs-18_x
prefetch-npm-deps
];
};
});
}

51
main.mjs Normal file
View File

@ -0,0 +1,51 @@
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,
});
console.log(config);
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)();
});
function extractMessageLink(msg) {
const links = (msg.entities ?? []).filter(isLink);
return links.length ? links[0] : null;
}
function isLink(msgEntity) {
return msgEntity.type === "text_link";
}

1314
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

15
package.json Normal file
View File

@ -0,0 +1,15 @@
{
"name": "yandexgpt_tg_bot",
"version": "0.1.0",
"private": true,
"type": "module",
"main": "./main.mjs",
"description": "",
"dependencies": {
"fp-ts": "^2.16.0",
"node-telegram-bot-api": "^0.61.0"
},
"bin": {
"yandexgpt_tg_bot": "./main.mjs"
}
}