telegram-bot-api-nodejs 1.0.0 โ 1.0.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/index.d.ts +76 -27
- package/index.js +114 -40
- package/index.ts +276 -104
- package/package.json +4 -3
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;
|
|
@@ -105,6 +100,8 @@ export interface Update {
|
|
|
105
100
|
edited_message?: Message;
|
|
106
101
|
channel_post?: Message;
|
|
107
102
|
edited_channel_post?: Message;
|
|
103
|
+
message_reaction?: MessageReactionUpdated;
|
|
104
|
+
message_reaction_count?: MessageReactionCountUpdated;
|
|
108
105
|
inline_query?: InlineQuery;
|
|
109
106
|
chosen_inline_result?: ChosenInlineResult;
|
|
110
107
|
callback_query?: CallbackQuery;
|
|
@@ -118,7 +115,7 @@ export interface WebhookInfo {
|
|
|
118
115
|
last_error_date?: number;
|
|
119
116
|
last_error_message?: string;
|
|
120
117
|
max_connections?: number;
|
|
121
|
-
allowed_updates?:
|
|
118
|
+
allowed_updates?: AllowedUpdates[];
|
|
122
119
|
}
|
|
123
120
|
export interface User {
|
|
124
121
|
id: string;
|
|
@@ -196,6 +193,49 @@ export interface Message {
|
|
|
196
193
|
reply_markup?: InlineKeyboardMarkup;
|
|
197
194
|
sender_chat?: Chat;
|
|
198
195
|
}
|
|
196
|
+
export interface MessageReactionUpdated {
|
|
197
|
+
chat: Chat;
|
|
198
|
+
message_id: number;
|
|
199
|
+
user?: User;
|
|
200
|
+
actor_chat?: Chat;
|
|
201
|
+
date: number;
|
|
202
|
+
old_reaction: ReactionType[];
|
|
203
|
+
new_reaction: ReactionType[];
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* @see https://core.telegram.org/bots/api#reactiontype
|
|
207
|
+
*/
|
|
208
|
+
export type ReactionType = ReactionTypeEmoji | ReactionTypeCustomEmoji;
|
|
209
|
+
/**
|
|
210
|
+
* @see https://core.telegram.org/bots/api#reactiontypeemoji
|
|
211
|
+
*/
|
|
212
|
+
export interface ReactionTypeEmoji {
|
|
213
|
+
type: "emoji";
|
|
214
|
+
emoji: string;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* @see https://core.telegram.org/bots/api#reactiontypecustomemoji
|
|
218
|
+
*/
|
|
219
|
+
export interface ReactionTypeCustomEmoji {
|
|
220
|
+
type: "custom_emoji";
|
|
221
|
+
custom_emoji_id: string;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* @see https://core.telegram.org/bots/api#messagereactioncountupdated
|
|
225
|
+
*/
|
|
226
|
+
export interface MessageReactionCountUpdated {
|
|
227
|
+
chat: Chat;
|
|
228
|
+
message_id: number;
|
|
229
|
+
date: number;
|
|
230
|
+
reactions: ReactionCount[];
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* @see https://core.telegram.org/bots/api#reactioncount
|
|
234
|
+
*/
|
|
235
|
+
export interface ReactionCount {
|
|
236
|
+
type: ReactionType;
|
|
237
|
+
total_count: number;
|
|
238
|
+
}
|
|
199
239
|
export interface MessageEntity {
|
|
200
240
|
type: MessageEntityType;
|
|
201
241
|
offset: number;
|
|
@@ -461,32 +501,37 @@ interface BotCommand {
|
|
|
461
501
|
/**
|
|
462
502
|
* Methods
|
|
463
503
|
*/
|
|
464
|
-
export declare function setWebhook(token: string, body: SetWebHookOptions, signal:
|
|
465
|
-
export declare function deleteWebhook(token: string, signal:
|
|
504
|
+
export declare function setWebhook(token: string, body: SetWebHookOptions, signal: AbortSignal): Promise<Boolean>;
|
|
505
|
+
export declare function deleteWebhook(token: string, signal: AbortSignal): Promise<Boolean>;
|
|
466
506
|
/** @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
|
-
|
|
507
|
+
export declare function getWebhookInfo(token: string, signal: AbortSignal): Promise<WebhookInfo>;
|
|
508
|
+
export declare function getMe(token: string, signal: AbortSignal): Promise<User>;
|
|
509
|
+
export declare function sendMessage(token: string, payload: SendMessageOptions, signal: AbortSignal): Promise<Message>;
|
|
510
|
+
export declare function editMessageText(token: string, payload: EditMessageTextOptions, signal: AbortSignal): Promise<Message>;
|
|
511
|
+
export declare function sendPhoto(token: string, payload: SendPhotoOptions, signal: AbortSignal): Promise<Message>;
|
|
512
|
+
export declare function sendAnimation(token: string, payload: SendAnimationOptions, signal: AbortSignal): Promise<Message>;
|
|
513
|
+
export declare function sendDocument(token: string, payload: SendDocumentOptions, signal: AbortSignal): Promise<Message>;
|
|
514
|
+
export declare function setMessageReaction(token: string, payload: {
|
|
515
|
+
chat_id: string | number;
|
|
516
|
+
message_id: number;
|
|
517
|
+
reaction?: ReactionType[];
|
|
518
|
+
is_big?: boolean;
|
|
519
|
+
}, signal: AbortSignal): Promise<Boolean>;
|
|
520
|
+
export declare function getUserProfilePhotos(token: string, payload: GetUserProfilePhotosOptions, signal: AbortSignal): Promise<UserProfilePhotos>;
|
|
521
|
+
export declare function getFile(token: string, file_id: string, signal: AbortSignal): Promise<File>;
|
|
522
|
+
export declare function answerCallbackQuery(token: string, payload: AnswerCallbackQueryOptions, signal: AbortSignal): Promise<File>;
|
|
523
|
+
export declare function getChat(token: string, payload: GetChatOptions, 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;
|
|
@@ -578,4 +623,8 @@ export declare class TelegramError extends Error {
|
|
|
578
623
|
status: number;
|
|
579
624
|
constructor(message: string, status: number);
|
|
580
625
|
}
|
|
626
|
+
/**
|
|
627
|
+
* Helpers
|
|
628
|
+
*/
|
|
629
|
+
export declare const SUPPORTED_REACTION_EMOJI: string[];
|
|
581
630
|
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,7 +95,7 @@ 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
100
|
export function getFile(token, file_id, signal) {
|
|
106
101
|
return fetch(`https://api.telegram.org/bot${token}/getFile`, {
|
|
@@ -108,7 +103,7 @@ export function getFile(token, file_id, signal) {
|
|
|
108
103
|
body: JSON.stringify({ file_id }),
|
|
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,6 +392,64 @@ export function parseCallbackQuery(v) {
|
|
|
388
392
|
chat_instance: String(v.chat_instance ?? ""),
|
|
389
393
|
};
|
|
390
394
|
}
|
|
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
|
+
}
|
|
391
453
|
function parseResponse(response) {
|
|
392
454
|
return response.text().then(function (text) {
|
|
393
455
|
let data;
|
|
@@ -399,7 +461,7 @@ 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 ?? "");
|
|
@@ -413,3 +475,15 @@ export class TelegramError extends Error {
|
|
|
413
475
|
this.status = status;
|
|
414
476
|
}
|
|
415
477
|
}
|
|
478
|
+
/**
|
|
479
|
+
* Helpers
|
|
480
|
+
*/
|
|
481
|
+
export const SUPPORTED_REACTION_EMOJI = ["๐", "๐", "\u2764\ufe0f" // red heart
|
|
482
|
+
,
|
|
483
|
+
"๐ฅ", "๐ฅฐ", "๐", "๐", "๐ค", "๐คฏ", "๐ฑ", "๐คฌ", "๐ข",
|
|
484
|
+
"๐", "๐คฉ", "๐คฎ", "๐ฉ", "๐", "๐", "๐", "๐คก", "๐ฅฑ", "๐ฅด", "๐", "๐ณ", "โคโ๐ฅ",
|
|
485
|
+
"๐", "๐ญ", "๐ฏ", "๐คฃ", "โก", "๐", "๐", "๐", "๐คจ", "๐", "๐", "๐พ", "๐",
|
|
486
|
+
"๐", "๐", "๐ด", "๐ญ", "๐ค", "๐ป", "๐จโ๐ป", "๐", "๐", "๐", "๐", "๐จ", "๐ค",
|
|
487
|
+
"โ", "๐ค", "๐ซก", "๐
", "๐", "โ", "๐
", "๐คช", "๐ฟ", "๐", "๐", "๐", "๐ฆ",
|
|
488
|
+
"๐", "๐", "๐", "๐", "๐พ", "๐คทโโ", "๐คท", "๐คทโโ", "๐ก"
|
|
489
|
+
];
|
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;
|
|
@@ -275,7 +308,9 @@ export interface Update {
|
|
|
275
308
|
message?: Message;
|
|
276
309
|
edited_message?: Message;
|
|
277
310
|
channel_post?: Message;
|
|
278
|
-
edited_channel_post?: Message
|
|
311
|
+
edited_channel_post?: Message
|
|
312
|
+
message_reaction?: MessageReactionUpdated
|
|
313
|
+
message_reaction_count?: MessageReactionCountUpdated
|
|
279
314
|
inline_query?: InlineQuery;
|
|
280
315
|
chosen_inline_result?: ChosenInlineResult;
|
|
281
316
|
callback_query?: CallbackQuery;
|
|
@@ -290,7 +325,7 @@ export interface WebhookInfo {
|
|
|
290
325
|
last_error_date?: number;
|
|
291
326
|
last_error_message?: string;
|
|
292
327
|
max_connections?: number;
|
|
293
|
-
allowed_updates?:
|
|
328
|
+
allowed_updates?: AllowedUpdates[]
|
|
294
329
|
}
|
|
295
330
|
|
|
296
331
|
export interface User {
|
|
@@ -372,6 +407,56 @@ export interface Message {
|
|
|
372
407
|
sender_chat?: Chat
|
|
373
408
|
}
|
|
374
409
|
|
|
410
|
+
export interface MessageReactionUpdated {
|
|
411
|
+
chat: Chat
|
|
412
|
+
message_id: number
|
|
413
|
+
user?: User
|
|
414
|
+
actor_chat?: Chat
|
|
415
|
+
date: number
|
|
416
|
+
old_reaction: ReactionType[]
|
|
417
|
+
new_reaction: ReactionType[]
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* @see https://core.telegram.org/bots/api#reactiontype
|
|
422
|
+
*/
|
|
423
|
+
export type ReactionType = ReactionTypeEmoji
|
|
424
|
+
| ReactionTypeCustomEmoji
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* @see https://core.telegram.org/bots/api#reactiontypeemoji
|
|
428
|
+
*/
|
|
429
|
+
export interface ReactionTypeEmoji {
|
|
430
|
+
type: "emoji"
|
|
431
|
+
emoji: string
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* @see https://core.telegram.org/bots/api#reactiontypecustomemoji
|
|
436
|
+
*/
|
|
437
|
+
export interface ReactionTypeCustomEmoji {
|
|
438
|
+
type: "custom_emoji"
|
|
439
|
+
custom_emoji_id: string
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* @see https://core.telegram.org/bots/api#messagereactioncountupdated
|
|
444
|
+
*/
|
|
445
|
+
export interface MessageReactionCountUpdated {
|
|
446
|
+
chat: Chat
|
|
447
|
+
message_id: number
|
|
448
|
+
date: number
|
|
449
|
+
reactions: ReactionCount[]
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* @see https://core.telegram.org/bots/api#reactioncount
|
|
454
|
+
*/
|
|
455
|
+
export interface ReactionCount {
|
|
456
|
+
type: ReactionType
|
|
457
|
+
total_count: number
|
|
458
|
+
}
|
|
459
|
+
|
|
375
460
|
export interface MessageEntity {
|
|
376
461
|
type: MessageEntityType;
|
|
377
462
|
offset: number;
|
|
@@ -977,171 +1062,163 @@ interface BotCommand {
|
|
|
977
1062
|
* Methods
|
|
978
1063
|
*/
|
|
979
1064
|
|
|
980
|
-
export function setWebhook(token: string, body: SetWebHookOptions, signal:
|
|
1065
|
+
export function setWebhook(token: string, body: SetWebHookOptions, signal: AbortSignal) {
|
|
981
1066
|
return fetch(`https://api.telegram.org/bot${token}/setWebhook`, {
|
|
982
1067
|
method: "POST",
|
|
983
1068
|
body: JSON.stringify(body),
|
|
984
1069
|
headers,
|
|
985
1070
|
signal
|
|
986
|
-
}).then(parseResponse)
|
|
1071
|
+
}).then(parseResponse<Boolean>)
|
|
987
1072
|
}
|
|
988
1073
|
|
|
989
|
-
export function deleteWebhook(token: string, signal:
|
|
1074
|
+
export function deleteWebhook(token: string, signal: AbortSignal) {
|
|
990
1075
|
return fetch(`https://api.telegram.org/bot${token}/deleteWebhook`, {
|
|
991
1076
|
method: "POST",
|
|
992
1077
|
body: JSON.stringify({}),
|
|
993
1078
|
headers,
|
|
994
1079
|
signal
|
|
995
|
-
}).then(parseResponse)
|
|
1080
|
+
}).then(parseResponse<Boolean>)
|
|
996
1081
|
}
|
|
997
1082
|
|
|
998
1083
|
/** @todo cehck if response is the same as in typings */
|
|
999
|
-
export function getWebhookInfo(token: string, signal:
|
|
1084
|
+
export function getWebhookInfo(token: string, signal: AbortSignal) {
|
|
1000
1085
|
return fetch(`https://api.telegram.org/bot${token}/getWebhookInfo`, {
|
|
1001
1086
|
method: "POST",
|
|
1002
1087
|
body: JSON.stringify({}),
|
|
1003
1088
|
headers,
|
|
1004
1089
|
signal
|
|
1005
|
-
}).then(parseResponse)
|
|
1090
|
+
}).then(parseResponse<WebhookInfo>)
|
|
1006
1091
|
}
|
|
1007
1092
|
|
|
1008
|
-
export function getMe(token: string, signal:
|
|
1093
|
+
export function getMe(token: string, signal: AbortSignal) {
|
|
1009
1094
|
return fetch(`https://api.telegram.org/bot${token}/getMe`, {
|
|
1010
1095
|
method: "POST",
|
|
1011
1096
|
body: JSON.stringify({}),
|
|
1012
1097
|
headers,
|
|
1013
1098
|
signal
|
|
1014
|
-
}).then(parseResponse)
|
|
1099
|
+
}).then(parseResponse<User>)
|
|
1015
1100
|
}
|
|
1016
1101
|
|
|
1017
|
-
export function sendMessage(token: string, payload: SendMessageOptions, signal:
|
|
1102
|
+
export function sendMessage(token: string, payload: SendMessageOptions, signal: AbortSignal) {
|
|
1018
1103
|
return fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
|
|
1019
1104
|
method: "POST",
|
|
1020
1105
|
body: JSON.stringify(payload),
|
|
1021
1106
|
headers,
|
|
1022
1107
|
signal
|
|
1023
|
-
}).then(parseResponse)
|
|
1108
|
+
}).then(parseResponse<Message>)
|
|
1024
1109
|
}
|
|
1025
1110
|
|
|
1026
|
-
export function editMessageText(token: string, payload: EditMessageTextOptions, signal:
|
|
1111
|
+
export function editMessageText(token: string, payload: EditMessageTextOptions, signal: AbortSignal) {
|
|
1027
1112
|
return fetch(`https://api.telegram.org/bot${token}/editMessageText`, {
|
|
1028
1113
|
method: "POST",
|
|
1029
1114
|
body: JSON.stringify(payload),
|
|
1030
1115
|
headers,
|
|
1031
1116
|
signal
|
|
1032
|
-
}).then(parseResponse)
|
|
1117
|
+
}).then(parseResponse<Message>)
|
|
1033
1118
|
}
|
|
1034
1119
|
|
|
1035
|
-
export function sendPhoto(token: string, payload: SendPhotoOptions, signal:
|
|
1120
|
+
export function sendPhoto(token: string, payload: SendPhotoOptions, signal: AbortSignal) {
|
|
1036
1121
|
return fetch(`https://api.telegram.org/bot${token}/sendPhoto`, {
|
|
1037
1122
|
method: "POST",
|
|
1038
1123
|
body: JSON.stringify(payload),
|
|
1039
1124
|
headers,
|
|
1040
1125
|
signal
|
|
1041
|
-
}).then(parseResponse)
|
|
1126
|
+
}).then(parseResponse<Message>)
|
|
1042
1127
|
}
|
|
1043
1128
|
|
|
1044
|
-
export function sendAnimation(token: string, payload: SendAnimationOptions, signal:
|
|
1129
|
+
export function sendAnimation(token: string, payload: SendAnimationOptions, signal: AbortSignal) {
|
|
1045
1130
|
return fetch(`https://api.telegram.org/bot${token}/sendAnimation`, {
|
|
1046
1131
|
method: "POST",
|
|
1047
1132
|
body: JSON.stringify(payload),
|
|
1048
1133
|
headers,
|
|
1049
1134
|
signal
|
|
1050
|
-
}).then(parseResponse)
|
|
1135
|
+
}).then(parseResponse<Message>)
|
|
1051
1136
|
}
|
|
1052
1137
|
|
|
1053
|
-
export function sendDocument(token: string, payload: SendDocumentOptions, signal:
|
|
1138
|
+
export function sendDocument(token: string, payload: SendDocumentOptions, signal: AbortSignal) {
|
|
1054
1139
|
return fetch(`https://api.telegram.org/bot${token}/sendDocument`, {
|
|
1055
1140
|
method: "POST",
|
|
1056
1141
|
body: JSON.stringify(payload),
|
|
1057
1142
|
headers,
|
|
1058
1143
|
signal
|
|
1059
|
-
}).then(parseResponse)
|
|
1144
|
+
}).then(parseResponse<Message>)
|
|
1060
1145
|
}
|
|
1061
1146
|
|
|
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`, {
|
|
1147
|
+
export function setMessageReaction(token: string, payload: {
|
|
1148
|
+
chat_id: string | number
|
|
1149
|
+
message_id: number
|
|
1150
|
+
reaction?: ReactionType[]
|
|
1151
|
+
is_big?: boolean
|
|
1152
|
+
}, signal: AbortSignal) {
|
|
1153
|
+
return fetch(`https://api.telegram.org/bot${token}/setMessageReaction`, {
|
|
1078
1154
|
method: "POST",
|
|
1079
|
-
body:
|
|
1155
|
+
body: JSON.stringify(payload),
|
|
1156
|
+
headers,
|
|
1080
1157
|
signal
|
|
1081
|
-
}).then(parseResponse)
|
|
1158
|
+
}).then(parseResponse<Boolean>)
|
|
1082
1159
|
}
|
|
1083
1160
|
|
|
1084
|
-
export function getUserProfilePhotos(token: string, payload: GetUserProfilePhotosOptions, signal:
|
|
1161
|
+
export function getUserProfilePhotos(token: string, payload: GetUserProfilePhotosOptions, signal: AbortSignal) {
|
|
1085
1162
|
return fetch(`https://api.telegram.org/bot${token}/getUserProfilePhotos`, {
|
|
1086
1163
|
method: "POST",
|
|
1087
1164
|
body: JSON.stringify(payload),
|
|
1088
1165
|
headers,
|
|
1089
1166
|
signal
|
|
1090
|
-
}).then(parseResponse)
|
|
1167
|
+
}).then(parseResponse<UserProfilePhotos>)
|
|
1091
1168
|
}
|
|
1092
1169
|
|
|
1093
|
-
export function getFile(token: string, file_id: string, signal:
|
|
1170
|
+
export function getFile(token: string, file_id: string, signal: AbortSignal) {
|
|
1094
1171
|
return fetch(`https://api.telegram.org/bot${token}/getFile`, {
|
|
1095
1172
|
method: "POST",
|
|
1096
1173
|
body: JSON.stringify({ file_id }),
|
|
1097
1174
|
headers,
|
|
1098
1175
|
signal
|
|
1099
|
-
}).then(parseResponse)
|
|
1176
|
+
}).then(parseResponse<File>)
|
|
1100
1177
|
}
|
|
1101
1178
|
|
|
1102
|
-
export function answerCallbackQuery(token: string, payload: AnswerCallbackQueryOptions, signal:
|
|
1179
|
+
export function answerCallbackQuery(token: string, payload: AnswerCallbackQueryOptions, signal: AbortSignal) {
|
|
1103
1180
|
return fetch(`https://api.telegram.org/bot${token}/answerCallbackQuery`, {
|
|
1104
1181
|
method: "POST",
|
|
1105
1182
|
body: JSON.stringify(payload),
|
|
1106
1183
|
headers,
|
|
1107
1184
|
signal
|
|
1108
|
-
}).then(parseResponse)
|
|
1185
|
+
}).then(parseResponse<File>)
|
|
1109
1186
|
}
|
|
1110
1187
|
|
|
1111
|
-
export function getChat(token: string, payload: GetChatOptions, signal:
|
|
1188
|
+
export function getChat(token: string, payload: GetChatOptions, signal: AbortSignal) {
|
|
1112
1189
|
return fetch(`https://api.telegram.org/bot${token}/getChat`, {
|
|
1113
1190
|
method: "POST",
|
|
1114
1191
|
body: JSON.stringify(payload),
|
|
1115
1192
|
headers,
|
|
1116
1193
|
signal
|
|
1117
|
-
}).then(parseResponse)
|
|
1194
|
+
}).then(parseResponse<Chat>)
|
|
1118
1195
|
}
|
|
1119
1196
|
|
|
1120
|
-
export function getChatMember(token: string, payload: { chat_id: string, user_id: string }, signal
|
|
1197
|
+
export function getChatMember(token: string, payload: { chat_id: string, user_id: string }, signal?: AbortSignal) {
|
|
1121
1198
|
return fetch(`https://api.telegram.org/bot${token}/getChatMember`, {
|
|
1122
1199
|
method: "POST",
|
|
1123
1200
|
body: JSON.stringify(payload),
|
|
1124
1201
|
headers,
|
|
1125
1202
|
signal: signal
|
|
1126
|
-
}).then(parseResponse)
|
|
1203
|
+
}).then(parseResponse<ChatMember>)
|
|
1127
1204
|
}
|
|
1128
1205
|
|
|
1129
|
-
export function setMyCommands(token: string, payload: SetMyCommandsOptions, signal
|
|
1206
|
+
export function setMyCommands(token: string, payload: SetMyCommandsOptions, signal?: AbortSignal) {
|
|
1130
1207
|
return fetch(`https://api.telegram.org/bot${token}/setMyCommands`, {
|
|
1131
1208
|
method: "POST",
|
|
1132
1209
|
body: JSON.stringify(payload),
|
|
1133
1210
|
headers,
|
|
1134
1211
|
signal
|
|
1135
|
-
}).then(parseResponse)
|
|
1212
|
+
}).then(parseResponse<Chat>)
|
|
1136
1213
|
}
|
|
1137
1214
|
|
|
1138
|
-
export function deleteMyCommands(token: string, payload: DeleteMyCommandsOptions, signal:
|
|
1215
|
+
export function deleteMyCommands(token: string, payload: DeleteMyCommandsOptions, signal: AbortSignal): Promise<Chat> {
|
|
1139
1216
|
return fetch(`https://api.telegram.org/bot${token}/deleteMyCommands`, {
|
|
1140
1217
|
method: "POST",
|
|
1141
1218
|
body: JSON.stringify(payload),
|
|
1142
1219
|
headers,
|
|
1143
1220
|
signal
|
|
1144
|
-
}).then(parseResponse)
|
|
1221
|
+
}).then(parseResponse<Chat>)
|
|
1145
1222
|
}
|
|
1146
1223
|
|
|
1147
1224
|
/**
|
|
@@ -1149,21 +1226,34 @@ export function deleteMyCommands(token: string, payload: DeleteMyCommandsOptions
|
|
|
1149
1226
|
*/
|
|
1150
1227
|
|
|
1151
1228
|
export function parseUpdate(update: any): Update {
|
|
1152
|
-
const
|
|
1229
|
+
const u: Update = {
|
|
1230
|
+
update_id: Number(update.update_id)
|
|
1231
|
+
}
|
|
1153
1232
|
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
message: parseMessage(update.message),
|
|
1157
|
-
edited_message: parseMessage(update.edited_message),
|
|
1158
|
-
callback_query: parseCallbackQuery(update.callback_query),
|
|
1233
|
+
if (update.message) {
|
|
1234
|
+
u.message = parseMessage(update.message)
|
|
1159
1235
|
}
|
|
1160
|
-
}
|
|
1161
1236
|
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
return
|
|
1237
|
+
if (update.edited_message) {
|
|
1238
|
+
u.edited_message = parseMessage(update.edited_message)
|
|
1165
1239
|
}
|
|
1166
1240
|
|
|
1241
|
+
if (update.callback_query) {
|
|
1242
|
+
u.callback_query = parseCallbackQuery(update.callback_query)
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
if (update.message_reaction) {
|
|
1246
|
+
u.message_reaction = parseMessageReaction(update.message_reaction)
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
if (update.message_reaction_count) {
|
|
1250
|
+
u.message_reaction_count = parseMessageReactionCount(update.message_reaction_count)
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
return u
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
export function parseMessage(m: any): Message {
|
|
1167
1257
|
return {
|
|
1168
1258
|
message_id: Number(m.message_id),
|
|
1169
1259
|
from: m.from ? parseUser(m.from) : void 0,
|
|
@@ -1409,6 +1499,76 @@ export function parseCallbackQuery(v?: CallbackQuery): CallbackQuery | undefined
|
|
|
1409
1499
|
}
|
|
1410
1500
|
}
|
|
1411
1501
|
|
|
1502
|
+
function parseMessageReaction(d?: MessageReactionUpdated): MessageReactionUpdated | undefined {
|
|
1503
|
+
if (!d) {
|
|
1504
|
+
return
|
|
1505
|
+
}
|
|
1506
|
+
|
|
1507
|
+
return {
|
|
1508
|
+
chat: parseChat(d.chat),
|
|
1509
|
+
message_id: Number(d.message_id),
|
|
1510
|
+
user: d.user ? parseUser(d.user) : void 0,
|
|
1511
|
+
actor_chat: d.actor_chat ? parseChat(d.actor_chat) : void 0,
|
|
1512
|
+
date: Number(d.date),
|
|
1513
|
+
old_reaction: parseReactions(d.old_reaction),
|
|
1514
|
+
new_reaction: parseReactions(d.new_reaction),
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
function parseReactions(reactions: ReactionType[]): ReactionType[] {
|
|
1519
|
+
if (!Array.isArray(reactions)) {
|
|
1520
|
+
return []
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
return reactions.map(parseReaction)
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
function parseReaction(reaction: ReactionType): ReactionType {
|
|
1527
|
+
if (reaction.type === "emoji") {
|
|
1528
|
+
return {
|
|
1529
|
+
type: "emoji",
|
|
1530
|
+
emoji: String(reaction.emoji),
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
if (reaction.type === "custom_emoji") {
|
|
1535
|
+
return {
|
|
1536
|
+
type: "custom_emoji",
|
|
1537
|
+
custom_emoji_id: String(reaction.custom_emoji_id),
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
throw new RangeError(`Unknown type of reaction: ${JSON.stringify(reaction)}`)
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
function parseMessageReactionCount(d?: MessageReactionCountUpdated): MessageReactionCountUpdated | undefined {
|
|
1545
|
+
if (!d) {
|
|
1546
|
+
return d
|
|
1547
|
+
}
|
|
1548
|
+
|
|
1549
|
+
return {
|
|
1550
|
+
chat: parseChat(d.chat),
|
|
1551
|
+
message_id: Number(d.message_id),
|
|
1552
|
+
date: Number(d.date),
|
|
1553
|
+
reactions: parseReactionsCount(d.reactions)
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
function parseReactionsCount(reactions?: ReactionCount[]): ReactionCount[] {
|
|
1558
|
+
if (!Array.isArray(reactions)) {
|
|
1559
|
+
return []
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
return reactions.map(parseReactionCount)
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
function parseReactionCount(reaction: ReactionCount): ReactionCount {
|
|
1566
|
+
return {
|
|
1567
|
+
type: parseReaction(reaction.type),
|
|
1568
|
+
total_count: Number(reaction.total_count),
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1412
1572
|
function parseResponse<T>(response: Response) {
|
|
1413
1573
|
return response.text().then(function (text) {
|
|
1414
1574
|
let data: any
|
|
@@ -1423,7 +1583,7 @@ function parseResponse<T>(response: Response) {
|
|
|
1423
1583
|
const ok = Boolean(data.ok)
|
|
1424
1584
|
|
|
1425
1585
|
if (ok) {
|
|
1426
|
-
return data.result
|
|
1586
|
+
return data.result as T
|
|
1427
1587
|
}
|
|
1428
1588
|
|
|
1429
1589
|
const error_code = Number(data.error_code ?? 0)
|
|
@@ -1442,3 +1602,15 @@ export class TelegramError extends Error {
|
|
|
1442
1602
|
this.status = status
|
|
1443
1603
|
}
|
|
1444
1604
|
}
|
|
1605
|
+
|
|
1606
|
+
/**
|
|
1607
|
+
* Helpers
|
|
1608
|
+
*/
|
|
1609
|
+
export const SUPPORTED_REACTION_EMOJI = ["๐", "๐", "\u2764\ufe0f" // red heart
|
|
1610
|
+
, "๐ฅ", "๐ฅฐ", "๐", "๐", "๐ค", "๐คฏ", "๐ฑ", "๐คฌ", "๐ข"
|
|
1611
|
+
, "๐", "๐คฉ", "๐คฎ", "๐ฉ", "๐", "๐", "๐", "๐คก", "๐ฅฑ", "๐ฅด", "๐", "๐ณ", "โคโ๐ฅ"
|
|
1612
|
+
, "๐", "๐ญ", "๐ฏ", "๐คฃ", "โก", "๐", "๐", "๐", "๐คจ", "๐", "๐", "๐พ", "๐"
|
|
1613
|
+
, "๐", "๐", "๐ด", "๐ญ", "๐ค", "๐ป", "๐จโ๐ป", "๐", "๐", "๐", "๐", "๐จ", "๐ค"
|
|
1614
|
+
, "โ", "๐ค", "๐ซก", "๐
", "๐", "โ", "๐
", "๐คช", "๐ฟ", "๐", "๐", "๐", "๐ฆ"
|
|
1615
|
+
, "๐", "๐", "๐", "๐", "๐พ", "๐คทโโ", "๐คท", "๐คทโโ", "๐ก"
|
|
1616
|
+
]
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "telegram-bot-api-nodejs",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
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
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
12
|
},
|