@effect-ak/tg-bot-client 0.0.3 → 0.0.6
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.
- package/dist/cjs/client/_client.js +22 -0
- package/dist/cjs/client/download-file.js +25 -0
- package/dist/cjs/client/{factory.js → execute-request.js} +12 -9
- package/dist/cjs/client/request.js +1 -9
- package/dist/cjs/index.js +4 -4
- package/dist/dts/client/_client.d.ts +12 -0
- package/dist/dts/client/download-file.d.ts +7 -0
- package/dist/dts/client/execute-request.d.ts +5 -0
- package/dist/dts/client/request.d.ts +0 -1
- package/dist/dts/index.d.ts +1 -1
- package/dist/dts/specification/api.d.ts +131 -132
- package/dist/esm/client/_client.js +15 -0
- package/dist/esm/client/download-file.js +17 -0
- package/dist/esm/client/{factory.js → execute-request.js} +14 -8
- package/dist/esm/client/request.js +0 -10
- package/dist/esm/index.js +1 -1
- package/package.json +5 -3
- package/readme.md +17 -0
- package/dist/dts/client/factory.d.ts +0 -9
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { makeExecute } from "./execute-request.js";
|
|
2
|
+
import { makeDownloadFile } from "./download-file.js";
|
|
3
|
+
const defaultBaseUrl = "https://api.telegram.org";
|
|
4
|
+
export const makeTgBotClient = (inputConfig) => {
|
|
5
|
+
const config = {
|
|
6
|
+
...inputConfig,
|
|
7
|
+
baseUrl: inputConfig.baseUrl ?? defaultBaseUrl
|
|
8
|
+
};
|
|
9
|
+
const execute = makeExecute(config);
|
|
10
|
+
const file = makeDownloadFile(config, execute);
|
|
11
|
+
return {
|
|
12
|
+
execute,
|
|
13
|
+
...file
|
|
14
|
+
};
|
|
15
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const makeDownloadFile = (config, execute) => {
|
|
2
|
+
const getFile = async (input) => {
|
|
3
|
+
const response = await execute("get_file", input);
|
|
4
|
+
response.result?.file_path;
|
|
5
|
+
const file_path = response.result?.file_path;
|
|
6
|
+
if (!file_path || file_path.length == 0)
|
|
7
|
+
throw new Error("NoFilePath", { cause: response });
|
|
8
|
+
const file_name = file_path.replaceAll("/", "-");
|
|
9
|
+
const url = `${config.baseUrl}/file/bot${config.token}/${file_path}`;
|
|
10
|
+
const fileContent = await fetch(url).then(_ => _.arrayBuffer());
|
|
11
|
+
const file = new File([new Uint8Array(fileContent)], file_name);
|
|
12
|
+
return file;
|
|
13
|
+
};
|
|
14
|
+
return {
|
|
15
|
+
getFile
|
|
16
|
+
};
|
|
17
|
+
};
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { makePayload
|
|
1
|
+
import { makePayload } from "./request.js";
|
|
2
2
|
import { isTgBotApiResponse } from "./response.js";
|
|
3
|
-
const
|
|
4
|
-
export const makeTgBotClient = (options) => {
|
|
5
|
-
const baseUrl = options.baseUrl ?? defaultBaseUrl;
|
|
3
|
+
export const makeExecute = (config) => {
|
|
6
4
|
const execute = async (method, input) => {
|
|
7
|
-
const httpResponse = await fetch(`${baseUrl}/bot${
|
|
5
|
+
const httpResponse = await fetch(`${config.baseUrl}/bot${config.token}/${snakeToCamel(method)}`, {
|
|
8
6
|
body: makePayload(input) ?? null,
|
|
9
7
|
method: "POST"
|
|
10
8
|
}).then(_ => _.json());
|
|
@@ -17,7 +15,15 @@ export const makeTgBotClient = (options) => {
|
|
|
17
15
|
}
|
|
18
16
|
return httpResponse;
|
|
19
17
|
};
|
|
20
|
-
return
|
|
21
|
-
execute
|
|
22
|
-
};
|
|
18
|
+
return execute;
|
|
23
19
|
};
|
|
20
|
+
const snakeToCamel = (methodName) => methodName
|
|
21
|
+
.split("_")
|
|
22
|
+
.reduce((result, word, step) => {
|
|
23
|
+
if (step == 0) {
|
|
24
|
+
return word;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return result + word.at(0)?.toUpperCase() + word.slice(1);
|
|
28
|
+
}
|
|
29
|
+
}, "");
|
|
@@ -1,13 +1,3 @@
|
|
|
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
1
|
const hasFileContent = (input) => (typeof input == "object" && input != null) &&
|
|
12
2
|
("file_content" in input && input.file_content instanceof Uint8Array) &&
|
|
13
3
|
("file_name" in input && typeof input.file_name === "string" && input.file_name.length > 0);
|
package/dist/esm/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@effect-ak/tg-bot-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Aleksandr Kondaurov",
|
|
@@ -34,11 +34,13 @@
|
|
|
34
34
|
"node-html-parser": "^6.1.13",
|
|
35
35
|
"ts-morph": "^24.0.0",
|
|
36
36
|
"tsc-alias": "^1.8.10",
|
|
37
|
+
"type-fest": "^4.30.0",
|
|
37
38
|
"typescript": "^5.7.2",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
39
|
+
"vite-tsconfig-paths": "^5.1.4",
|
|
40
|
+
"vitest": "^2.1.8"
|
|
40
41
|
},
|
|
41
42
|
"scripts": {
|
|
43
|
+
"gen": "bun run codegen/main",
|
|
42
44
|
"build": "pnpm build-esm && pnpm build-cjs",
|
|
43
45
|
"build-esm": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|
|
44
46
|
"build-cjs": "babel dist/esm --out-dir dist/cjs"
|
package/readme.md
CHANGED
|
@@ -67,6 +67,23 @@ await client.execute("send_document", {
|
|
|
67
67
|
})
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
+
|
|
71
|
+
#### 4. Getting a file
|
|
72
|
+
|
|
73
|
+
In order to download file from Telegram server we need to send two http requests:
|
|
74
|
+
1. execute `get_file` and get `remote_path`
|
|
75
|
+
2. get file content via GET request with different url
|
|
76
|
+
|
|
77
|
+
`client.getFile` does exactly that. It returns [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File)
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const file =
|
|
81
|
+
await client.getFile({
|
|
82
|
+
file_id: fileId
|
|
83
|
+
});
|
|
84
|
+
// file is File
|
|
85
|
+
```
|
|
86
|
+
|
|
70
87
|
---
|
|
71
88
|
|
|
72
89
|
### Summary
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Api } from "../specification/api.js";
|
|
2
|
-
import { TgBotApiResponse } from "./response.js";
|
|
3
|
-
export type TgBotClient = ReturnType<typeof makeTgBotClient>;
|
|
4
|
-
export declare const makeTgBotClient: (options: {
|
|
5
|
-
token: string;
|
|
6
|
-
baseUrl?: string;
|
|
7
|
-
}) => {
|
|
8
|
-
readonly execute: <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Promise<TgBotApiResponse<ReturnType<Api[M]>>>;
|
|
9
|
-
};
|