@effect-ak/tg-bot-client 0.0.2 → 0.0.4
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/execute-request.js +32 -0
- 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/esm/client/_client.js +15 -0
- package/dist/esm/client/download-file.js +17 -0
- package/dist/esm/client/execute-request.js +29 -0
- package/dist/esm/client/request.js +0 -10
- package/dist/esm/index.js +1 -1
- package/package.json +5 -4
- package/readme.md +17 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.makeTgBotClient = void 0;
|
|
7
|
+
var _executeRequest = require("./execute-request.js");
|
|
8
|
+
var _downloadFile = require("./download-file.js");
|
|
9
|
+
const defaultBaseUrl = "https://api.telegram.org";
|
|
10
|
+
const makeTgBotClient = inputConfig => {
|
|
11
|
+
const config = {
|
|
12
|
+
...inputConfig,
|
|
13
|
+
baseUrl: inputConfig.baseUrl ?? defaultBaseUrl
|
|
14
|
+
};
|
|
15
|
+
const execute = (0, _executeRequest.makeExecute)(config);
|
|
16
|
+
const file = (0, _downloadFile.makeDownloadFile)(config, execute);
|
|
17
|
+
return {
|
|
18
|
+
execute,
|
|
19
|
+
...file
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
exports.makeTgBotClient = makeTgBotClient;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.makeDownloadFile = void 0;
|
|
7
|
+
const makeDownloadFile = (config, execute) => {
|
|
8
|
+
const getFile = async input => {
|
|
9
|
+
const response = await execute("get_file", input);
|
|
10
|
+
response.result?.file_path;
|
|
11
|
+
const file_path = response.result?.file_path;
|
|
12
|
+
if (!file_path || file_path.length == 0) throw new Error("NoFilePath", {
|
|
13
|
+
cause: response
|
|
14
|
+
});
|
|
15
|
+
const file_name = file_path.replaceAll("/", "-");
|
|
16
|
+
const url = `${config.baseUrl}/file/bot${config.token}/${file_path}`;
|
|
17
|
+
const fileContent = await fetch(url).then(_ => _.arrayBuffer());
|
|
18
|
+
const file = new File([new Uint8Array(fileContent)], file_name);
|
|
19
|
+
return file;
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
getFile
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
exports.makeDownloadFile = makeDownloadFile;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.makeExecute = void 0;
|
|
7
|
+
var _request = require("./request.js");
|
|
8
|
+
var _response = require("./response.js");
|
|
9
|
+
const makeExecute = config => {
|
|
10
|
+
const execute = async (method, input) => {
|
|
11
|
+
const httpResponse = await fetch(`${config.baseUrl}/bot${config.token}/${snakeToCamel(method)}`, {
|
|
12
|
+
body: (0, _request.makePayload)(input) ?? null,
|
|
13
|
+
method: "POST"
|
|
14
|
+
}).then(_ => _.json());
|
|
15
|
+
if (!(0, _response.isTgBotApiResponse)(httpResponse)) throw new Error("Not valid response", {
|
|
16
|
+
cause: httpResponse
|
|
17
|
+
});
|
|
18
|
+
if (httpResponse.ok == false) {
|
|
19
|
+
console.warn(httpResponse);
|
|
20
|
+
}
|
|
21
|
+
return httpResponse;
|
|
22
|
+
};
|
|
23
|
+
return execute;
|
|
24
|
+
};
|
|
25
|
+
exports.makeExecute = makeExecute;
|
|
26
|
+
const snakeToCamel = methodName => methodName.split("_").reduce((result, word, step) => {
|
|
27
|
+
if (step == 0) {
|
|
28
|
+
return word;
|
|
29
|
+
} else {
|
|
30
|
+
return result + word.at(0)?.toUpperCase() + word.slice(1);
|
|
31
|
+
}
|
|
32
|
+
}, "");
|
|
@@ -3,15 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
7
|
-
const methodPath = methodName => methodName.split("_").reduce((result, word, step) => {
|
|
8
|
-
if (step == 0) {
|
|
9
|
-
return word;
|
|
10
|
-
} else {
|
|
11
|
-
return result + word.at(0)?.toUpperCase() + word.slice(1);
|
|
12
|
-
}
|
|
13
|
-
}, "");
|
|
14
|
-
exports.methodPath = methodPath;
|
|
6
|
+
exports.makePayload = void 0;
|
|
15
7
|
const hasFileContent = input => typeof input == "object" && input != null && "file_content" in input && input.file_content instanceof Uint8Array && "file_name" in input && typeof input.file_name === "string" && input.file_name.length > 0;
|
|
16
8
|
const makePayload = body => {
|
|
17
9
|
const entries = Object.entries(body);
|
package/dist/cjs/index.js
CHANGED
|
@@ -14,14 +14,14 @@ Object.keys(_const).forEach(function (key) {
|
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
16
|
});
|
|
17
|
-
var
|
|
18
|
-
Object.keys(
|
|
17
|
+
var _client = require("./client/_client.js");
|
|
18
|
+
Object.keys(_client).forEach(function (key) {
|
|
19
19
|
if (key === "default" || key === "__esModule") return;
|
|
20
|
-
if (key in exports && exports[key] ===
|
|
20
|
+
if (key in exports && exports[key] === _client[key]) return;
|
|
21
21
|
Object.defineProperty(exports, key, {
|
|
22
22
|
enumerable: true,
|
|
23
23
|
get: function () {
|
|
24
|
-
return
|
|
24
|
+
return _client[key];
|
|
25
25
|
}
|
|
26
26
|
});
|
|
27
27
|
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SetOptional } from "type-fest";
|
|
2
|
+
export type BotConfig = {
|
|
3
|
+
token: string;
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
};
|
|
6
|
+
export type TgBotClient = ReturnType<typeof makeTgBotClient>;
|
|
7
|
+
export declare const makeTgBotClient: (inputConfig: SetOptional<BotConfig, "baseUrl">) => {
|
|
8
|
+
readonly getFile: (input: {
|
|
9
|
+
file_id: string;
|
|
10
|
+
}) => Promise<import("buffer").File>;
|
|
11
|
+
readonly execute: <M extends keyof import("../index.js").Api>(method: M, input: Parameters<import("../index.js").Api[M]>[0]) => Promise<import("./response.js").TgBotApiResponse<ReturnType<import("../index.js").Api[M]>>>;
|
|
12
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BotConfig } from "./_client.js";
|
|
2
|
+
import type { ExecuteRequest } from "./execute-request.js";
|
|
3
|
+
export declare const makeDownloadFile: (config: BotConfig, execute: ExecuteRequest) => {
|
|
4
|
+
readonly getFile: (input: {
|
|
5
|
+
file_id: string;
|
|
6
|
+
}) => Promise<import("buffer").File>;
|
|
7
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BotConfig } from "./_client.js";
|
|
2
|
+
import type { Api } from "../specification/api.js";
|
|
3
|
+
import { TgBotApiResponse } from "./response.js";
|
|
4
|
+
export type ExecuteRequest = ReturnType<typeof makeExecute>;
|
|
5
|
+
export declare const makeExecute: (config: BotConfig) => <M extends keyof Api>(method: M, input: Parameters<Api[M]>[0]) => Promise<TgBotApiResponse<ReturnType<Api[M]>>>;
|
package/dist/dts/index.d.ts
CHANGED
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { makePayload } from "./request.js";
|
|
2
|
+
import { isTgBotApiResponse } from "./response.js";
|
|
3
|
+
export const makeExecute = (config) => {
|
|
4
|
+
const execute = async (method, input) => {
|
|
5
|
+
const httpResponse = await fetch(`${config.baseUrl}/bot${config.token}/${snakeToCamel(method)}`, {
|
|
6
|
+
body: makePayload(input) ?? null,
|
|
7
|
+
method: "POST"
|
|
8
|
+
}).then(_ => _.json());
|
|
9
|
+
if (!isTgBotApiResponse(httpResponse))
|
|
10
|
+
throw new Error("Not valid response", {
|
|
11
|
+
cause: httpResponse
|
|
12
|
+
});
|
|
13
|
+
if (httpResponse.ok == false) {
|
|
14
|
+
console.warn(httpResponse);
|
|
15
|
+
}
|
|
16
|
+
return httpResponse;
|
|
17
|
+
};
|
|
18
|
+
return execute;
|
|
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.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Aleksandr Kondaurov",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"telegram bot api"
|
|
19
19
|
],
|
|
20
20
|
"repository": {
|
|
21
|
-
"url": "https://github.com/
|
|
21
|
+
"url": "https://github.com/effect-ak/tg-bot-client"
|
|
22
22
|
},
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"main": "./dist/cjs/index.js",
|
|
@@ -34,9 +34,10 @@
|
|
|
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": {
|
|
42
43
|
"build": "pnpm build-esm && pnpm build-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
|