telegram-bot-api-nodejs 1.0.0 โ 1.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.
- package/index.d.ts +88 -37
- package/index.js +124 -47
- package/index.ts +293 -120
- package/package.json +6 -4
package/index.d.ts
CHANGED
|
@@ -5,10 +5,11 @@ export type DocumentMimeType = "application/pdf" | "application/zip";
|
|
|
5
5
|
export type MessageType = "text" | "animation" | "audio" | "channel_chat_created" | "contact" | "delete_chat_photo" | "document" | "game" | "group_chat_created" | "invoice" | "left_chat_member" | "location" | "migrate_from_chat_id" | "migrate_to_chat_id" | "new_chat_members" | "new_chat_photo" | "new_chat_title" | "passport_data" | "photo" | "pinned_message" | "sticker" | "successful_payment" | "supergroup_chat_created" | "video" | "video_note" | "voice";
|
|
6
6
|
type MessageEntityType = "mention" | "hashtag" | "bot_command" | "url" | "email" | "bold" | "italic" | "code" | "pre" | "text_link" | "text_mention";
|
|
7
7
|
type ParseMode = "Markdown" | "MarkdownV2" | "HTML";
|
|
8
|
+
export type AllowedUpdates = keyof (Omit<Update, "update_id">);
|
|
8
9
|
export interface SetWebHookOptions {
|
|
9
|
-
url
|
|
10
|
+
url: string;
|
|
10
11
|
max_connections?: number;
|
|
11
|
-
allowed_updates?:
|
|
12
|
+
allowed_updates?: AllowedUpdates[];
|
|
12
13
|
}
|
|
13
14
|
interface SendBasicOptions {
|
|
14
15
|
chat_id: number | string;
|
|
@@ -37,12 +38,6 @@ interface SendDocumentOptions extends SendBasicOptions {
|
|
|
37
38
|
parse_mode?: ParseMode;
|
|
38
39
|
caption?: string;
|
|
39
40
|
}
|
|
40
|
-
interface SendDocumentStreamOptions extends SendBasicOptions {
|
|
41
|
-
document: Blob;
|
|
42
|
-
documentName: string;
|
|
43
|
-
parse_mode?: ParseMode;
|
|
44
|
-
caption?: string;
|
|
45
|
-
}
|
|
46
41
|
export interface AnswerCallbackQueryOptions {
|
|
47
42
|
callback_query_id: string;
|
|
48
43
|
text?: string;
|
|
@@ -50,9 +45,6 @@ export interface AnswerCallbackQueryOptions {
|
|
|
50
45
|
url?: string;
|
|
51
46
|
cache_time?: number;
|
|
52
47
|
}
|
|
53
|
-
export interface GetChatOptions {
|
|
54
|
-
chat_id: string;
|
|
55
|
-
}
|
|
56
48
|
interface SetMyCommandsOptions {
|
|
57
49
|
commands: BotCommand[];
|
|
58
50
|
}
|
|
@@ -68,11 +60,6 @@ interface EditMessageReplyMarkupOptions {
|
|
|
68
60
|
message_id?: number | string;
|
|
69
61
|
inline_message_id?: string;
|
|
70
62
|
}
|
|
71
|
-
export interface GetUserProfilePhotosOptions {
|
|
72
|
-
user_id: number | string;
|
|
73
|
-
offset?: number;
|
|
74
|
-
limit?: number;
|
|
75
|
-
}
|
|
76
63
|
interface PassportFile {
|
|
77
64
|
file_id: string;
|
|
78
65
|
file_size: number;
|
|
@@ -105,6 +92,8 @@ export interface Update {
|
|
|
105
92
|
edited_message?: Message;
|
|
106
93
|
channel_post?: Message;
|
|
107
94
|
edited_channel_post?: Message;
|
|
95
|
+
message_reaction?: MessageReactionUpdated;
|
|
96
|
+
message_reaction_count?: MessageReactionCountUpdated;
|
|
108
97
|
inline_query?: InlineQuery;
|
|
109
98
|
chosen_inline_result?: ChosenInlineResult;
|
|
110
99
|
callback_query?: CallbackQuery;
|
|
@@ -118,7 +107,7 @@ export interface WebhookInfo {
|
|
|
118
107
|
last_error_date?: number;
|
|
119
108
|
last_error_message?: string;
|
|
120
109
|
max_connections?: number;
|
|
121
|
-
allowed_updates?:
|
|
110
|
+
allowed_updates?: AllowedUpdates[];
|
|
122
111
|
}
|
|
123
112
|
export interface User {
|
|
124
113
|
id: string;
|
|
@@ -196,6 +185,49 @@ export interface Message {
|
|
|
196
185
|
reply_markup?: InlineKeyboardMarkup;
|
|
197
186
|
sender_chat?: Chat;
|
|
198
187
|
}
|
|
188
|
+
export interface MessageReactionUpdated {
|
|
189
|
+
chat: Chat;
|
|
190
|
+
message_id: number;
|
|
191
|
+
user?: User;
|
|
192
|
+
actor_chat?: Chat;
|
|
193
|
+
date: number;
|
|
194
|
+
old_reaction: ReactionType[];
|
|
195
|
+
new_reaction: ReactionType[];
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* @see https://core.telegram.org/bots/api#reactiontype
|
|
199
|
+
*/
|
|
200
|
+
export type ReactionType = ReactionTypeEmoji | ReactionTypeCustomEmoji;
|
|
201
|
+
/**
|
|
202
|
+
* @see https://core.telegram.org/bots/api#reactiontypeemoji
|
|
203
|
+
*/
|
|
204
|
+
export interface ReactionTypeEmoji {
|
|
205
|
+
type: "emoji";
|
|
206
|
+
emoji: string;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* @see https://core.telegram.org/bots/api#reactiontypecustomemoji
|
|
210
|
+
*/
|
|
211
|
+
export interface ReactionTypeCustomEmoji {
|
|
212
|
+
type: "custom_emoji";
|
|
213
|
+
custom_emoji_id: string;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* @see https://core.telegram.org/bots/api#messagereactioncountupdated
|
|
217
|
+
*/
|
|
218
|
+
export interface MessageReactionCountUpdated {
|
|
219
|
+
chat: Chat;
|
|
220
|
+
message_id: number;
|
|
221
|
+
date: number;
|
|
222
|
+
reactions: ReactionCount[];
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* @see https://core.telegram.org/bots/api#reactioncount
|
|
226
|
+
*/
|
|
227
|
+
export interface ReactionCount {
|
|
228
|
+
type: ReactionType;
|
|
229
|
+
total_count: number;
|
|
230
|
+
}
|
|
199
231
|
export interface MessageEntity {
|
|
200
232
|
type: MessageEntityType;
|
|
201
233
|
offset: number;
|
|
@@ -461,32 +493,45 @@ interface BotCommand {
|
|
|
461
493
|
/**
|
|
462
494
|
* Methods
|
|
463
495
|
*/
|
|
464
|
-
export declare function setWebhook(token: string, body: SetWebHookOptions, signal:
|
|
465
|
-
export declare function deleteWebhook(token: string, signal:
|
|
496
|
+
export declare function setWebhook(token: string, body: SetWebHookOptions, signal: AbortSignal): Promise<Boolean>;
|
|
497
|
+
export declare function deleteWebhook(token: string, signal: AbortSignal): Promise<Boolean>;
|
|
466
498
|
/** @todo cehck if response is the same as in typings */
|
|
467
|
-
export declare function getWebhookInfo(token: string, signal:
|
|
468
|
-
export declare function getMe(token: string, signal:
|
|
469
|
-
export declare function sendMessage(token: string, payload: SendMessageOptions, signal:
|
|
470
|
-
export declare function editMessageText(token: string, payload: EditMessageTextOptions, signal:
|
|
471
|
-
export declare function sendPhoto(token: string, payload: SendPhotoOptions, signal:
|
|
472
|
-
export declare function sendAnimation(token: string, payload: SendAnimationOptions, signal:
|
|
473
|
-
export declare function sendDocument(token: string, payload: SendDocumentOptions, signal:
|
|
474
|
-
export declare function
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
499
|
+
export declare function getWebhookInfo(token: string, signal: AbortSignal): Promise<WebhookInfo>;
|
|
500
|
+
export declare function getMe(token: string, signal: AbortSignal): Promise<User>;
|
|
501
|
+
export declare function sendMessage(token: string, payload: SendMessageOptions, signal: AbortSignal): Promise<Message>;
|
|
502
|
+
export declare function editMessageText(token: string, payload: EditMessageTextOptions, signal: AbortSignal): Promise<Message>;
|
|
503
|
+
export declare function sendPhoto(token: string, payload: SendPhotoOptions, signal: AbortSignal): Promise<Message>;
|
|
504
|
+
export declare function sendAnimation(token: string, payload: SendAnimationOptions, signal: AbortSignal): Promise<Message>;
|
|
505
|
+
export declare function sendDocument(token: string, payload: SendDocumentOptions, signal: AbortSignal): Promise<Message>;
|
|
506
|
+
export declare function setMessageReaction(token: string, payload: {
|
|
507
|
+
chat_id: string | number;
|
|
508
|
+
message_id: number;
|
|
509
|
+
reaction?: ReactionType[];
|
|
510
|
+
is_big?: boolean;
|
|
511
|
+
}, signal: AbortSignal): Promise<Boolean>;
|
|
512
|
+
export declare function getUserProfilePhotos(token: string, payload: {
|
|
513
|
+
user_id: number | string;
|
|
514
|
+
offset?: number;
|
|
515
|
+
limit?: number;
|
|
516
|
+
}, signal: AbortSignal): Promise<UserProfilePhotos>;
|
|
517
|
+
export declare function getFile(token: string, payload: {
|
|
518
|
+
file_id: string;
|
|
519
|
+
}, signal: AbortSignal): Promise<File>;
|
|
520
|
+
export declare function answerCallbackQuery(token: string, payload: AnswerCallbackQueryOptions, signal: AbortSignal): Promise<File>;
|
|
521
|
+
export declare function getChat(token: string, payload: {
|
|
522
|
+
chat_id: string;
|
|
523
|
+
}, signal: AbortSignal): Promise<Chat>;
|
|
479
524
|
export declare function getChatMember(token: string, payload: {
|
|
480
525
|
chat_id: string;
|
|
481
526
|
user_id: string;
|
|
482
|
-
}, signal
|
|
483
|
-
export declare function setMyCommands(token: string, payload: SetMyCommandsOptions, signal
|
|
484
|
-
export declare function deleteMyCommands(token: string, payload: DeleteMyCommandsOptions, signal:
|
|
527
|
+
}, signal?: AbortSignal): Promise<ChatMember>;
|
|
528
|
+
export declare function setMyCommands(token: string, payload: SetMyCommandsOptions, signal?: AbortSignal): Promise<Chat>;
|
|
529
|
+
export declare function deleteMyCommands(token: string, payload: DeleteMyCommandsOptions, signal: AbortSignal): Promise<Chat>;
|
|
485
530
|
/**
|
|
486
531
|
* Parsers
|
|
487
532
|
*/
|
|
488
533
|
export declare function parseUpdate(update: any): Update;
|
|
489
|
-
export declare function parseMessage(m
|
|
534
|
+
export declare function parseMessage(m: any): Message;
|
|
490
535
|
export declare function parseUser(u: User): {
|
|
491
536
|
id: string;
|
|
492
537
|
is_bot: boolean;
|
|
@@ -574,8 +619,14 @@ export declare function parsePollOption(v: PollOption): {
|
|
|
574
619
|
* @see https://core.telegram.org/bots/api#callbackquery
|
|
575
620
|
*/
|
|
576
621
|
export declare function parseCallbackQuery(v?: CallbackQuery): CallbackQuery | undefined;
|
|
622
|
+
export declare function parseResponse<T>(response: Response): Promise<T>;
|
|
577
623
|
export declare class TelegramError extends Error {
|
|
578
|
-
|
|
579
|
-
|
|
624
|
+
error_code: number;
|
|
625
|
+
parameters: Record<string, any>;
|
|
626
|
+
constructor(message: string, error_code: number, parameters?: Record<string, any>);
|
|
580
627
|
}
|
|
628
|
+
/**
|
|
629
|
+
* Helpers
|
|
630
|
+
*/
|
|
631
|
+
export declare const SUPPORTED_REACTION_EMOJI: string[];
|
|
581
632
|
export {};
|
package/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common headers for API calls
|
|
3
|
+
*/
|
|
1
4
|
const headers = {
|
|
2
|
-
"Content-Type": "application/json",
|
|
3
5
|
"Accept": "application/json",
|
|
6
|
+
"Content-Type": "application/json",
|
|
4
7
|
};
|
|
5
8
|
/**
|
|
6
9
|
* Methods
|
|
@@ -11,7 +14,7 @@ export function setWebhook(token, body, signal) {
|
|
|
11
14
|
body: JSON.stringify(body),
|
|
12
15
|
headers,
|
|
13
16
|
signal
|
|
14
|
-
}).then(parseResponse);
|
|
17
|
+
}).then((parseResponse));
|
|
15
18
|
}
|
|
16
19
|
export function deleteWebhook(token, signal) {
|
|
17
20
|
return fetch(`https://api.telegram.org/bot${token}/deleteWebhook`, {
|
|
@@ -19,7 +22,7 @@ export function deleteWebhook(token, signal) {
|
|
|
19
22
|
body: JSON.stringify({}),
|
|
20
23
|
headers,
|
|
21
24
|
signal
|
|
22
|
-
}).then(parseResponse);
|
|
25
|
+
}).then((parseResponse));
|
|
23
26
|
}
|
|
24
27
|
/** @todo cehck if response is the same as in typings */
|
|
25
28
|
export function getWebhookInfo(token, signal) {
|
|
@@ -28,7 +31,7 @@ export function getWebhookInfo(token, signal) {
|
|
|
28
31
|
body: JSON.stringify({}),
|
|
29
32
|
headers,
|
|
30
33
|
signal
|
|
31
|
-
}).then(parseResponse);
|
|
34
|
+
}).then((parseResponse));
|
|
32
35
|
}
|
|
33
36
|
export function getMe(token, signal) {
|
|
34
37
|
return fetch(`https://api.telegram.org/bot${token}/getMe`, {
|
|
@@ -36,7 +39,7 @@ export function getMe(token, signal) {
|
|
|
36
39
|
body: JSON.stringify({}),
|
|
37
40
|
headers,
|
|
38
41
|
signal
|
|
39
|
-
}).then(parseResponse);
|
|
42
|
+
}).then((parseResponse));
|
|
40
43
|
}
|
|
41
44
|
export function sendMessage(token, payload, signal) {
|
|
42
45
|
return fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
|
|
@@ -44,7 +47,7 @@ export function sendMessage(token, payload, signal) {
|
|
|
44
47
|
body: JSON.stringify(payload),
|
|
45
48
|
headers,
|
|
46
49
|
signal
|
|
47
|
-
}).then(parseResponse);
|
|
50
|
+
}).then((parseResponse));
|
|
48
51
|
}
|
|
49
52
|
export function editMessageText(token, payload, signal) {
|
|
50
53
|
return fetch(`https://api.telegram.org/bot${token}/editMessageText`, {
|
|
@@ -52,7 +55,7 @@ export function editMessageText(token, payload, signal) {
|
|
|
52
55
|
body: JSON.stringify(payload),
|
|
53
56
|
headers,
|
|
54
57
|
signal
|
|
55
|
-
}).then(parseResponse);
|
|
58
|
+
}).then((parseResponse));
|
|
56
59
|
}
|
|
57
60
|
export function sendPhoto(token, payload, signal) {
|
|
58
61
|
return fetch(`https://api.telegram.org/bot${token}/sendPhoto`, {
|
|
@@ -60,7 +63,7 @@ export function sendPhoto(token, payload, signal) {
|
|
|
60
63
|
body: JSON.stringify(payload),
|
|
61
64
|
headers,
|
|
62
65
|
signal
|
|
63
|
-
}).then(parseResponse);
|
|
66
|
+
}).then((parseResponse));
|
|
64
67
|
}
|
|
65
68
|
export function sendAnimation(token, payload, signal) {
|
|
66
69
|
return fetch(`https://api.telegram.org/bot${token}/sendAnimation`, {
|
|
@@ -68,7 +71,7 @@ export function sendAnimation(token, payload, signal) {
|
|
|
68
71
|
body: JSON.stringify(payload),
|
|
69
72
|
headers,
|
|
70
73
|
signal
|
|
71
|
-
}).then(parseResponse);
|
|
74
|
+
}).then((parseResponse));
|
|
72
75
|
}
|
|
73
76
|
export function sendDocument(token, payload, signal) {
|
|
74
77
|
return fetch(`https://api.telegram.org/bot${token}/sendDocument`, {
|
|
@@ -76,23 +79,15 @@ export function sendDocument(token, payload, signal) {
|
|
|
76
79
|
body: JSON.stringify(payload),
|
|
77
80
|
headers,
|
|
78
81
|
signal
|
|
79
|
-
}).then(parseResponse);
|
|
82
|
+
}).then((parseResponse));
|
|
80
83
|
}
|
|
81
|
-
export function
|
|
82
|
-
|
|
83
|
-
body.append("chat_id", String(payload.chat_id));
|
|
84
|
-
if (payload.caption) {
|
|
85
|
-
body.append("caption", payload.caption);
|
|
86
|
-
}
|
|
87
|
-
body.append("document", payload.document, payload.documentName);
|
|
88
|
-
if (payload.parse_mode) {
|
|
89
|
-
body.append("parse_mode", payload.parse_mode);
|
|
90
|
-
}
|
|
91
|
-
return fetch(`https://api.telegram.org/bot${token}/sendDocument`, {
|
|
84
|
+
export function setMessageReaction(token, payload, signal) {
|
|
85
|
+
return fetch(`https://api.telegram.org/bot${token}/setMessageReaction`, {
|
|
92
86
|
method: "POST",
|
|
93
|
-
body:
|
|
87
|
+
body: JSON.stringify(payload),
|
|
88
|
+
headers,
|
|
94
89
|
signal
|
|
95
|
-
}).then(parseResponse);
|
|
90
|
+
}).then((parseResponse));
|
|
96
91
|
}
|
|
97
92
|
export function getUserProfilePhotos(token, payload, signal) {
|
|
98
93
|
return fetch(`https://api.telegram.org/bot${token}/getUserProfilePhotos`, {
|
|
@@ -100,15 +95,15 @@ export function getUserProfilePhotos(token, payload, signal) {
|
|
|
100
95
|
body: JSON.stringify(payload),
|
|
101
96
|
headers,
|
|
102
97
|
signal
|
|
103
|
-
}).then(parseResponse);
|
|
98
|
+
}).then((parseResponse));
|
|
104
99
|
}
|
|
105
|
-
export function getFile(token,
|
|
100
|
+
export function getFile(token, payload, signal) {
|
|
106
101
|
return fetch(`https://api.telegram.org/bot${token}/getFile`, {
|
|
107
102
|
method: "POST",
|
|
108
|
-
body: JSON.stringify(
|
|
103
|
+
body: JSON.stringify(payload),
|
|
109
104
|
headers,
|
|
110
105
|
signal
|
|
111
|
-
}).then(parseResponse);
|
|
106
|
+
}).then((parseResponse));
|
|
112
107
|
}
|
|
113
108
|
export function answerCallbackQuery(token, payload, signal) {
|
|
114
109
|
return fetch(`https://api.telegram.org/bot${token}/answerCallbackQuery`, {
|
|
@@ -116,7 +111,7 @@ export function answerCallbackQuery(token, payload, signal) {
|
|
|
116
111
|
body: JSON.stringify(payload),
|
|
117
112
|
headers,
|
|
118
113
|
signal
|
|
119
|
-
}).then(parseResponse);
|
|
114
|
+
}).then((parseResponse));
|
|
120
115
|
}
|
|
121
116
|
export function getChat(token, payload, signal) {
|
|
122
117
|
return fetch(`https://api.telegram.org/bot${token}/getChat`, {
|
|
@@ -124,7 +119,7 @@ export function getChat(token, payload, signal) {
|
|
|
124
119
|
body: JSON.stringify(payload),
|
|
125
120
|
headers,
|
|
126
121
|
signal
|
|
127
|
-
}).then(parseResponse);
|
|
122
|
+
}).then((parseResponse));
|
|
128
123
|
}
|
|
129
124
|
export function getChatMember(token, payload, signal) {
|
|
130
125
|
return fetch(`https://api.telegram.org/bot${token}/getChatMember`, {
|
|
@@ -132,7 +127,7 @@ export function getChatMember(token, payload, signal) {
|
|
|
132
127
|
body: JSON.stringify(payload),
|
|
133
128
|
headers,
|
|
134
129
|
signal: signal
|
|
135
|
-
}).then(parseResponse);
|
|
130
|
+
}).then((parseResponse));
|
|
136
131
|
}
|
|
137
132
|
export function setMyCommands(token, payload, signal) {
|
|
138
133
|
return fetch(`https://api.telegram.org/bot${token}/setMyCommands`, {
|
|
@@ -140,7 +135,7 @@ export function setMyCommands(token, payload, signal) {
|
|
|
140
135
|
body: JSON.stringify(payload),
|
|
141
136
|
headers,
|
|
142
137
|
signal
|
|
143
|
-
}).then(parseResponse);
|
|
138
|
+
}).then((parseResponse));
|
|
144
139
|
}
|
|
145
140
|
export function deleteMyCommands(token, payload, signal) {
|
|
146
141
|
return fetch(`https://api.telegram.org/bot${token}/deleteMyCommands`, {
|
|
@@ -148,24 +143,33 @@ export function deleteMyCommands(token, payload, signal) {
|
|
|
148
143
|
body: JSON.stringify(payload),
|
|
149
144
|
headers,
|
|
150
145
|
signal
|
|
151
|
-
}).then(parseResponse);
|
|
146
|
+
}).then((parseResponse));
|
|
152
147
|
}
|
|
153
148
|
/**
|
|
154
149
|
* Parsers
|
|
155
150
|
*/
|
|
156
151
|
export function parseUpdate(update) {
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
update_id,
|
|
160
|
-
message: parseMessage(update.message),
|
|
161
|
-
edited_message: parseMessage(update.edited_message),
|
|
162
|
-
callback_query: parseCallbackQuery(update.callback_query),
|
|
152
|
+
const u = {
|
|
153
|
+
update_id: Number(update.update_id)
|
|
163
154
|
};
|
|
155
|
+
if (update.message) {
|
|
156
|
+
u.message = parseMessage(update.message);
|
|
157
|
+
}
|
|
158
|
+
if (update.edited_message) {
|
|
159
|
+
u.edited_message = parseMessage(update.edited_message);
|
|
160
|
+
}
|
|
161
|
+
if (update.callback_query) {
|
|
162
|
+
u.callback_query = parseCallbackQuery(update.callback_query);
|
|
163
|
+
}
|
|
164
|
+
if (update.message_reaction) {
|
|
165
|
+
u.message_reaction = parseMessageReaction(update.message_reaction);
|
|
166
|
+
}
|
|
167
|
+
if (update.message_reaction_count) {
|
|
168
|
+
u.message_reaction_count = parseMessageReactionCount(update.message_reaction_count);
|
|
169
|
+
}
|
|
170
|
+
return u;
|
|
164
171
|
}
|
|
165
172
|
export function parseMessage(m) {
|
|
166
|
-
if (!m) {
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
173
|
return {
|
|
170
174
|
message_id: Number(m.message_id),
|
|
171
175
|
from: m.from ? parseUser(m.from) : void 0,
|
|
@@ -388,7 +392,65 @@ export function parseCallbackQuery(v) {
|
|
|
388
392
|
chat_instance: String(v.chat_instance ?? ""),
|
|
389
393
|
};
|
|
390
394
|
}
|
|
391
|
-
function
|
|
395
|
+
function parseMessageReaction(d) {
|
|
396
|
+
if (!d) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
chat: parseChat(d.chat),
|
|
401
|
+
message_id: Number(d.message_id),
|
|
402
|
+
user: d.user ? parseUser(d.user) : void 0,
|
|
403
|
+
actor_chat: d.actor_chat ? parseChat(d.actor_chat) : void 0,
|
|
404
|
+
date: Number(d.date),
|
|
405
|
+
old_reaction: parseReactions(d.old_reaction),
|
|
406
|
+
new_reaction: parseReactions(d.new_reaction),
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function parseReactions(reactions) {
|
|
410
|
+
if (!Array.isArray(reactions)) {
|
|
411
|
+
return [];
|
|
412
|
+
}
|
|
413
|
+
return reactions.map(parseReaction);
|
|
414
|
+
}
|
|
415
|
+
function parseReaction(reaction) {
|
|
416
|
+
if (reaction.type === "emoji") {
|
|
417
|
+
return {
|
|
418
|
+
type: "emoji",
|
|
419
|
+
emoji: String(reaction.emoji),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
if (reaction.type === "custom_emoji") {
|
|
423
|
+
return {
|
|
424
|
+
type: "custom_emoji",
|
|
425
|
+
custom_emoji_id: String(reaction.custom_emoji_id),
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
throw new RangeError(`Unknown type of reaction: ${JSON.stringify(reaction)}`);
|
|
429
|
+
}
|
|
430
|
+
function parseMessageReactionCount(d) {
|
|
431
|
+
if (!d) {
|
|
432
|
+
return d;
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
chat: parseChat(d.chat),
|
|
436
|
+
message_id: Number(d.message_id),
|
|
437
|
+
date: Number(d.date),
|
|
438
|
+
reactions: parseReactionsCount(d.reactions)
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
function parseReactionsCount(reactions) {
|
|
442
|
+
if (!Array.isArray(reactions)) {
|
|
443
|
+
return [];
|
|
444
|
+
}
|
|
445
|
+
return reactions.map(parseReactionCount);
|
|
446
|
+
}
|
|
447
|
+
function parseReactionCount(reaction) {
|
|
448
|
+
return {
|
|
449
|
+
type: parseReaction(reaction.type),
|
|
450
|
+
total_count: Number(reaction.total_count),
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
export function parseResponse(response) {
|
|
392
454
|
return response.text().then(function (text) {
|
|
393
455
|
let data;
|
|
394
456
|
try {
|
|
@@ -399,17 +461,32 @@ function parseResponse(response) {
|
|
|
399
461
|
}
|
|
400
462
|
const ok = Boolean(data.ok);
|
|
401
463
|
if (ok) {
|
|
402
|
-
return data.result
|
|
464
|
+
return data.result;
|
|
403
465
|
}
|
|
404
466
|
const error_code = Number(data.error_code ?? 0);
|
|
405
467
|
const description = String(data.description ?? "");
|
|
406
|
-
|
|
468
|
+
const parameters = data.parameters || {};
|
|
469
|
+
throw new TelegramError(description, error_code, parameters);
|
|
407
470
|
});
|
|
408
471
|
}
|
|
409
472
|
export class TelegramError extends Error {
|
|
410
|
-
|
|
411
|
-
|
|
473
|
+
error_code;
|
|
474
|
+
parameters;
|
|
475
|
+
constructor(message, error_code, parameters = {}) {
|
|
412
476
|
super(message);
|
|
413
|
-
this.
|
|
477
|
+
this.error_code = error_code;
|
|
478
|
+
this.parameters = parameters;
|
|
414
479
|
}
|
|
415
480
|
}
|
|
481
|
+
/**
|
|
482
|
+
* Helpers
|
|
483
|
+
*/
|
|
484
|
+
export const SUPPORTED_REACTION_EMOJI = ["๐", "๐", "\u2764\ufe0f" // red heart
|
|
485
|
+
,
|
|
486
|
+
"๐ฅ", "๐ฅฐ", "๐", "๐", "๐ค", "๐คฏ", "๐ฑ", "๐คฌ", "๐ข",
|
|
487
|
+
"๐", "๐คฉ", "๐คฎ", "๐ฉ", "๐", "๐", "๐", "๐คก", "๐ฅฑ", "๐ฅด", "๐", "๐ณ", "โคโ๐ฅ",
|
|
488
|
+
"๐", "๐ญ", "๐ฏ", "๐คฃ", "โก", "๐", "๐", "๐", "๐คจ", "๐", "๐", "๐พ", "๐",
|
|
489
|
+
"๐", "๐", "๐ด", "๐ญ", "๐ค", "๐ป", "๐จโ๐ป", "๐", "๐", "๐", "๐", "๐จ", "๐ค",
|
|
490
|
+
"โ", "๐ค", "๐ซก", "๐
", "๐", "โ", "๐
", "๐คช", "๐ฟ", "๐", "๐", "๐", "๐ฆ",
|
|
491
|
+
"๐", "๐", "๐", "๐", "๐พ", "๐คทโโ", "๐คท", "๐คทโโ", "๐ก"
|
|
492
|
+
];
|
package/index.ts
CHANGED
|
@@ -1,46 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common headers for API calls
|
|
3
|
+
*/
|
|
1
4
|
const headers = {
|
|
2
|
-
"Content-Type": "application/json",
|
|
3
5
|
"Accept": "application/json",
|
|
6
|
+
"Content-Type": "application/json",
|
|
4
7
|
}
|
|
5
8
|
|
|
6
|
-
export type ChatType = "private"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
export type ChatType = "private"
|
|
10
|
+
| "group"
|
|
11
|
+
| "supergroup"
|
|
12
|
+
| "channel";
|
|
13
|
+
|
|
14
|
+
export type ChatAction = "typing"
|
|
15
|
+
| "upload_photo"
|
|
16
|
+
| "record_video"
|
|
17
|
+
| "upload_video"
|
|
18
|
+
| "record_audio"
|
|
19
|
+
| "upload_audio"
|
|
20
|
+
| "upload_document"
|
|
21
|
+
| "find_location"
|
|
22
|
+
| "record_video_note"
|
|
23
|
+
| "upload_video_note";
|
|
24
|
+
|
|
25
|
+
export type ChatMemberStatus = "creator"
|
|
26
|
+
| "administrator"
|
|
27
|
+
| "member"
|
|
28
|
+
| "restricted"
|
|
29
|
+
| "left"
|
|
30
|
+
| "kicked";
|
|
31
|
+
|
|
9
32
|
export type DocumentMimeType = "application/pdf" | "application/zip";
|
|
33
|
+
|
|
10
34
|
export type MessageType =
|
|
11
|
-
"text"
|
|
12
|
-
"animation"
|
|
13
|
-
"audio"
|
|
14
|
-
"channel_chat_created"
|
|
15
|
-
"contact"
|
|
16
|
-
"delete_chat_photo"
|
|
17
|
-
"document"
|
|
18
|
-
"game"
|
|
19
|
-
"group_chat_created"
|
|
20
|
-
"invoice"
|
|
21
|
-
"left_chat_member"
|
|
22
|
-
"location"
|
|
23
|
-
"migrate_from_chat_id"
|
|
24
|
-
"migrate_to_chat_id"
|
|
25
|
-
"new_chat_members"
|
|
26
|
-
"new_chat_photo"
|
|
27
|
-
"new_chat_title"
|
|
28
|
-
"passport_data"
|
|
29
|
-
"photo"
|
|
30
|
-
"pinned_message"
|
|
31
|
-
"sticker"
|
|
32
|
-
"successful_payment"
|
|
33
|
-
"supergroup_chat_created"
|
|
34
|
-
"video"
|
|
35
|
-
"video_note"
|
|
36
|
-
"voice";
|
|
37
|
-
|
|
38
|
-
type
|
|
35
|
+
| "text"
|
|
36
|
+
| "animation"
|
|
37
|
+
| "audio"
|
|
38
|
+
| "channel_chat_created"
|
|
39
|
+
| "contact"
|
|
40
|
+
| "delete_chat_photo"
|
|
41
|
+
| "document"
|
|
42
|
+
| "game"
|
|
43
|
+
| "group_chat_created"
|
|
44
|
+
| "invoice"
|
|
45
|
+
| "left_chat_member"
|
|
46
|
+
| "location"
|
|
47
|
+
| "migrate_from_chat_id"
|
|
48
|
+
| "migrate_to_chat_id"
|
|
49
|
+
| "new_chat_members"
|
|
50
|
+
| "new_chat_photo"
|
|
51
|
+
| "new_chat_title"
|
|
52
|
+
| "passport_data"
|
|
53
|
+
| "photo"
|
|
54
|
+
| "pinned_message"
|
|
55
|
+
| "sticker"
|
|
56
|
+
| "successful_payment"
|
|
57
|
+
| "supergroup_chat_created"
|
|
58
|
+
| "video"
|
|
59
|
+
| "video_note"
|
|
60
|
+
| "voice";
|
|
61
|
+
|
|
62
|
+
type MessageEntityType = "mention"
|
|
63
|
+
| "hashtag"
|
|
64
|
+
| "bot_command"
|
|
65
|
+
| "url"
|
|
66
|
+
| "email"
|
|
67
|
+
| "bold"
|
|
68
|
+
| "italic"
|
|
69
|
+
| "code"
|
|
70
|
+
| "pre"
|
|
71
|
+
| "text_link"
|
|
72
|
+
| "text_mention"
|
|
73
|
+
|
|
74
|
+
type ParseMode = "Markdown"
|
|
75
|
+
| "MarkdownV2"
|
|
76
|
+
| "HTML";
|
|
77
|
+
|
|
78
|
+
export type AllowedUpdates = keyof (Omit<Update, "update_id">)
|
|
39
79
|
|
|
40
80
|
export interface SetWebHookOptions {
|
|
41
|
-
url
|
|
81
|
+
url: string;
|
|
42
82
|
max_connections?: number;
|
|
43
|
-
allowed_updates?:
|
|
83
|
+
allowed_updates?: AllowedUpdates[]
|
|
44
84
|
}
|
|
45
85
|
|
|
46
86
|
interface SendBasicOptions {
|
|
@@ -95,13 +135,6 @@ interface SendDocumentOptions extends SendBasicOptions {
|
|
|
95
135
|
caption?: string;
|
|
96
136
|
}
|
|
97
137
|
|
|
98
|
-
interface SendDocumentStreamOptions extends SendBasicOptions {
|
|
99
|
-
document: Blob
|
|
100
|
-
documentName: string
|
|
101
|
-
parse_mode?: ParseMode;
|
|
102
|
-
caption?: string;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
138
|
interface SendMediaGroupOptions {
|
|
106
139
|
disable_notification?: boolean;
|
|
107
140
|
reply_to_message_id?: number;
|
|
@@ -184,10 +217,6 @@ export interface AnswerCallbackQueryOptions {
|
|
|
184
217
|
cache_time?: number
|
|
185
218
|
}
|
|
186
219
|
|
|
187
|
-
export interface GetChatOptions {
|
|
188
|
-
chat_id: string
|
|
189
|
-
}
|
|
190
|
-
|
|
191
220
|
interface SetMyCommandsOptions {
|
|
192
221
|
commands: BotCommand[]
|
|
193
222
|
}
|
|
@@ -210,12 +239,6 @@ interface EditMessageReplyMarkupOptions {
|
|
|
210
239
|
inline_message_id?: string;
|
|
211
240
|
}
|
|
212
241
|
|
|
213
|
-
export interface GetUserProfilePhotosOptions {
|
|
214
|
-
user_id: number | string
|
|
215
|
-
offset?: number;
|
|
216
|
-
limit?: number;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
242
|
interface SetGameScoreOptions {
|
|
220
243
|
force?: boolean;
|
|
221
244
|
disable_edit_message?: boolean;
|
|
@@ -275,7 +298,9 @@ export interface Update {
|
|
|
275
298
|
message?: Message;
|
|
276
299
|
edited_message?: Message;
|
|
277
300
|
channel_post?: Message;
|
|
278
|
-
edited_channel_post?: Message
|
|
301
|
+
edited_channel_post?: Message
|
|
302
|
+
message_reaction?: MessageReactionUpdated
|
|
303
|
+
message_reaction_count?: MessageReactionCountUpdated
|
|
279
304
|
inline_query?: InlineQuery;
|
|
280
305
|
chosen_inline_result?: ChosenInlineResult;
|
|
281
306
|
callback_query?: CallbackQuery;
|
|
@@ -290,7 +315,7 @@ export interface WebhookInfo {
|
|
|
290
315
|
last_error_date?: number;
|
|
291
316
|
last_error_message?: string;
|
|
292
317
|
max_connections?: number;
|
|
293
|
-
allowed_updates?:
|
|
318
|
+
allowed_updates?: AllowedUpdates[]
|
|
294
319
|
}
|
|
295
320
|
|
|
296
321
|
export interface User {
|
|
@@ -372,6 +397,56 @@ export interface Message {
|
|
|
372
397
|
sender_chat?: Chat
|
|
373
398
|
}
|
|
374
399
|
|
|
400
|
+
export interface MessageReactionUpdated {
|
|
401
|
+
chat: Chat
|
|
402
|
+
message_id: number
|
|
403
|
+
user?: User
|
|
404
|
+
actor_chat?: Chat
|
|
405
|
+
date: number
|
|
406
|
+
old_reaction: ReactionType[]
|
|
407
|
+
new_reaction: ReactionType[]
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* @see https://core.telegram.org/bots/api#reactiontype
|
|
412
|
+
*/
|
|
413
|
+
export type ReactionType = ReactionTypeEmoji
|
|
414
|
+
| ReactionTypeCustomEmoji
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* @see https://core.telegram.org/bots/api#reactiontypeemoji
|
|
418
|
+
*/
|
|
419
|
+
export interface ReactionTypeEmoji {
|
|
420
|
+
type: "emoji"
|
|
421
|
+
emoji: string
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* @see https://core.telegram.org/bots/api#reactiontypecustomemoji
|
|
426
|
+
*/
|
|
427
|
+
export interface ReactionTypeCustomEmoji {
|
|
428
|
+
type: "custom_emoji"
|
|
429
|
+
custom_emoji_id: string
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* @see https://core.telegram.org/bots/api#messagereactioncountupdated
|
|
434
|
+
*/
|
|
435
|
+
export interface MessageReactionCountUpdated {
|
|
436
|
+
chat: Chat
|
|
437
|
+
message_id: number
|
|
438
|
+
date: number
|
|
439
|
+
reactions: ReactionCount[]
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* @see https://core.telegram.org/bots/api#reactioncount
|
|
444
|
+
*/
|
|
445
|
+
export interface ReactionCount {
|
|
446
|
+
type: ReactionType
|
|
447
|
+
total_count: number
|
|
448
|
+
}
|
|
449
|
+
|
|
375
450
|
export interface MessageEntity {
|
|
376
451
|
type: MessageEntityType;
|
|
377
452
|
offset: number;
|
|
@@ -977,171 +1052,171 @@ interface BotCommand {
|
|
|
977
1052
|
* Methods
|
|
978
1053
|
*/
|
|
979
1054
|
|
|
980
|
-
export function setWebhook(token: string, body: SetWebHookOptions, signal:
|
|
1055
|
+
export function setWebhook(token: string, body: SetWebHookOptions, signal: AbortSignal) {
|
|
981
1056
|
return fetch(`https://api.telegram.org/bot${token}/setWebhook`, {
|
|
982
1057
|
method: "POST",
|
|
983
1058
|
body: JSON.stringify(body),
|
|
984
1059
|
headers,
|
|
985
1060
|
signal
|
|
986
|
-
}).then(parseResponse)
|
|
1061
|
+
}).then(parseResponse<Boolean>)
|
|
987
1062
|
}
|
|
988
1063
|
|
|
989
|
-
export function deleteWebhook(token: string, signal:
|
|
1064
|
+
export function deleteWebhook(token: string, signal: AbortSignal) {
|
|
990
1065
|
return fetch(`https://api.telegram.org/bot${token}/deleteWebhook`, {
|
|
991
1066
|
method: "POST",
|
|
992
1067
|
body: JSON.stringify({}),
|
|
993
1068
|
headers,
|
|
994
1069
|
signal
|
|
995
|
-
}).then(parseResponse)
|
|
1070
|
+
}).then(parseResponse<Boolean>)
|
|
996
1071
|
}
|
|
997
1072
|
|
|
998
1073
|
/** @todo cehck if response is the same as in typings */
|
|
999
|
-
export function getWebhookInfo(token: string, signal:
|
|
1074
|
+
export function getWebhookInfo(token: string, signal: AbortSignal) {
|
|
1000
1075
|
return fetch(`https://api.telegram.org/bot${token}/getWebhookInfo`, {
|
|
1001
1076
|
method: "POST",
|
|
1002
1077
|
body: JSON.stringify({}),
|
|
1003
1078
|
headers,
|
|
1004
1079
|
signal
|
|
1005
|
-
}).then(parseResponse)
|
|
1080
|
+
}).then(parseResponse<WebhookInfo>)
|
|
1006
1081
|
}
|
|
1007
1082
|
|
|
1008
|
-
export function getMe(token: string, signal:
|
|
1083
|
+
export function getMe(token: string, signal: AbortSignal) {
|
|
1009
1084
|
return fetch(`https://api.telegram.org/bot${token}/getMe`, {
|
|
1010
1085
|
method: "POST",
|
|
1011
1086
|
body: JSON.stringify({}),
|
|
1012
1087
|
headers,
|
|
1013
1088
|
signal
|
|
1014
|
-
}).then(parseResponse)
|
|
1089
|
+
}).then(parseResponse<User>)
|
|
1015
1090
|
}
|
|
1016
1091
|
|
|
1017
|
-
export function sendMessage(token: string, payload: SendMessageOptions, signal:
|
|
1092
|
+
export function sendMessage(token: string, payload: SendMessageOptions, signal: AbortSignal) {
|
|
1018
1093
|
return fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
|
|
1019
1094
|
method: "POST",
|
|
1020
1095
|
body: JSON.stringify(payload),
|
|
1021
1096
|
headers,
|
|
1022
1097
|
signal
|
|
1023
|
-
}).then(parseResponse)
|
|
1098
|
+
}).then(parseResponse<Message>)
|
|
1024
1099
|
}
|
|
1025
1100
|
|
|
1026
|
-
export function editMessageText(token: string, payload: EditMessageTextOptions, signal:
|
|
1101
|
+
export function editMessageText(token: string, payload: EditMessageTextOptions, signal: AbortSignal) {
|
|
1027
1102
|
return fetch(`https://api.telegram.org/bot${token}/editMessageText`, {
|
|
1028
1103
|
method: "POST",
|
|
1029
1104
|
body: JSON.stringify(payload),
|
|
1030
1105
|
headers,
|
|
1031
1106
|
signal
|
|
1032
|
-
}).then(parseResponse)
|
|
1107
|
+
}).then(parseResponse<Message>)
|
|
1033
1108
|
}
|
|
1034
1109
|
|
|
1035
|
-
export function sendPhoto(token: string, payload: SendPhotoOptions, signal:
|
|
1110
|
+
export function sendPhoto(token: string, payload: SendPhotoOptions, signal: AbortSignal) {
|
|
1036
1111
|
return fetch(`https://api.telegram.org/bot${token}/sendPhoto`, {
|
|
1037
1112
|
method: "POST",
|
|
1038
1113
|
body: JSON.stringify(payload),
|
|
1039
1114
|
headers,
|
|
1040
1115
|
signal
|
|
1041
|
-
}).then(parseResponse)
|
|
1116
|
+
}).then(parseResponse<Message>)
|
|
1042
1117
|
}
|
|
1043
1118
|
|
|
1044
|
-
export function sendAnimation(token: string, payload: SendAnimationOptions, signal:
|
|
1119
|
+
export function sendAnimation(token: string, payload: SendAnimationOptions, signal: AbortSignal) {
|
|
1045
1120
|
return fetch(`https://api.telegram.org/bot${token}/sendAnimation`, {
|
|
1046
1121
|
method: "POST",
|
|
1047
1122
|
body: JSON.stringify(payload),
|
|
1048
1123
|
headers,
|
|
1049
1124
|
signal
|
|
1050
|
-
}).then(parseResponse)
|
|
1125
|
+
}).then(parseResponse<Message>)
|
|
1051
1126
|
}
|
|
1052
1127
|
|
|
1053
|
-
export function sendDocument(token: string, payload: SendDocumentOptions, signal:
|
|
1128
|
+
export function sendDocument(token: string, payload: SendDocumentOptions, signal: AbortSignal) {
|
|
1054
1129
|
return fetch(`https://api.telegram.org/bot${token}/sendDocument`, {
|
|
1055
1130
|
method: "POST",
|
|
1056
1131
|
body: JSON.stringify(payload),
|
|
1057
1132
|
headers,
|
|
1058
1133
|
signal
|
|
1059
|
-
}).then(parseResponse)
|
|
1134
|
+
}).then(parseResponse<Message>)
|
|
1060
1135
|
}
|
|
1061
1136
|
|
|
1062
|
-
export function
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
}
|
|
1070
|
-
|
|
1071
|
-
body.append("document", payload.document, payload.documentName)
|
|
1072
|
-
|
|
1073
|
-
if (payload.parse_mode) {
|
|
1074
|
-
body.append("parse_mode", payload.parse_mode)
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
|
-
return fetch(`https://api.telegram.org/bot${token}/sendDocument`, {
|
|
1137
|
+
export function setMessageReaction(token: string, payload: {
|
|
1138
|
+
chat_id: string | number
|
|
1139
|
+
message_id: number
|
|
1140
|
+
reaction?: ReactionType[]
|
|
1141
|
+
is_big?: boolean
|
|
1142
|
+
}, signal: AbortSignal) {
|
|
1143
|
+
return fetch(`https://api.telegram.org/bot${token}/setMessageReaction`, {
|
|
1078
1144
|
method: "POST",
|
|
1079
|
-
body:
|
|
1145
|
+
body: JSON.stringify(payload),
|
|
1146
|
+
headers,
|
|
1080
1147
|
signal
|
|
1081
|
-
}).then(parseResponse)
|
|
1148
|
+
}).then(parseResponse<Boolean>)
|
|
1082
1149
|
}
|
|
1083
1150
|
|
|
1084
|
-
export function getUserProfilePhotos(token: string, payload:
|
|
1151
|
+
export function getUserProfilePhotos(token: string, payload: {
|
|
1152
|
+
user_id: number | string
|
|
1153
|
+
offset?: number;
|
|
1154
|
+
limit?: number;
|
|
1155
|
+
}, signal: AbortSignal) {
|
|
1085
1156
|
return fetch(`https://api.telegram.org/bot${token}/getUserProfilePhotos`, {
|
|
1086
1157
|
method: "POST",
|
|
1087
1158
|
body: JSON.stringify(payload),
|
|
1088
1159
|
headers,
|
|
1089
1160
|
signal
|
|
1090
|
-
}).then(parseResponse)
|
|
1161
|
+
}).then(parseResponse<UserProfilePhotos>)
|
|
1091
1162
|
}
|
|
1092
1163
|
|
|
1093
|
-
export function getFile(token: string,
|
|
1164
|
+
export function getFile(token: string, payload: {
|
|
1165
|
+
file_id: string
|
|
1166
|
+
}, signal: AbortSignal) {
|
|
1094
1167
|
return fetch(`https://api.telegram.org/bot${token}/getFile`, {
|
|
1095
1168
|
method: "POST",
|
|
1096
|
-
body: JSON.stringify(
|
|
1169
|
+
body: JSON.stringify(payload),
|
|
1097
1170
|
headers,
|
|
1098
1171
|
signal
|
|
1099
|
-
}).then(parseResponse)
|
|
1172
|
+
}).then(parseResponse<File>)
|
|
1100
1173
|
}
|
|
1101
1174
|
|
|
1102
|
-
export function answerCallbackQuery(token: string, payload: AnswerCallbackQueryOptions, signal:
|
|
1175
|
+
export function answerCallbackQuery(token: string, payload: AnswerCallbackQueryOptions, signal: AbortSignal) {
|
|
1103
1176
|
return fetch(`https://api.telegram.org/bot${token}/answerCallbackQuery`, {
|
|
1104
1177
|
method: "POST",
|
|
1105
1178
|
body: JSON.stringify(payload),
|
|
1106
1179
|
headers,
|
|
1107
1180
|
signal
|
|
1108
|
-
}).then(parseResponse)
|
|
1181
|
+
}).then(parseResponse<File>)
|
|
1109
1182
|
}
|
|
1110
1183
|
|
|
1111
|
-
export function getChat(token: string, payload:
|
|
1184
|
+
export function getChat(token: string, payload: {
|
|
1185
|
+
chat_id: string
|
|
1186
|
+
}, signal: AbortSignal) {
|
|
1112
1187
|
return fetch(`https://api.telegram.org/bot${token}/getChat`, {
|
|
1113
1188
|
method: "POST",
|
|
1114
1189
|
body: JSON.stringify(payload),
|
|
1115
1190
|
headers,
|
|
1116
1191
|
signal
|
|
1117
|
-
}).then(parseResponse)
|
|
1192
|
+
}).then(parseResponse<Chat>)
|
|
1118
1193
|
}
|
|
1119
1194
|
|
|
1120
|
-
export function getChatMember(token: string, payload: { chat_id: string, user_id: string }, signal
|
|
1195
|
+
export function getChatMember(token: string, payload: { chat_id: string, user_id: string }, signal?: AbortSignal) {
|
|
1121
1196
|
return fetch(`https://api.telegram.org/bot${token}/getChatMember`, {
|
|
1122
1197
|
method: "POST",
|
|
1123
1198
|
body: JSON.stringify(payload),
|
|
1124
1199
|
headers,
|
|
1125
1200
|
signal: signal
|
|
1126
|
-
}).then(parseResponse)
|
|
1201
|
+
}).then(parseResponse<ChatMember>)
|
|
1127
1202
|
}
|
|
1128
1203
|
|
|
1129
|
-
export function setMyCommands(token: string, payload: SetMyCommandsOptions, signal
|
|
1204
|
+
export function setMyCommands(token: string, payload: SetMyCommandsOptions, signal?: AbortSignal) {
|
|
1130
1205
|
return fetch(`https://api.telegram.org/bot${token}/setMyCommands`, {
|
|
1131
1206
|
method: "POST",
|
|
1132
1207
|
body: JSON.stringify(payload),
|
|
1133
1208
|
headers,
|
|
1134
1209
|
signal
|
|
1135
|
-
}).then(parseResponse)
|
|
1210
|
+
}).then(parseResponse<Chat>)
|
|
1136
1211
|
}
|
|
1137
1212
|
|
|
1138
|
-
export function deleteMyCommands(token: string, payload: DeleteMyCommandsOptions, signal:
|
|
1213
|
+
export function deleteMyCommands(token: string, payload: DeleteMyCommandsOptions, signal: AbortSignal): Promise<Chat> {
|
|
1139
1214
|
return fetch(`https://api.telegram.org/bot${token}/deleteMyCommands`, {
|
|
1140
1215
|
method: "POST",
|
|
1141
1216
|
body: JSON.stringify(payload),
|
|
1142
1217
|
headers,
|
|
1143
1218
|
signal
|
|
1144
|
-
}).then(parseResponse)
|
|
1219
|
+
}).then(parseResponse<Chat>)
|
|
1145
1220
|
}
|
|
1146
1221
|
|
|
1147
1222
|
/**
|
|
@@ -1149,21 +1224,34 @@ export function deleteMyCommands(token: string, payload: DeleteMyCommandsOptions
|
|
|
1149
1224
|
*/
|
|
1150
1225
|
|
|
1151
1226
|
export function parseUpdate(update: any): Update {
|
|
1152
|
-
const
|
|
1227
|
+
const u: Update = {
|
|
1228
|
+
update_id: Number(update.update_id)
|
|
1229
|
+
}
|
|
1153
1230
|
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
message: parseMessage(update.message),
|
|
1157
|
-
edited_message: parseMessage(update.edited_message),
|
|
1158
|
-
callback_query: parseCallbackQuery(update.callback_query),
|
|
1231
|
+
if (update.message) {
|
|
1232
|
+
u.message = parseMessage(update.message)
|
|
1159
1233
|
}
|
|
1160
|
-
}
|
|
1161
1234
|
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1235
|
+
if (update.edited_message) {
|
|
1236
|
+
u.edited_message = parseMessage(update.edited_message)
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
if (update.callback_query) {
|
|
1240
|
+
u.callback_query = parseCallbackQuery(update.callback_query)
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
if (update.message_reaction) {
|
|
1244
|
+
u.message_reaction = parseMessageReaction(update.message_reaction)
|
|
1165
1245
|
}
|
|
1166
1246
|
|
|
1247
|
+
if (update.message_reaction_count) {
|
|
1248
|
+
u.message_reaction_count = parseMessageReactionCount(update.message_reaction_count)
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
return u
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
export function parseMessage(m: any): Message {
|
|
1167
1255
|
return {
|
|
1168
1256
|
message_id: Number(m.message_id),
|
|
1169
1257
|
from: m.from ? parseUser(m.from) : void 0,
|
|
@@ -1409,7 +1497,77 @@ export function parseCallbackQuery(v?: CallbackQuery): CallbackQuery | undefined
|
|
|
1409
1497
|
}
|
|
1410
1498
|
}
|
|
1411
1499
|
|
|
1412
|
-
function
|
|
1500
|
+
function parseMessageReaction(d?: MessageReactionUpdated): MessageReactionUpdated | undefined {
|
|
1501
|
+
if (!d) {
|
|
1502
|
+
return
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
return {
|
|
1506
|
+
chat: parseChat(d.chat),
|
|
1507
|
+
message_id: Number(d.message_id),
|
|
1508
|
+
user: d.user ? parseUser(d.user) : void 0,
|
|
1509
|
+
actor_chat: d.actor_chat ? parseChat(d.actor_chat) : void 0,
|
|
1510
|
+
date: Number(d.date),
|
|
1511
|
+
old_reaction: parseReactions(d.old_reaction),
|
|
1512
|
+
new_reaction: parseReactions(d.new_reaction),
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
function parseReactions(reactions: ReactionType[]): ReactionType[] {
|
|
1517
|
+
if (!Array.isArray(reactions)) {
|
|
1518
|
+
return []
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
return reactions.map(parseReaction)
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
function parseReaction(reaction: ReactionType): ReactionType {
|
|
1525
|
+
if (reaction.type === "emoji") {
|
|
1526
|
+
return {
|
|
1527
|
+
type: "emoji",
|
|
1528
|
+
emoji: String(reaction.emoji),
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
if (reaction.type === "custom_emoji") {
|
|
1533
|
+
return {
|
|
1534
|
+
type: "custom_emoji",
|
|
1535
|
+
custom_emoji_id: String(reaction.custom_emoji_id),
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
throw new RangeError(`Unknown type of reaction: ${JSON.stringify(reaction)}`)
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
function parseMessageReactionCount(d?: MessageReactionCountUpdated): MessageReactionCountUpdated | undefined {
|
|
1543
|
+
if (!d) {
|
|
1544
|
+
return d
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
return {
|
|
1548
|
+
chat: parseChat(d.chat),
|
|
1549
|
+
message_id: Number(d.message_id),
|
|
1550
|
+
date: Number(d.date),
|
|
1551
|
+
reactions: parseReactionsCount(d.reactions)
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
function parseReactionsCount(reactions?: ReactionCount[]): ReactionCount[] {
|
|
1556
|
+
if (!Array.isArray(reactions)) {
|
|
1557
|
+
return []
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
return reactions.map(parseReactionCount)
|
|
1561
|
+
}
|
|
1562
|
+
|
|
1563
|
+
function parseReactionCount(reaction: ReactionCount): ReactionCount {
|
|
1564
|
+
return {
|
|
1565
|
+
type: parseReaction(reaction.type),
|
|
1566
|
+
total_count: Number(reaction.total_count),
|
|
1567
|
+
}
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
export function parseResponse<T>(response: Response) {
|
|
1413
1571
|
return response.text().then(function (text) {
|
|
1414
1572
|
let data: any
|
|
1415
1573
|
|
|
@@ -1423,22 +1581,37 @@ function parseResponse<T>(response: Response) {
|
|
|
1423
1581
|
const ok = Boolean(data.ok)
|
|
1424
1582
|
|
|
1425
1583
|
if (ok) {
|
|
1426
|
-
return data.result
|
|
1584
|
+
return data.result as T
|
|
1427
1585
|
}
|
|
1428
1586
|
|
|
1429
1587
|
const error_code = Number(data.error_code ?? 0)
|
|
1430
1588
|
const description = String(data.description ?? "")
|
|
1589
|
+
const parameters = data.parameters || {}
|
|
1431
1590
|
|
|
1432
|
-
throw new TelegramError(description,
|
|
1591
|
+
throw new TelegramError(description, error_code, parameters)
|
|
1433
1592
|
})
|
|
1434
1593
|
}
|
|
1435
1594
|
|
|
1436
1595
|
export class TelegramError extends Error {
|
|
1437
|
-
|
|
1596
|
+
error_code: number
|
|
1597
|
+
parameters: Record<string, any>
|
|
1438
1598
|
|
|
1439
|
-
constructor(message: string,
|
|
1599
|
+
constructor(message: string, error_code: number, parameters: Record<string, any> = {}) {
|
|
1440
1600
|
super(message)
|
|
1441
1601
|
|
|
1442
|
-
this.
|
|
1602
|
+
this.error_code = error_code
|
|
1603
|
+
this.parameters = parameters
|
|
1443
1604
|
}
|
|
1444
1605
|
}
|
|
1606
|
+
|
|
1607
|
+
/**
|
|
1608
|
+
* Helpers
|
|
1609
|
+
*/
|
|
1610
|
+
export const SUPPORTED_REACTION_EMOJI = ["๐", "๐", "\u2764\ufe0f" // red heart
|
|
1611
|
+
, "๐ฅ", "๐ฅฐ", "๐", "๐", "๐ค", "๐คฏ", "๐ฑ", "๐คฌ", "๐ข"
|
|
1612
|
+
, "๐", "๐คฉ", "๐คฎ", "๐ฉ", "๐", "๐", "๐", "๐คก", "๐ฅฑ", "๐ฅด", "๐", "๐ณ", "โคโ๐ฅ"
|
|
1613
|
+
, "๐", "๐ญ", "๐ฏ", "๐คฃ", "โก", "๐", "๐", "๐", "๐คจ", "๐", "๐", "๐พ", "๐"
|
|
1614
|
+
, "๐", "๐", "๐ด", "๐ญ", "๐ค", "๐ป", "๐จโ๐ป", "๐", "๐", "๐", "๐", "๐จ", "๐ค"
|
|
1615
|
+
, "โ", "๐ค", "๐ซก", "๐
", "๐", "โ", "๐
", "๐คช", "๐ฟ", "๐", "๐", "๐", "๐ฆ"
|
|
1616
|
+
, "๐", "๐", "๐", "๐", "๐พ", "๐คทโโ", "๐คท", "๐คทโโ", "๐ก"
|
|
1617
|
+
]
|
package/package.json
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "telegram-bot-api-nodejs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Telegram Bot API client for nodejs",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
6
|
+
"module": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
7
8
|
"scripts": {
|
|
8
|
-
"
|
|
9
|
+
"prepublishOnly": "npm run build",
|
|
9
10
|
"build": "tsc",
|
|
10
|
-
"
|
|
11
|
+
"pretest": "npm run build",
|
|
12
|
+
"test": "node --test"
|
|
11
13
|
},
|
|
12
14
|
"author": "",
|
|
13
15
|
"license": "ISC",
|