@effect-ak/tg-bot-client 1.3.4 → 1.4.1
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/CHANGELOG.md +14 -0
- package/dist/index.cjs +59 -99
- package/dist/index.d.cts +26 -46
- package/dist/index.d.ts +26 -46
- package/dist/index.js +58 -91
- package/package.json +13 -7
- package/readme.md +11 -262
- package/src/client.ts +56 -16
- package/src/execute.ts +93 -20
- package/src/index.ts +24 -7
- package/test/execute.test.ts +31 -19
- package/test/file.test.ts +6 -4
- package/src/client-file.ts +0 -57
- package/src/const.ts +0 -20
- package/src/errors.ts +0 -18
- package/src/guards.ts +0 -27
- package/src/utils.ts +0 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @effect-ak/tg-bot-client
|
|
2
2
|
|
|
3
|
+
## 1.4.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 8ac8abd: Add homepage, keywords, and update documentation links to tg-bot-sdk.website
|
|
8
|
+
- Updated dependencies [8ac8abd]
|
|
9
|
+
- @effect-ak/tg-bot-api@1.3.1
|
|
10
|
+
|
|
11
|
+
## 1.4.0
|
|
12
|
+
|
|
13
|
+
### Minor Changes
|
|
14
|
+
|
|
15
|
+
- c5b9674: Replace thrown `TgBotClientError` with `ClientResult<T>` return type for all client methods
|
|
16
|
+
|
|
3
17
|
## 1.3.4
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -21,43 +21,39 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
MESSAGE_EFFECTS: () => MESSAGE_EFFECTS,
|
|
24
|
-
TG_BOT_API_URL: () => TG_BOT_API_URL,
|
|
25
|
-
TgBotClientError: () => TgBotClientError,
|
|
26
24
|
executeTgBotMethod: () => executeTgBotMethod,
|
|
27
|
-
getFile: () => getFile,
|
|
28
|
-
isFileContent: () => isFileContent,
|
|
29
|
-
isMessageEffect: () => isMessageEffect,
|
|
30
|
-
isTgBotApiResponse: () => isTgBotApiResponse,
|
|
31
25
|
makePayload: () => makePayload,
|
|
32
26
|
makeTgBotClient: () => makeTgBotClient,
|
|
33
|
-
messageEffectIdCodes: () => messageEffectIdCodes
|
|
34
|
-
snakeToCamel: () => snakeToCamel
|
|
27
|
+
messageEffectIdCodes: () => messageEffectIdCodes
|
|
35
28
|
});
|
|
36
29
|
module.exports = __toCommonJS(index_exports);
|
|
37
30
|
|
|
38
|
-
// src/
|
|
39
|
-
var TgBotClientError = class extends Error {
|
|
40
|
-
_tag = "TgBotClientError";
|
|
41
|
-
cause;
|
|
42
|
-
constructor(options) {
|
|
43
|
-
super(`TgBotClientError: ${options.cause._tag}`);
|
|
44
|
-
this.cause = options.cause;
|
|
45
|
-
this.name = "TgBotClientError";
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
// src/guards.ts
|
|
31
|
+
// src/execute.ts
|
|
50
32
|
var isFileContent = (input) => typeof input == "object" && input != null && "file_content" in input && input.file_content instanceof Uint8Array && "file_name" in input && typeof input.file_name == "string";
|
|
51
33
|
var isTgBotApiResponse = (input) => typeof input == "object" && input != null && "ok" in input && typeof input.ok == "boolean";
|
|
52
|
-
|
|
53
|
-
|
|
34
|
+
var MESSAGE_EFFECTS = {
|
|
35
|
+
"\u{1F525}": "5104841245755180586",
|
|
36
|
+
"\u{1F44D}": "5107584321108051014",
|
|
37
|
+
"\u{1F44E}": "5104858069142078462",
|
|
38
|
+
"\u2764\uFE0F": "5159385139981059251",
|
|
39
|
+
"\u{1F389}": "5046509860389126442",
|
|
40
|
+
"\u{1F4A9}": "5046589136895476101"
|
|
41
|
+
};
|
|
42
|
+
var messageEffectIdCodes = Object.keys(
|
|
43
|
+
MESSAGE_EFFECTS
|
|
44
|
+
);
|
|
45
|
+
var isMessageEffect = (input) => {
|
|
46
|
+
return typeof input === "string" && input in MESSAGE_EFFECTS;
|
|
47
|
+
};
|
|
54
48
|
var snakeToCamel = (str) => {
|
|
55
49
|
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
56
50
|
};
|
|
57
|
-
|
|
58
|
-
// src/execute.ts
|
|
59
51
|
async function executeTgBotMethod(params) {
|
|
60
|
-
const { config, method
|
|
52
|
+
const { config, method } = params;
|
|
53
|
+
let { input } = params;
|
|
54
|
+
if ("message_effect_id" in input && isMessageEffect(input.message_effect_id)) {
|
|
55
|
+
input = { ...input, message_effect_id: MESSAGE_EFFECTS[input.message_effect_id] };
|
|
56
|
+
}
|
|
61
57
|
let httpResponse;
|
|
62
58
|
try {
|
|
63
59
|
httpResponse = await fetch(
|
|
@@ -68,33 +64,28 @@ async function executeTgBotMethod(params) {
|
|
|
68
64
|
}
|
|
69
65
|
);
|
|
70
66
|
} catch (cause) {
|
|
71
|
-
|
|
72
|
-
cause: { _tag: "ClientInternalError", cause }
|
|
73
|
-
});
|
|
67
|
+
return { ok: false, error: { _tag: "ClientInternalError", cause } };
|
|
74
68
|
}
|
|
75
69
|
let response;
|
|
76
70
|
try {
|
|
77
71
|
response = await httpResponse.json();
|
|
78
72
|
} catch {
|
|
79
|
-
|
|
80
|
-
cause: { _tag: "NotJsonResponse", response: httpResponse }
|
|
81
|
-
});
|
|
73
|
+
return { ok: false, error: { _tag: "NotJsonResponse", response: httpResponse } };
|
|
82
74
|
}
|
|
83
75
|
if (!isTgBotApiResponse(response)) {
|
|
84
|
-
|
|
85
|
-
cause: { _tag: "UnexpectedResponse", response }
|
|
86
|
-
});
|
|
76
|
+
return { ok: false, error: { _tag: "UnexpectedResponse", response } };
|
|
87
77
|
}
|
|
88
78
|
if (!httpResponse.ok) {
|
|
89
|
-
|
|
90
|
-
|
|
79
|
+
return {
|
|
80
|
+
ok: false,
|
|
81
|
+
error: {
|
|
91
82
|
_tag: "NotOkResponse",
|
|
92
83
|
...response.error_code ? { errorCode: response.error_code } : {},
|
|
93
84
|
...response.description ? { details: response.description } : {}
|
|
94
85
|
}
|
|
95
|
-
}
|
|
86
|
+
};
|
|
96
87
|
}
|
|
97
|
-
return response.result;
|
|
88
|
+
return { ok: true, data: response.result };
|
|
98
89
|
}
|
|
99
90
|
var makePayload = (body) => {
|
|
100
91
|
const entries = Object.entries(body);
|
|
@@ -113,59 +104,8 @@ var makePayload = (body) => {
|
|
|
113
104
|
return result;
|
|
114
105
|
};
|
|
115
106
|
|
|
116
|
-
// src/client-file.ts
|
|
117
|
-
var getFile = async (params) => {
|
|
118
|
-
const { fileId, config, execute } = params;
|
|
119
|
-
const response = await execute("get_file", { file_id: fileId });
|
|
120
|
-
const file_path = response.file_path;
|
|
121
|
-
if (!file_path || file_path.length === 0) {
|
|
122
|
-
throw new TgBotClientError({
|
|
123
|
-
cause: {
|
|
124
|
-
_tag: "UnableToGetFile",
|
|
125
|
-
cause: "File path not defined"
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
const file_name = file_path.replaceAll("/", "-");
|
|
130
|
-
const url = `${config.base_url}/file/bot${config.bot_token}/${file_path}`;
|
|
131
|
-
let content;
|
|
132
|
-
try {
|
|
133
|
-
content = await fetch(url).then((_) => _.arrayBuffer());
|
|
134
|
-
} catch (cause) {
|
|
135
|
-
throw new TgBotClientError({
|
|
136
|
-
cause: { _tag: "UnableToGetFile", cause }
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
const base64String = () => Buffer.from(content).toString("base64");
|
|
140
|
-
const file = () => new File([content], file_name, {
|
|
141
|
-
...params.type ? { type: params.type } : {}
|
|
142
|
-
});
|
|
143
|
-
return {
|
|
144
|
-
content,
|
|
145
|
-
file_name,
|
|
146
|
-
base64String,
|
|
147
|
-
file
|
|
148
|
-
};
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
// src/const.ts
|
|
152
|
-
var TG_BOT_API_URL = "https://api.telegram.org";
|
|
153
|
-
var MESSAGE_EFFECTS = {
|
|
154
|
-
"\u{1F525}": "5104841245755180586",
|
|
155
|
-
"\u{1F44D}": "5107584321108051014",
|
|
156
|
-
"\u{1F44E}": "5104858069142078462",
|
|
157
|
-
"\u2764\uFE0F": "5159385139981059251",
|
|
158
|
-
"\u{1F389}": "5046509860389126442",
|
|
159
|
-
"\u{1F4A9}": "5046589136895476101"
|
|
160
|
-
};
|
|
161
|
-
var messageEffectIdCodes = Object.keys(
|
|
162
|
-
MESSAGE_EFFECTS
|
|
163
|
-
);
|
|
164
|
-
var isMessageEffect = (input) => {
|
|
165
|
-
return typeof input === "string" && input in MESSAGE_EFFECTS;
|
|
166
|
-
};
|
|
167
|
-
|
|
168
107
|
// src/client.ts
|
|
108
|
+
var TG_BOT_API_URL = "https://api.telegram.org";
|
|
169
109
|
function makeTgBotClient(config) {
|
|
170
110
|
const tgConfig = {
|
|
171
111
|
bot_token: config.bot_token,
|
|
@@ -176,24 +116,44 @@ function makeTgBotClient(config) {
|
|
|
176
116
|
method,
|
|
177
117
|
input
|
|
178
118
|
});
|
|
119
|
+
const getFile = async (params) => {
|
|
120
|
+
const response = await execute("get_file", { file_id: params.fileId });
|
|
121
|
+
if (!response.ok) return response;
|
|
122
|
+
const file_path = response.data.file_path;
|
|
123
|
+
if (!file_path || file_path.length === 0) {
|
|
124
|
+
return {
|
|
125
|
+
ok: false,
|
|
126
|
+
error: { _tag: "UnableToGetFile", cause: "File path not defined" }
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const file_name = file_path.replaceAll("/", "-");
|
|
130
|
+
const url = `${tgConfig.base_url}/file/bot${tgConfig.bot_token}/${file_path}`;
|
|
131
|
+
let content;
|
|
132
|
+
try {
|
|
133
|
+
content = await fetch(url).then((_) => _.arrayBuffer());
|
|
134
|
+
} catch (cause) {
|
|
135
|
+
return { ok: false, error: { _tag: "UnableToGetFile", cause } };
|
|
136
|
+
}
|
|
137
|
+
const base64String = () => Buffer.from(content).toString("base64");
|
|
138
|
+
const file = () => new File([content], file_name, {
|
|
139
|
+
...params.type ? { type: params.type } : {}
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
ok: true,
|
|
143
|
+
data: { content, file_name, base64String, file }
|
|
144
|
+
};
|
|
145
|
+
};
|
|
179
146
|
return {
|
|
180
147
|
config: tgConfig,
|
|
181
148
|
execute,
|
|
182
|
-
getFile
|
|
149
|
+
getFile
|
|
183
150
|
};
|
|
184
151
|
}
|
|
185
152
|
// Annotate the CommonJS export names for ESM import in node:
|
|
186
153
|
0 && (module.exports = {
|
|
187
154
|
MESSAGE_EFFECTS,
|
|
188
|
-
TG_BOT_API_URL,
|
|
189
|
-
TgBotClientError,
|
|
190
155
|
executeTgBotMethod,
|
|
191
|
-
getFile,
|
|
192
|
-
isFileContent,
|
|
193
|
-
isMessageEffect,
|
|
194
|
-
isTgBotApiResponse,
|
|
195
156
|
makePayload,
|
|
196
157
|
makeTgBotClient,
|
|
197
|
-
messageEffectIdCodes
|
|
198
|
-
snakeToCamel
|
|
158
|
+
messageEffectIdCodes
|
|
199
159
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,44 +1,26 @@
|
|
|
1
1
|
import { Api } from '@effect-ak/tg-bot-api';
|
|
2
2
|
|
|
3
|
+
interface TgClientConfig {
|
|
4
|
+
bot_token: string;
|
|
5
|
+
base_url?: string;
|
|
6
|
+
}
|
|
3
7
|
interface TgFile {
|
|
4
8
|
readonly content: ArrayBuffer;
|
|
5
9
|
readonly file_name: string;
|
|
6
10
|
readonly base64String: () => string;
|
|
7
11
|
readonly file: () => File;
|
|
8
12
|
}
|
|
9
|
-
declare const getFile: (params: {
|
|
10
|
-
fileId: string;
|
|
11
|
-
config: {
|
|
12
|
-
bot_token: string;
|
|
13
|
-
base_url: string;
|
|
14
|
-
};
|
|
15
|
-
type?: string;
|
|
16
|
-
execute: ExecuteMethod;
|
|
17
|
-
}) => Promise<TgFile>;
|
|
18
|
-
|
|
19
13
|
interface TgBotClient {
|
|
20
14
|
readonly config: Required<TgClientConfig>;
|
|
21
15
|
readonly execute: ExecuteMethod;
|
|
22
16
|
readonly getFile: (input: {
|
|
23
17
|
fileId: string;
|
|
24
18
|
type?: string;
|
|
25
|
-
}) => Promise<TgFile
|
|
26
|
-
}
|
|
27
|
-
interface TgClientConfig {
|
|
28
|
-
bot_token: string;
|
|
29
|
-
base_url?: string;
|
|
19
|
+
}) => Promise<ClientResult<TgFile>>;
|
|
30
20
|
}
|
|
31
21
|
declare function makeTgBotClient(config: TgClientConfig): TgBotClient;
|
|
32
22
|
|
|
33
|
-
type
|
|
34
|
-
declare function executeTgBotMethod<M extends keyof Api>(params: {
|
|
35
|
-
config: Required<TgClientConfig>;
|
|
36
|
-
method: M;
|
|
37
|
-
input: Parameters<Api[M]>[0];
|
|
38
|
-
}): Promise<ReturnType<Api[M]>>;
|
|
39
|
-
declare const makePayload: (body: object) => FormData | undefined;
|
|
40
|
-
|
|
41
|
-
type ErrorReason = {
|
|
23
|
+
type ClientErrorReason = {
|
|
42
24
|
_tag: "NotOkResponse";
|
|
43
25
|
errorCode?: number;
|
|
44
26
|
details?: string;
|
|
@@ -58,28 +40,17 @@ type ErrorReason = {
|
|
|
58
40
|
_tag: "NotJsonResponse";
|
|
59
41
|
response: unknown;
|
|
60
42
|
};
|
|
61
|
-
|
|
62
|
-
readonly
|
|
63
|
-
readonly
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
43
|
+
type ClientResult<T> = {
|
|
44
|
+
readonly ok: true;
|
|
45
|
+
readonly data: T;
|
|
46
|
+
} | {
|
|
47
|
+
readonly ok: false;
|
|
48
|
+
readonly error: ClientErrorReason;
|
|
49
|
+
};
|
|
69
50
|
interface FileContent {
|
|
70
51
|
file_content: Uint8Array<ArrayBuffer>;
|
|
71
52
|
file_name: string;
|
|
72
53
|
}
|
|
73
|
-
declare const isFileContent: (input: unknown) => input is FileContent;
|
|
74
|
-
interface TgBotApiResponseSchema {
|
|
75
|
-
ok: boolean;
|
|
76
|
-
error_code?: number;
|
|
77
|
-
description?: string;
|
|
78
|
-
result?: unknown;
|
|
79
|
-
}
|
|
80
|
-
declare const isTgBotApiResponse: (input: unknown) => input is TgBotApiResponseSchema;
|
|
81
|
-
|
|
82
|
-
declare const TG_BOT_API_URL = "https://api.telegram.org";
|
|
83
54
|
declare const MESSAGE_EFFECTS: {
|
|
84
55
|
readonly "\uD83D\uDD25": "5104841245755180586";
|
|
85
56
|
readonly "\uD83D\uDC4D": "5107584321108051014";
|
|
@@ -90,8 +61,17 @@ declare const MESSAGE_EFFECTS: {
|
|
|
90
61
|
};
|
|
91
62
|
type MessageEffect = keyof typeof MESSAGE_EFFECTS;
|
|
92
63
|
declare const messageEffectIdCodes: MessageEffect[];
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
64
|
+
type WithMessageEffect<T> = T extends {
|
|
65
|
+
message_effect_id?: string;
|
|
66
|
+
} ? Omit<T, "message_effect_id"> & {
|
|
67
|
+
message_effect_id?: MessageEffect | (string & {});
|
|
68
|
+
} : T;
|
|
69
|
+
type ExecuteMethod = <M extends keyof Api>(method: M, input: WithMessageEffect<Parameters<Api[M]>[0]>) => Promise<ClientResult<ReturnType<Api[M]>>>;
|
|
70
|
+
declare function executeTgBotMethod<M extends keyof Api>(params: {
|
|
71
|
+
config: Required<TgClientConfig>;
|
|
72
|
+
method: M;
|
|
73
|
+
input: Parameters<Api[M]>[0];
|
|
74
|
+
}): Promise<ClientResult<ReturnType<Api[M]>>>;
|
|
75
|
+
declare const makePayload: (body: object) => FormData | undefined;
|
|
96
76
|
|
|
97
|
-
export { type
|
|
77
|
+
export { type ClientErrorReason, type ClientResult, type ExecuteMethod, type FileContent, MESSAGE_EFFECTS, type MessageEffect, type TgBotClient, type TgClientConfig, type TgFile, executeTgBotMethod, makePayload, makeTgBotClient, messageEffectIdCodes };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,44 +1,26 @@
|
|
|
1
1
|
import { Api } from '@effect-ak/tg-bot-api';
|
|
2
2
|
|
|
3
|
+
interface TgClientConfig {
|
|
4
|
+
bot_token: string;
|
|
5
|
+
base_url?: string;
|
|
6
|
+
}
|
|
3
7
|
interface TgFile {
|
|
4
8
|
readonly content: ArrayBuffer;
|
|
5
9
|
readonly file_name: string;
|
|
6
10
|
readonly base64String: () => string;
|
|
7
11
|
readonly file: () => File;
|
|
8
12
|
}
|
|
9
|
-
declare const getFile: (params: {
|
|
10
|
-
fileId: string;
|
|
11
|
-
config: {
|
|
12
|
-
bot_token: string;
|
|
13
|
-
base_url: string;
|
|
14
|
-
};
|
|
15
|
-
type?: string;
|
|
16
|
-
execute: ExecuteMethod;
|
|
17
|
-
}) => Promise<TgFile>;
|
|
18
|
-
|
|
19
13
|
interface TgBotClient {
|
|
20
14
|
readonly config: Required<TgClientConfig>;
|
|
21
15
|
readonly execute: ExecuteMethod;
|
|
22
16
|
readonly getFile: (input: {
|
|
23
17
|
fileId: string;
|
|
24
18
|
type?: string;
|
|
25
|
-
}) => Promise<TgFile
|
|
26
|
-
}
|
|
27
|
-
interface TgClientConfig {
|
|
28
|
-
bot_token: string;
|
|
29
|
-
base_url?: string;
|
|
19
|
+
}) => Promise<ClientResult<TgFile>>;
|
|
30
20
|
}
|
|
31
21
|
declare function makeTgBotClient(config: TgClientConfig): TgBotClient;
|
|
32
22
|
|
|
33
|
-
type
|
|
34
|
-
declare function executeTgBotMethod<M extends keyof Api>(params: {
|
|
35
|
-
config: Required<TgClientConfig>;
|
|
36
|
-
method: M;
|
|
37
|
-
input: Parameters<Api[M]>[0];
|
|
38
|
-
}): Promise<ReturnType<Api[M]>>;
|
|
39
|
-
declare const makePayload: (body: object) => FormData | undefined;
|
|
40
|
-
|
|
41
|
-
type ErrorReason = {
|
|
23
|
+
type ClientErrorReason = {
|
|
42
24
|
_tag: "NotOkResponse";
|
|
43
25
|
errorCode?: number;
|
|
44
26
|
details?: string;
|
|
@@ -58,28 +40,17 @@ type ErrorReason = {
|
|
|
58
40
|
_tag: "NotJsonResponse";
|
|
59
41
|
response: unknown;
|
|
60
42
|
};
|
|
61
|
-
|
|
62
|
-
readonly
|
|
63
|
-
readonly
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
43
|
+
type ClientResult<T> = {
|
|
44
|
+
readonly ok: true;
|
|
45
|
+
readonly data: T;
|
|
46
|
+
} | {
|
|
47
|
+
readonly ok: false;
|
|
48
|
+
readonly error: ClientErrorReason;
|
|
49
|
+
};
|
|
69
50
|
interface FileContent {
|
|
70
51
|
file_content: Uint8Array<ArrayBuffer>;
|
|
71
52
|
file_name: string;
|
|
72
53
|
}
|
|
73
|
-
declare const isFileContent: (input: unknown) => input is FileContent;
|
|
74
|
-
interface TgBotApiResponseSchema {
|
|
75
|
-
ok: boolean;
|
|
76
|
-
error_code?: number;
|
|
77
|
-
description?: string;
|
|
78
|
-
result?: unknown;
|
|
79
|
-
}
|
|
80
|
-
declare const isTgBotApiResponse: (input: unknown) => input is TgBotApiResponseSchema;
|
|
81
|
-
|
|
82
|
-
declare const TG_BOT_API_URL = "https://api.telegram.org";
|
|
83
54
|
declare const MESSAGE_EFFECTS: {
|
|
84
55
|
readonly "\uD83D\uDD25": "5104841245755180586";
|
|
85
56
|
readonly "\uD83D\uDC4D": "5107584321108051014";
|
|
@@ -90,8 +61,17 @@ declare const MESSAGE_EFFECTS: {
|
|
|
90
61
|
};
|
|
91
62
|
type MessageEffect = keyof typeof MESSAGE_EFFECTS;
|
|
92
63
|
declare const messageEffectIdCodes: MessageEffect[];
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
64
|
+
type WithMessageEffect<T> = T extends {
|
|
65
|
+
message_effect_id?: string;
|
|
66
|
+
} ? Omit<T, "message_effect_id"> & {
|
|
67
|
+
message_effect_id?: MessageEffect | (string & {});
|
|
68
|
+
} : T;
|
|
69
|
+
type ExecuteMethod = <M extends keyof Api>(method: M, input: WithMessageEffect<Parameters<Api[M]>[0]>) => Promise<ClientResult<ReturnType<Api[M]>>>;
|
|
70
|
+
declare function executeTgBotMethod<M extends keyof Api>(params: {
|
|
71
|
+
config: Required<TgClientConfig>;
|
|
72
|
+
method: M;
|
|
73
|
+
input: Parameters<Api[M]>[0];
|
|
74
|
+
}): Promise<ClientResult<ReturnType<Api[M]>>>;
|
|
75
|
+
declare const makePayload: (body: object) => FormData | undefined;
|
|
96
76
|
|
|
97
|
-
export { type
|
|
77
|
+
export { type ClientErrorReason, type ClientResult, type ExecuteMethod, type FileContent, MESSAGE_EFFECTS, type MessageEffect, type TgBotClient, type TgClientConfig, type TgFile, executeTgBotMethod, makePayload, makeTgBotClient, messageEffectIdCodes };
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,29 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
var TgBotClientError = class extends Error {
|
|
3
|
-
_tag = "TgBotClientError";
|
|
4
|
-
cause;
|
|
5
|
-
constructor(options) {
|
|
6
|
-
super(`TgBotClientError: ${options.cause._tag}`);
|
|
7
|
-
this.cause = options.cause;
|
|
8
|
-
this.name = "TgBotClientError";
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
// src/guards.ts
|
|
1
|
+
// src/execute.ts
|
|
13
2
|
var isFileContent = (input) => typeof input == "object" && input != null && "file_content" in input && input.file_content instanceof Uint8Array && "file_name" in input && typeof input.file_name == "string";
|
|
14
3
|
var isTgBotApiResponse = (input) => typeof input == "object" && input != null && "ok" in input && typeof input.ok == "boolean";
|
|
15
|
-
|
|
16
|
-
|
|
4
|
+
var MESSAGE_EFFECTS = {
|
|
5
|
+
"\u{1F525}": "5104841245755180586",
|
|
6
|
+
"\u{1F44D}": "5107584321108051014",
|
|
7
|
+
"\u{1F44E}": "5104858069142078462",
|
|
8
|
+
"\u2764\uFE0F": "5159385139981059251",
|
|
9
|
+
"\u{1F389}": "5046509860389126442",
|
|
10
|
+
"\u{1F4A9}": "5046589136895476101"
|
|
11
|
+
};
|
|
12
|
+
var messageEffectIdCodes = Object.keys(
|
|
13
|
+
MESSAGE_EFFECTS
|
|
14
|
+
);
|
|
15
|
+
var isMessageEffect = (input) => {
|
|
16
|
+
return typeof input === "string" && input in MESSAGE_EFFECTS;
|
|
17
|
+
};
|
|
17
18
|
var snakeToCamel = (str) => {
|
|
18
19
|
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
19
20
|
};
|
|
20
|
-
|
|
21
|
-
// src/execute.ts
|
|
22
21
|
async function executeTgBotMethod(params) {
|
|
23
|
-
const { config, method
|
|
22
|
+
const { config, method } = params;
|
|
23
|
+
let { input } = params;
|
|
24
|
+
if ("message_effect_id" in input && isMessageEffect(input.message_effect_id)) {
|
|
25
|
+
input = { ...input, message_effect_id: MESSAGE_EFFECTS[input.message_effect_id] };
|
|
26
|
+
}
|
|
24
27
|
let httpResponse;
|
|
25
28
|
try {
|
|
26
29
|
httpResponse = await fetch(
|
|
@@ -31,33 +34,28 @@ async function executeTgBotMethod(params) {
|
|
|
31
34
|
}
|
|
32
35
|
);
|
|
33
36
|
} catch (cause) {
|
|
34
|
-
|
|
35
|
-
cause: { _tag: "ClientInternalError", cause }
|
|
36
|
-
});
|
|
37
|
+
return { ok: false, error: { _tag: "ClientInternalError", cause } };
|
|
37
38
|
}
|
|
38
39
|
let response;
|
|
39
40
|
try {
|
|
40
41
|
response = await httpResponse.json();
|
|
41
42
|
} catch {
|
|
42
|
-
|
|
43
|
-
cause: { _tag: "NotJsonResponse", response: httpResponse }
|
|
44
|
-
});
|
|
43
|
+
return { ok: false, error: { _tag: "NotJsonResponse", response: httpResponse } };
|
|
45
44
|
}
|
|
46
45
|
if (!isTgBotApiResponse(response)) {
|
|
47
|
-
|
|
48
|
-
cause: { _tag: "UnexpectedResponse", response }
|
|
49
|
-
});
|
|
46
|
+
return { ok: false, error: { _tag: "UnexpectedResponse", response } };
|
|
50
47
|
}
|
|
51
48
|
if (!httpResponse.ok) {
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
return {
|
|
50
|
+
ok: false,
|
|
51
|
+
error: {
|
|
54
52
|
_tag: "NotOkResponse",
|
|
55
53
|
...response.error_code ? { errorCode: response.error_code } : {},
|
|
56
54
|
...response.description ? { details: response.description } : {}
|
|
57
55
|
}
|
|
58
|
-
}
|
|
56
|
+
};
|
|
59
57
|
}
|
|
60
|
-
return response.result;
|
|
58
|
+
return { ok: true, data: response.result };
|
|
61
59
|
}
|
|
62
60
|
var makePayload = (body) => {
|
|
63
61
|
const entries = Object.entries(body);
|
|
@@ -76,59 +74,8 @@ var makePayload = (body) => {
|
|
|
76
74
|
return result;
|
|
77
75
|
};
|
|
78
76
|
|
|
79
|
-
// src/client-file.ts
|
|
80
|
-
var getFile = async (params) => {
|
|
81
|
-
const { fileId, config, execute } = params;
|
|
82
|
-
const response = await execute("get_file", { file_id: fileId });
|
|
83
|
-
const file_path = response.file_path;
|
|
84
|
-
if (!file_path || file_path.length === 0) {
|
|
85
|
-
throw new TgBotClientError({
|
|
86
|
-
cause: {
|
|
87
|
-
_tag: "UnableToGetFile",
|
|
88
|
-
cause: "File path not defined"
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
const file_name = file_path.replaceAll("/", "-");
|
|
93
|
-
const url = `${config.base_url}/file/bot${config.bot_token}/${file_path}`;
|
|
94
|
-
let content;
|
|
95
|
-
try {
|
|
96
|
-
content = await fetch(url).then((_) => _.arrayBuffer());
|
|
97
|
-
} catch (cause) {
|
|
98
|
-
throw new TgBotClientError({
|
|
99
|
-
cause: { _tag: "UnableToGetFile", cause }
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
const base64String = () => Buffer.from(content).toString("base64");
|
|
103
|
-
const file = () => new File([content], file_name, {
|
|
104
|
-
...params.type ? { type: params.type } : {}
|
|
105
|
-
});
|
|
106
|
-
return {
|
|
107
|
-
content,
|
|
108
|
-
file_name,
|
|
109
|
-
base64String,
|
|
110
|
-
file
|
|
111
|
-
};
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
// src/const.ts
|
|
115
|
-
var TG_BOT_API_URL = "https://api.telegram.org";
|
|
116
|
-
var MESSAGE_EFFECTS = {
|
|
117
|
-
"\u{1F525}": "5104841245755180586",
|
|
118
|
-
"\u{1F44D}": "5107584321108051014",
|
|
119
|
-
"\u{1F44E}": "5104858069142078462",
|
|
120
|
-
"\u2764\uFE0F": "5159385139981059251",
|
|
121
|
-
"\u{1F389}": "5046509860389126442",
|
|
122
|
-
"\u{1F4A9}": "5046589136895476101"
|
|
123
|
-
};
|
|
124
|
-
var messageEffectIdCodes = Object.keys(
|
|
125
|
-
MESSAGE_EFFECTS
|
|
126
|
-
);
|
|
127
|
-
var isMessageEffect = (input) => {
|
|
128
|
-
return typeof input === "string" && input in MESSAGE_EFFECTS;
|
|
129
|
-
};
|
|
130
|
-
|
|
131
77
|
// src/client.ts
|
|
78
|
+
var TG_BOT_API_URL = "https://api.telegram.org";
|
|
132
79
|
function makeTgBotClient(config) {
|
|
133
80
|
const tgConfig = {
|
|
134
81
|
bot_token: config.bot_token,
|
|
@@ -139,23 +86,43 @@ function makeTgBotClient(config) {
|
|
|
139
86
|
method,
|
|
140
87
|
input
|
|
141
88
|
});
|
|
89
|
+
const getFile = async (params) => {
|
|
90
|
+
const response = await execute("get_file", { file_id: params.fileId });
|
|
91
|
+
if (!response.ok) return response;
|
|
92
|
+
const file_path = response.data.file_path;
|
|
93
|
+
if (!file_path || file_path.length === 0) {
|
|
94
|
+
return {
|
|
95
|
+
ok: false,
|
|
96
|
+
error: { _tag: "UnableToGetFile", cause: "File path not defined" }
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const file_name = file_path.replaceAll("/", "-");
|
|
100
|
+
const url = `${tgConfig.base_url}/file/bot${tgConfig.bot_token}/${file_path}`;
|
|
101
|
+
let content;
|
|
102
|
+
try {
|
|
103
|
+
content = await fetch(url).then((_) => _.arrayBuffer());
|
|
104
|
+
} catch (cause) {
|
|
105
|
+
return { ok: false, error: { _tag: "UnableToGetFile", cause } };
|
|
106
|
+
}
|
|
107
|
+
const base64String = () => Buffer.from(content).toString("base64");
|
|
108
|
+
const file = () => new File([content], file_name, {
|
|
109
|
+
...params.type ? { type: params.type } : {}
|
|
110
|
+
});
|
|
111
|
+
return {
|
|
112
|
+
ok: true,
|
|
113
|
+
data: { content, file_name, base64String, file }
|
|
114
|
+
};
|
|
115
|
+
};
|
|
142
116
|
return {
|
|
143
117
|
config: tgConfig,
|
|
144
118
|
execute,
|
|
145
|
-
getFile
|
|
119
|
+
getFile
|
|
146
120
|
};
|
|
147
121
|
}
|
|
148
122
|
export {
|
|
149
123
|
MESSAGE_EFFECTS,
|
|
150
|
-
TG_BOT_API_URL,
|
|
151
|
-
TgBotClientError,
|
|
152
124
|
executeTgBotMethod,
|
|
153
|
-
getFile,
|
|
154
|
-
isFileContent,
|
|
155
|
-
isMessageEffect,
|
|
156
|
-
isTgBotApiResponse,
|
|
157
125
|
makePayload,
|
|
158
126
|
makeTgBotClient,
|
|
159
|
-
messageEffectIdCodes
|
|
160
|
-
snakeToCamel
|
|
127
|
+
messageEffectIdCodes
|
|
161
128
|
};
|