@effect-ak/tg-bot-client 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ export const MESSAGE_EFFECTS = {
2
+ "🔥": "5104841245755180586",
3
+ "👍": "5107584321108051014",
4
+ "👎": "5104858069142078462",
5
+ "❤️": "5159385139981059251",
6
+ "🎉": "5046509860389126442",
7
+ "💩": "5046589136895476101"
8
+ };
9
+ export const messageEffectIdCodes = Object.keys(MESSAGE_EFFECTS);
10
+ export const isMessageEffect = (input) => {
11
+ return typeof input === "string" && input in MESSAGE_EFFECTS;
12
+ };
@@ -0,0 +1,23 @@
1
+ import { makePayload, methodPath } from "./request.js";
2
+ import { isTgBotApiResponse } from "./response.js";
3
+ const defaultBaseUrl = "https://api.telegram.org";
4
+ export const makeTgBotClient = (options) => {
5
+ const baseUrl = options.baseUrl ?? defaultBaseUrl;
6
+ const execute = async (method, input) => {
7
+ const httpResponse = await fetch(`${baseUrl}/bot${options.token}/${methodPath(method)}`, {
8
+ body: makePayload(input) ?? null,
9
+ method: "POST"
10
+ }).then(_ => _.json());
11
+ if (!isTgBotApiResponse(httpResponse))
12
+ throw new Error("Not valid response", {
13
+ cause: httpResponse
14
+ });
15
+ if (httpResponse.ok == false) {
16
+ console.warn(httpResponse);
17
+ }
18
+ return httpResponse;
19
+ };
20
+ return {
21
+ execute
22
+ };
23
+ };
@@ -0,0 +1,33 @@
1
+ export const methodPath = (methodName) => methodName
2
+ .split("_")
3
+ .reduce((result, word, step) => {
4
+ if (step == 0) {
5
+ return word;
6
+ }
7
+ else {
8
+ return result + word.at(0)?.toUpperCase() + word.slice(1);
9
+ }
10
+ }, "");
11
+ const hasFileContent = (input) => (typeof input == "object" && input != null) &&
12
+ ("file_content" in input && input.file_content instanceof Uint8Array) &&
13
+ ("file_name" in input && typeof input.file_name === "string" && input.file_name.length > 0);
14
+ export const makePayload = (body) => {
15
+ const entries = Object.entries(body);
16
+ if (entries.length == 0)
17
+ return undefined;
18
+ const result = new FormData();
19
+ for (const [key, value] of entries) {
20
+ if (!value)
21
+ continue;
22
+ if (typeof value != "object") {
23
+ result.append(key, `${value}`);
24
+ }
25
+ else if (hasFileContent(value)) {
26
+ result.append(key, new Blob([value.file_content]), value.file_name);
27
+ }
28
+ else {
29
+ result.append(key, JSON.stringify(value));
30
+ }
31
+ }
32
+ return result;
33
+ };
@@ -0,0 +1,6 @@
1
+ export const isTgBotApiResponse = (input) => {
2
+ return (("ok" in input && typeof input.ok == "boolean") &&
3
+ (typeof input.error_code === 'undefined' || typeof input.error_code === 'number') &&
4
+ (typeof input.description === 'undefined' || typeof input.description === 'string') &&
5
+ (typeof input.result === 'undefined' || typeof input.result === 'object'));
6
+ };
@@ -0,0 +1,4 @@
1
+ export * from "./client/const.js";
2
+ export * from "./client/factory.js";
3
+ export * from "./specification/api.js";
4
+ export * from "./specification/types.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@effect-ak/tg-bot-client",
3
+ "version": "0.0.2",
4
+ "type": "module",
5
+ "author": {
6
+ "name": "Aleksandr Kondaurov",
7
+ "email": "kondaurov.dev@gmail.com"
8
+ },
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "description": "Telegram bot client",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "keywords": [
17
+ "telegram",
18
+ "telegram bot api"
19
+ ],
20
+ "repository": {
21
+ "url": "https://github.com/kondaurovDev/effect-ak/tg-bot-client"
22
+ },
23
+ "license": "MIT",
24
+ "main": "./dist/cjs/index.js",
25
+ "module": "./dist/esm/index.js",
26
+ "types": "./dist/dts/index.d.ts",
27
+ "devDependencies": {
28
+ "@babel/cli": "^7.26.4",
29
+ "@babel/core": "^7.26.0",
30
+ "@babel/plugin-transform-export-namespace-from": "^7.25.9",
31
+ "@babel/plugin-transform-modules-commonjs": "^7.26.3",
32
+ "@types/node": "^22.10.1",
33
+ "effect": "^3.11.4",
34
+ "node-html-parser": "^6.1.13",
35
+ "ts-morph": "^24.0.0",
36
+ "tsc-alias": "^1.8.10",
37
+ "typescript": "^5.7.2",
38
+ "vitest": "^2.1.8",
39
+ "vite-tsconfig-paths": "^5.1.4"
40
+ },
41
+ "scripts": {
42
+ "build": "pnpm build-esm && pnpm build-cjs",
43
+ "build-esm": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
44
+ "build-cjs": "babel dist/esm --out-dir dist/cjs"
45
+ }
46
+ }
package/readme.md ADDED
@@ -0,0 +1,74 @@
1
+ ![NPM Version](https://img.shields.io/npm/v/%40effect-ak%2Ftg-bot-client)
2
+
3
+ ### What is it?
4
+
5
+ This is a client for interacting with the Telegram Bot API.
6
+ The main reason for creating this package is that Telegram does not provide an SDK for working with their API.
7
+
8
+ They only provide documentation in the form of a massive HTML page, which is very inconvenient for navigating and understanding what the Telegram Bot API offers.
9
+
10
+ ## Features:
11
+ - **Pure TypeScript Client**: This is a clean client written in TypeScript with no abstractions.
12
+ - **Complete**: The entire API is generated from the official documentation [https://core.telegram.org/bots/api](https://core.telegram.org/bots/api) using a [code generator](./codegen/main.ts).
13
+ - **Inline Documentation**: No need to read lengthy official documentation. All types and comments are available in JS DOC, allowing you to develop your bot without leaving your IDE.
14
+ - **Type Mapping**: Types from the documentation are converted to TypeScript types. For example, `Integer` becomes `number`, `True` becomes `boolean`, `String or Number` becomes `string | number`, and so on.
15
+ - **Readable Method Names**: Method names, such as `SetChatAdministratorCustomTitleInput`, are converted to snake_case for easier code readability, e.g., `set_chat_administrator_custom_title`.
16
+
17
+ ### Usage example
18
+
19
+ #### Install
20
+
21
+ `npm i @effect-ak/tg-bot-client`
22
+
23
+ #### Creating a Client
24
+
25
+ ```typescript
26
+ import { makeTgBotClient } from "effect-ak/tg-bot-client"
27
+
28
+ const client = makeTgBotClient({
29
+ token: "" //your token from bot father
30
+ });
31
+ ```
32
+
33
+ Now, `client` is an object that has an `execute` method. This method takes two arguments: the first is the API method, and the second is an object containing the arguments for that method.
34
+
35
+ #### 1. Sending a Message with an Effect
36
+
37
+ ```typescript
38
+ import { MESSAGE_EFFECTS } from "effect-ak/tg-bot-client"
39
+
40
+ await client.execute("send_message", {
41
+ chat_id: "???", // replace ??? with the chat number
42
+ text: "hey again",
43
+ message_effect_id: MESSAGE_EFFECTS["🔥"]
44
+ });
45
+ ```
46
+
47
+ #### 2. Sending a Dice
48
+
49
+ ```typescript
50
+ await client.execute("send_dice", {
51
+ chat_id: "???", // replace ??? with the chat number
52
+ emoji: "🎲"
53
+ });
54
+ ```
55
+
56
+ #### 3. Sending a Document
57
+
58
+ ```typescript
59
+ await client.execute("send_document", {
60
+ chat_id: "???", // replace ??? with the chat number
61
+ message_effect_id: MESSAGE_EFFECTS["🎉"],
62
+ document: {
63
+ file_content: Buffer.from("Hello!"),
64
+ file_name: "hello.txt"
65
+ },
66
+ caption: "simple text file"
67
+ })
68
+ ```
69
+
70
+ ---
71
+
72
+ ### Summary
73
+
74
+ This code generator and client will continue to be developed. However, for now, I have generated all the methods and types. If you find any errors, please let me know.