grammy 1.6.2 → 1.7.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/README.md +5 -3
- package/out/bot.d.ts +37 -9
- package/out/bot.js +133 -147
- package/out/composer.d.ts +2 -2
- package/out/composer.js +2 -18
- package/out/context.d.ts +5 -5
- package/out/context.js +5 -30
- package/out/convenience/frameworks.node.d.ts +6 -5
- package/out/convenience/keyboard.d.ts +1 -1
- package/out/convenience/keyboard.js +2 -12
- package/out/convenience/session.d.ts +2 -2
- package/out/convenience/session.js +2 -12
- package/out/convenience/webhook.d.ts +11 -7
- package/out/core/api.d.ts +9 -9
- package/out/core/api.js +5 -31
- package/out/core/client.d.ts +21 -2
- package/out/core/client.js +78 -94
- package/out/core/error.d.ts +3 -1
- package/out/core/error.js +26 -46
- package/out/filter.d.ts +36 -19
- package/out/mod.d.ts +4 -7
- package/out/mod.js +5 -1
- package/out/platform.node.d.ts +2 -2
- package/out/platform.node.js +6 -26
- package/out/shim.node.d.ts +1 -1
- package/out/shim.node.js +1 -2
- package/package.json +7 -7
|
@@ -212,21 +212,11 @@ class MemorySessionStorage {
|
|
|
212
212
|
* @param timeToLive TTL in milliseconds, default is `Infinity`
|
|
213
213
|
*/
|
|
214
214
|
constructor(timeToLive = Infinity) {
|
|
215
|
-
|
|
216
|
-
enumerable: true,
|
|
217
|
-
configurable: true,
|
|
218
|
-
writable: true,
|
|
219
|
-
value: timeToLive
|
|
220
|
-
});
|
|
215
|
+
this.timeToLive = timeToLive;
|
|
221
216
|
/**
|
|
222
217
|
* Internally used `Map` instance that stores the session data
|
|
223
218
|
*/
|
|
224
|
-
|
|
225
|
-
enumerable: true,
|
|
226
|
-
configurable: true,
|
|
227
|
-
writable: true,
|
|
228
|
-
value: new Map()
|
|
229
|
-
});
|
|
219
|
+
this.storage = new Map();
|
|
230
220
|
}
|
|
231
221
|
read(key) {
|
|
232
222
|
const value = this.storage.get(key);
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Bot } from "../bot.js";
|
|
3
|
-
import { Update } from "../platform.node.js";
|
|
4
|
-
import { Context } from "../context.js";
|
|
2
|
+
import { type Bot } from "../bot.js";
|
|
3
|
+
import { type Update } from "../platform.node.js";
|
|
4
|
+
import { type Context } from "../context.js";
|
|
5
5
|
declare const adapters: {
|
|
6
6
|
callback: FrameworkAdapter;
|
|
7
7
|
http: (req: import("http").IncomingMessage, res: import("http").ServerResponse) => {
|
|
8
8
|
update: Promise<any>;
|
|
9
|
-
end: () =>
|
|
10
|
-
respond: (json: string) =>
|
|
9
|
+
end: () => import("http").ServerResponse;
|
|
10
|
+
respond: (json: string) => import("http").ServerResponse;
|
|
11
11
|
};
|
|
12
12
|
https: (req: import("http").IncomingMessage, res: import("http").ServerResponse) => {
|
|
13
13
|
update: Promise<any>;
|
|
14
|
-
end: () =>
|
|
15
|
-
respond: (json: string) =>
|
|
14
|
+
end: () => import("http").ServerResponse;
|
|
15
|
+
respond: (json: string) => import("http").ServerResponse;
|
|
16
16
|
};
|
|
17
17
|
express: (req: any, res: any) => {
|
|
18
18
|
update: Promise<any>;
|
|
@@ -31,6 +31,10 @@ declare const adapters: {
|
|
|
31
31
|
};
|
|
32
32
|
worktop: (req: any, res: any) => {
|
|
33
33
|
update: Promise<any>;
|
|
34
|
+
/**
|
|
35
|
+
* Middleware for a web framework. Creates a request-response handler for a
|
|
36
|
+
* request. The handler will be used to integrate with the compatible framework.
|
|
37
|
+
*/
|
|
34
38
|
end: () => any;
|
|
35
39
|
respond: (json: string) => any;
|
|
36
40
|
};
|
package/out/core/api.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BotCommand, ChatPermissions, InlineQueryResult, InputFile, InputMedia, InputMediaAudio, InputMediaDocument, InputMediaPhoto, InputMediaVideo, LabeledPrice, PassportElementError } from "../platform.node.js";
|
|
2
|
-
import { ApiClientOptions, Methods, Payload, RawApi, Transformer, TransformerConsumer, WebhookReplyEnvelope } from "./client.js";
|
|
1
|
+
import { type BotCommand, type ChatPermissions, type InlineQueryResult, type InputFile, type InputMedia, type InputMediaAudio, type InputMediaDocument, type InputMediaPhoto, type InputMediaVideo, type LabeledPrice, type PassportElementError } from "../platform.node.js";
|
|
2
|
+
import { type ApiClientOptions, type Methods, type Payload, type RawApi, type Transformer, type TransformerConsumer, type WebhookReplyEnvelope } from "./client.js";
|
|
3
3
|
declare type AlwaysOmittedInOther = "chat_id";
|
|
4
4
|
/**
|
|
5
5
|
* Helper type to derive remaining properties of a given API method call M,
|
|
@@ -589,7 +589,7 @@ export declare class Api<R extends RawApi = RawApi> {
|
|
|
589
589
|
*
|
|
590
590
|
* **Official reference:** https://core.telegram.org/bots/api#setchatdescription
|
|
591
591
|
*/
|
|
592
|
-
setChatDescription(chat_id: number | string, description
|
|
592
|
+
setChatDescription(chat_id: number | string, description?: string, signal?: AbortSignal): Promise<true>;
|
|
593
593
|
/**
|
|
594
594
|
* Use this method to add a message to the list of pinned messages in a chat. If the chat is not a private chat, the bot must be an administrator in the chat for this to work and must have the 'can_pin_messages' administrator right in a supergroup or 'can_edit_messages' administrator right in a channel. Returns True on success.
|
|
595
595
|
*
|
|
@@ -845,7 +845,7 @@ export declare class Api<R extends RawApi = RawApi> {
|
|
|
845
845
|
*/
|
|
846
846
|
deleteMessage(chat_id: number | string, message_id: number, signal?: AbortSignal): Promise<true>;
|
|
847
847
|
/**
|
|
848
|
-
* Use this method to send static .WEBP
|
|
848
|
+
* Use this method to send static .WEBP, animated .TGS, or video .WEBM stickers. On success, the sent Message is returned.
|
|
849
849
|
*
|
|
850
850
|
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
|
|
851
851
|
* @param sticker Sticker to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a .WEBP file from the Internet, or upload a new one using multipart/form-data.
|
|
@@ -875,7 +875,7 @@ export declare class Api<R extends RawApi = RawApi> {
|
|
|
875
875
|
*/
|
|
876
876
|
uploadStickerFile(user_id: number, png_sticker: InputFile, signal?: AbortSignal): Promise<import("@grammyjs/types/manage").File>;
|
|
877
877
|
/**
|
|
878
|
-
* Use this method to create a new sticker set owned by a user. The bot will be able to edit the sticker set thus created. You must use exactly one of the fields png_sticker or
|
|
878
|
+
* Use this method to create a new sticker set owned by a user. The bot will be able to edit the sticker set thus created. You must use exactly one of the fields png_sticker, tgs_sticker, or webm_sticker. Returns True on success.
|
|
879
879
|
*
|
|
880
880
|
* @param user_id User identifier of created sticker set owner
|
|
881
881
|
* @param name Short name of sticker set, to be used in t.me/addstickers/ URLs (e.g., animals). Can contain only english letters, digits and underscores. Must begin with a letter, can't contain consecutive underscores and must end in “_by_<bot username>”. <bot_username> is case insensitive. 1-64 characters.
|
|
@@ -888,7 +888,7 @@ export declare class Api<R extends RawApi = RawApi> {
|
|
|
888
888
|
*/
|
|
889
889
|
createNewStickerSet(user_id: number, name: string, title: string, emojis: string, other?: Other<R, "createNewStickerSet", "user_id" | "name" | "title" | "emojis">, signal?: AbortSignal): Promise<true>;
|
|
890
890
|
/**
|
|
891
|
-
* Use this method to add a new sticker to a set created by the bot. You must use exactly one of the fields png_sticker or
|
|
891
|
+
* Use this method to add a new sticker to a set created by the bot. You must use exactly one of the fields png_sticker, tgs_sticker, or webm_sticker. Animated stickers can be added to animated sticker sets and only to them. Animated sticker sets can have up to 50 stickers. Static sticker sets can have up to 120 stickers. Returns True on success.
|
|
892
892
|
*
|
|
893
893
|
* @param user_id User identifier of sticker set owner
|
|
894
894
|
* @param name Sticker set name
|
|
@@ -919,16 +919,16 @@ export declare class Api<R extends RawApi = RawApi> {
|
|
|
919
919
|
*/
|
|
920
920
|
deleteStickerFromSet(sticker: string, signal?: AbortSignal): Promise<true>;
|
|
921
921
|
/**
|
|
922
|
-
* Use this method to set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets only. Returns True on success.
|
|
922
|
+
* Use this method to set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets only. Video thumbnails can be set only for video sticker sets only. Returns True on success.
|
|
923
923
|
*
|
|
924
924
|
* @param name Sticker set name
|
|
925
925
|
* @param user_id User identifier of the sticker set owner
|
|
926
|
-
* @param thumb A PNG image with the thumbnail, must be up to 128 kilobytes in size and have width and height exactly 100px, or a TGS animation with the thumbnail up to 32 kilobytes in size; see https://core.telegram.org/
|
|
926
|
+
* @param thumb A PNG image with the thumbnail, must be up to 128 kilobytes in size and have width and height exactly 100px, or a TGS animation with the thumbnail up to 32 kilobytes in size; see https://core.telegram.org/stickers#animated-sticker-requirements for animated sticker technical requirements, or a WEBM video with the thumbnail up to 32 kilobytes in size; see https://core.telegram.org/stickers#video-sticker-requirements for video sticker technical requirements. Pass a file_id as a String to send a file that already exists on the Telegram servers, pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. More info on Sending Files ». Animated sticker set thumbnails can't be uploaded via HTTP URL.
|
|
927
927
|
* @param signal Optional `AbortSignal` to cancel the request
|
|
928
928
|
*
|
|
929
929
|
* **Official reference:** https://core.telegram.org/bots/api#setstickersetthumb
|
|
930
930
|
*/
|
|
931
|
-
setStickerSetThumb(name: string, user_id: number, thumb
|
|
931
|
+
setStickerSetThumb(name: string, user_id: number, thumb?: InputFile | string, signal?: AbortSignal): Promise<true>;
|
|
932
932
|
/**
|
|
933
933
|
* Use this method to send answers to an inline query. On success, True is returned.
|
|
934
934
|
* No more than 50 results per query are allowed.
|
package/out/core/api.js
CHANGED
|
@@ -21,32 +21,6 @@ const client_js_1 = require("./client.js");
|
|
|
21
21
|
*/
|
|
22
22
|
class Api {
|
|
23
23
|
constructor(token, config, webhookReplyEnvelope) {
|
|
24
|
-
/**
|
|
25
|
-
* Provides access to all methods of the Telegram Bot API exactly as
|
|
26
|
-
* documented on the website (https://core.telegram.org/bots/api). No
|
|
27
|
-
* arguments are pulled up in the function signature for convenience.
|
|
28
|
-
*
|
|
29
|
-
* If you suppress compiler warnings, this also allows for raw api calls to
|
|
30
|
-
* undocumented methods with arbitrary parameters—use only if you know what
|
|
31
|
-
* you are doing.
|
|
32
|
-
*/
|
|
33
|
-
Object.defineProperty(this, "raw", {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true,
|
|
37
|
-
value: void 0
|
|
38
|
-
});
|
|
39
|
-
/**
|
|
40
|
-
* Configuration object for the API instance, used as a namespace to
|
|
41
|
-
* separate those API operations that are related to grammY from methods of
|
|
42
|
-
* the Telegram Bot API. Contains advanced options!
|
|
43
|
-
*/
|
|
44
|
-
Object.defineProperty(this, "config", {
|
|
45
|
-
enumerable: true,
|
|
46
|
-
configurable: true,
|
|
47
|
-
writable: true,
|
|
48
|
-
value: void 0
|
|
49
|
-
});
|
|
50
24
|
const { raw, use, installedTransformers } = (0, client_js_1.createRawApi)(token, config, webhookReplyEnvelope);
|
|
51
25
|
this.raw = raw;
|
|
52
26
|
this.config = {
|
|
@@ -987,7 +961,7 @@ class Api {
|
|
|
987
961
|
return this.raw.deleteMessage({ chat_id, message_id }, signal);
|
|
988
962
|
}
|
|
989
963
|
/**
|
|
990
|
-
* Use this method to send static .WEBP
|
|
964
|
+
* Use this method to send static .WEBP, animated .TGS, or video .WEBM stickers. On success, the sent Message is returned.
|
|
991
965
|
*
|
|
992
966
|
* @param chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
|
|
993
967
|
* @param sticker Sticker to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a .WEBP file from the Internet, or upload a new one using multipart/form-data.
|
|
@@ -1023,7 +997,7 @@ class Api {
|
|
|
1023
997
|
return this.raw.uploadStickerFile({ user_id, png_sticker }, signal);
|
|
1024
998
|
}
|
|
1025
999
|
/**
|
|
1026
|
-
* Use this method to create a new sticker set owned by a user. The bot will be able to edit the sticker set thus created. You must use exactly one of the fields png_sticker or
|
|
1000
|
+
* Use this method to create a new sticker set owned by a user. The bot will be able to edit the sticker set thus created. You must use exactly one of the fields png_sticker, tgs_sticker, or webm_sticker. Returns True on success.
|
|
1027
1001
|
*
|
|
1028
1002
|
* @param user_id User identifier of created sticker set owner
|
|
1029
1003
|
* @param name Short name of sticker set, to be used in t.me/addstickers/ URLs (e.g., animals). Can contain only english letters, digits and underscores. Must begin with a letter, can't contain consecutive underscores and must end in “_by_<bot username>”. <bot_username> is case insensitive. 1-64 characters.
|
|
@@ -1038,7 +1012,7 @@ class Api {
|
|
|
1038
1012
|
return this.raw.createNewStickerSet({ user_id, name, title, emojis, ...other }, signal);
|
|
1039
1013
|
}
|
|
1040
1014
|
/**
|
|
1041
|
-
* Use this method to add a new sticker to a set created by the bot. You must use exactly one of the fields png_sticker or
|
|
1015
|
+
* Use this method to add a new sticker to a set created by the bot. You must use exactly one of the fields png_sticker, tgs_sticker, or webm_sticker. Animated stickers can be added to animated sticker sets and only to them. Animated sticker sets can have up to 50 stickers. Static sticker sets can have up to 120 stickers. Returns True on success.
|
|
1042
1016
|
*
|
|
1043
1017
|
* @param user_id User identifier of sticker set owner
|
|
1044
1018
|
* @param name Sticker set name
|
|
@@ -1075,11 +1049,11 @@ class Api {
|
|
|
1075
1049
|
return this.raw.deleteStickerFromSet({ sticker }, signal);
|
|
1076
1050
|
}
|
|
1077
1051
|
/**
|
|
1078
|
-
* Use this method to set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets only. Returns True on success.
|
|
1052
|
+
* Use this method to set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets only. Video thumbnails can be set only for video sticker sets only. Returns True on success.
|
|
1079
1053
|
*
|
|
1080
1054
|
* @param name Sticker set name
|
|
1081
1055
|
* @param user_id User identifier of the sticker set owner
|
|
1082
|
-
* @param thumb A PNG image with the thumbnail, must be up to 128 kilobytes in size and have width and height exactly 100px, or a TGS animation with the thumbnail up to 32 kilobytes in size; see https://core.telegram.org/
|
|
1056
|
+
* @param thumb A PNG image with the thumbnail, must be up to 128 kilobytes in size and have width and height exactly 100px, or a TGS animation with the thumbnail up to 32 kilobytes in size; see https://core.telegram.org/stickers#animated-sticker-requirements for animated sticker technical requirements, or a WEBM video with the thumbnail up to 32 kilobytes in size; see https://core.telegram.org/stickers#video-sticker-requirements for video sticker technical requirements. Pass a file_id as a String to send a file that already exists on the Telegram servers, pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. More info on Sending Files ». Animated sticker set thumbnails can't be uploaded via HTTP URL.
|
|
1083
1057
|
* @param signal Optional `AbortSignal` to cancel the request
|
|
1084
1058
|
*
|
|
1085
1059
|
* **Official reference:** https://core.telegram.org/bots/api#setstickersetthumb
|
package/out/core/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node-fetch" />
|
|
2
|
-
import { ApiResponse, Opts, Telegram } from "../platform.node.js";
|
|
2
|
+
import { type ApiResponse, type Opts, type Telegram } from "../platform.node.js";
|
|
3
3
|
export declare type Methods<R extends RawApi> = string & keyof R;
|
|
4
4
|
/**
|
|
5
5
|
* Represents the raw Telegram Bot API with all methods specified 1:1 as
|
|
@@ -74,7 +74,26 @@ export interface ApiClientOptions {
|
|
|
74
74
|
* @param method The API method to be called, e.g. `getMe`
|
|
75
75
|
* @returns The URL that will be fetched during the API call
|
|
76
76
|
*/
|
|
77
|
-
buildUrl?: (root: string, token: string, method: string) =>
|
|
77
|
+
buildUrl?: (root: string, token: string, method: string) => string | URL;
|
|
78
|
+
/**
|
|
79
|
+
* Maximum number of seconds that a request to the Bot API server may take.
|
|
80
|
+
* If a request has not completed before this time has elapsed, grammY
|
|
81
|
+
* aborts the request and errors. Without such a timeout, networking issues
|
|
82
|
+
* may cause your bot to leave open a connection indefinitely, which may
|
|
83
|
+
* effectively make your bot freeze.
|
|
84
|
+
*
|
|
85
|
+
* You probably do not have to care about this option. In rare cases, you
|
|
86
|
+
* may want to adjust it if you are transferring large files via slow
|
|
87
|
+
* connections to your own Bot API server.
|
|
88
|
+
*
|
|
89
|
+
* The default number of seconds is `500`, which corresponds to 8 minutes
|
|
90
|
+
* and 20 seconds. Note that this is also the value that is hard-coded in
|
|
91
|
+
* the official Bot API server, so you cannot perform any successful
|
|
92
|
+
* requests that exceed this time frame (even if you would allow it in
|
|
93
|
+
* grammY). Setting this option to higher than the default only makes sense
|
|
94
|
+
* with a custom Bot API server.
|
|
95
|
+
*/
|
|
96
|
+
timeoutSeconds?: number;
|
|
78
97
|
/**
|
|
79
98
|
* If the bot is running on webhooks, as soon as the bot receives an update
|
|
80
99
|
* from Telegram, it is possible to make up to one API call in the response
|
package/out/core/client.js
CHANGED
|
@@ -11,90 +11,64 @@ function concatTransformer(prev, trans) {
|
|
|
11
11
|
}
|
|
12
12
|
class ApiClient {
|
|
13
13
|
constructor(token, options = {}, webhookReplyEnvelope = {}) {
|
|
14
|
-
var _a, _b, _c, _d;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true,
|
|
37
|
-
value: false
|
|
38
|
-
});
|
|
39
|
-
Object.defineProperty(this, "installedTransformers", {
|
|
40
|
-
enumerable: true,
|
|
41
|
-
configurable: true,
|
|
42
|
-
writable: true,
|
|
43
|
-
value: []
|
|
44
|
-
});
|
|
45
|
-
Object.defineProperty(this, "call", {
|
|
46
|
-
enumerable: true,
|
|
47
|
-
configurable: true,
|
|
48
|
-
writable: true,
|
|
49
|
-
value: async (method, payload, signal) => {
|
|
50
|
-
debug("Calling", method);
|
|
51
|
-
const url = this.options.buildUrl(this.options.apiRoot, this.token, method);
|
|
52
|
-
const formDataRequired = (0, payload_js_1.requiresFormDataUpload)(payload);
|
|
53
|
-
if (this.webhookReplyEnvelope.send !== undefined &&
|
|
54
|
-
!this.hasUsedWebhookReply &&
|
|
55
|
-
!formDataRequired &&
|
|
56
|
-
this.options.canUseWebhookReply(method)) {
|
|
57
|
-
this.hasUsedWebhookReply = true;
|
|
58
|
-
const config = (0, payload_js_1.createJsonPayload)({ ...payload, method });
|
|
59
|
-
await this.webhookReplyEnvelope.send(config.body);
|
|
60
|
-
return { ok: true, result: true };
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
const p = payload !== null && payload !== void 0 ? payload : {};
|
|
64
|
-
const sensLogs = this.options.sensitiveLogs;
|
|
65
|
-
const abortController = new shim_node_js_1.AbortController();
|
|
66
|
-
const abort = combineAborts(abortController, signal);
|
|
67
|
-
const res = await new Promise((resolve, reject) => {
|
|
68
|
-
function onStreamError(err) {
|
|
69
|
-
abort();
|
|
70
|
-
reject(err);
|
|
71
|
-
}
|
|
72
|
-
const onHttpError = toHttpError(method, sensLogs, reject);
|
|
73
|
-
const config = formDataRequired
|
|
74
|
-
? (0, payload_js_1.createFormDataPayload)(p, onStreamError)
|
|
75
|
-
: (0, payload_js_1.createJsonPayload)(p);
|
|
76
|
-
const opts = {
|
|
77
|
-
...this.options.baseFetchConfig,
|
|
78
|
-
signal: abortController.signal,
|
|
79
|
-
...config,
|
|
80
|
-
};
|
|
81
|
-
(0, shim_node_js_1.fetch)(url, opts).then((res) => res.json()).then(resolve)
|
|
82
|
-
.catch(onHttpError);
|
|
83
|
-
});
|
|
84
|
-
return res;
|
|
85
|
-
}
|
|
14
|
+
var _a, _b, _c, _d, _e;
|
|
15
|
+
this.token = token;
|
|
16
|
+
this.webhookReplyEnvelope = webhookReplyEnvelope;
|
|
17
|
+
this.hasUsedWebhookReply = false;
|
|
18
|
+
this.installedTransformers = [];
|
|
19
|
+
this.call = async (method, p, signal) => {
|
|
20
|
+
const payload = p !== null && p !== void 0 ? p : {};
|
|
21
|
+
debug("Calling", method);
|
|
22
|
+
// General config
|
|
23
|
+
const opts = this.options;
|
|
24
|
+
const formDataRequired = (0, payload_js_1.requiresFormDataUpload)(payload);
|
|
25
|
+
// Short-circuit on webhook reply
|
|
26
|
+
if (this.webhookReplyEnvelope.send !== undefined &&
|
|
27
|
+
!this.hasUsedWebhookReply &&
|
|
28
|
+
!formDataRequired &&
|
|
29
|
+
opts.canUseWebhookReply(method)) {
|
|
30
|
+
this.hasUsedWebhookReply = true;
|
|
31
|
+
const config = (0, payload_js_1.createJsonPayload)({ ...payload, method });
|
|
32
|
+
await this.webhookReplyEnvelope.send(config.body);
|
|
33
|
+
return { ok: true, result: true };
|
|
86
34
|
}
|
|
87
|
-
|
|
35
|
+
// Handle timeouts and errors in the underlying form-data stream
|
|
36
|
+
const controller = createAbortControllerFromSignal(signal);
|
|
37
|
+
const timeout = createTimeout(controller, opts.timeoutSeconds, method);
|
|
38
|
+
const streamErr = createStreamError(controller);
|
|
39
|
+
// Build request URL and config
|
|
40
|
+
const url = opts.buildUrl(opts.apiRoot, this.token, method);
|
|
41
|
+
const config = formDataRequired
|
|
42
|
+
? (0, payload_js_1.createFormDataPayload)(payload, (err) => streamErr.catch(err))
|
|
43
|
+
: (0, payload_js_1.createJsonPayload)(payload);
|
|
44
|
+
const sig = controller.signal;
|
|
45
|
+
const options = { ...opts.baseFetchConfig, signal: sig, ...config };
|
|
46
|
+
// Perform fetch call, and handle networking errors
|
|
47
|
+
const successPromise = (0, shim_node_js_1.fetch)(url instanceof URL ? url.href : url, options)
|
|
48
|
+
.catch((0, error_js_1.toHttpError)(method, opts.sensitiveLogs));
|
|
49
|
+
// Those are the three possible outcomes of the fetch call:
|
|
50
|
+
const operations = [successPromise, streamErr.promise, timeout.promise];
|
|
51
|
+
// Wait for result
|
|
52
|
+
try {
|
|
53
|
+
const res = await Promise.race(operations);
|
|
54
|
+
return await res.json();
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
if (timeout.handle !== undefined)
|
|
58
|
+
clearTimeout(timeout.handle);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
88
61
|
const apiRoot = (_a = options.apiRoot) !== null && _a !== void 0 ? _a : "https://api.telegram.org";
|
|
89
62
|
this.options = {
|
|
90
63
|
apiRoot,
|
|
91
64
|
buildUrl: (_b = options.buildUrl) !== null && _b !== void 0 ? _b : ((root, token, method) => `${root}/bot${token}/${method}`),
|
|
65
|
+
timeoutSeconds: (_c = options.timeoutSeconds) !== null && _c !== void 0 ? _c : 500,
|
|
92
66
|
baseFetchConfig: {
|
|
93
67
|
...(0, platform_node_js_1.baseFetchConfig)(apiRoot),
|
|
94
68
|
...options.baseFetchConfig,
|
|
95
69
|
},
|
|
96
|
-
canUseWebhookReply: (
|
|
97
|
-
sensitiveLogs: (
|
|
70
|
+
canUseWebhookReply: (_d = options.canUseWebhookReply) !== null && _d !== void 0 ? _d : (() => false),
|
|
71
|
+
sensitiveLogs: (_e = options.sensitiveLogs) !== null && _e !== void 0 ? _e : false,
|
|
98
72
|
};
|
|
99
73
|
if (this.options.apiRoot.endsWith("/")) {
|
|
100
74
|
throw new Error(`Remove the trailing '/' from the 'apiRoot' option (use '${this.options.apiRoot.substring(0, this.options.apiRoot.length - 1)}' instead of '${this.options.apiRoot}')`);
|
|
@@ -109,9 +83,8 @@ class ApiClient {
|
|
|
109
83
|
const data = await this.call(method, payload, signal);
|
|
110
84
|
if (data.ok)
|
|
111
85
|
return data.result;
|
|
112
|
-
else
|
|
113
|
-
throw
|
|
114
|
-
}
|
|
86
|
+
else
|
|
87
|
+
throw (0, error_js_1.toGrammyError)(data, method, payload);
|
|
115
88
|
}
|
|
116
89
|
}
|
|
117
90
|
/**
|
|
@@ -164,25 +137,36 @@ const proxyMethods = {
|
|
|
164
137
|
return [];
|
|
165
138
|
},
|
|
166
139
|
};
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
140
|
+
/** Creates a timeout error which aborts a given controller */
|
|
141
|
+
function createTimeout(controller, seconds, method) {
|
|
142
|
+
let handle = undefined;
|
|
143
|
+
const promise = new Promise((_, reject) => {
|
|
144
|
+
handle = setTimeout(() => {
|
|
145
|
+
const msg = `Request to '${method}' timed out after ${seconds} seconds`;
|
|
146
|
+
reject(new Error(msg));
|
|
147
|
+
controller.abort();
|
|
148
|
+
}, 1000 * seconds);
|
|
149
|
+
});
|
|
150
|
+
return { promise, handle };
|
|
172
151
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if (sensitiveLogs && err instanceof Error)
|
|
179
|
-
msg += ` ${err.message}`;
|
|
180
|
-
reject(new error_js_1.HttpError(msg, err));
|
|
152
|
+
/** Creates a stream error which abort a given controller */
|
|
153
|
+
function createStreamError(abortController) {
|
|
154
|
+
let onError = (err) => {
|
|
155
|
+
// Re-throw by default, but will be overwritten immediately
|
|
156
|
+
throw err;
|
|
181
157
|
};
|
|
158
|
+
const promise = new Promise((_, reject) => {
|
|
159
|
+
onError = (err) => {
|
|
160
|
+
reject(err);
|
|
161
|
+
abortController.abort();
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
return { promise, catch: onError };
|
|
182
165
|
}
|
|
183
|
-
function
|
|
166
|
+
function createAbortControllerFromSignal(signal) {
|
|
167
|
+
const abortController = new shim_node_js_1.AbortController();
|
|
184
168
|
if (signal === undefined)
|
|
185
|
-
return
|
|
169
|
+
return abortController;
|
|
186
170
|
const sig = signal;
|
|
187
171
|
function abort() {
|
|
188
172
|
abortController.abort();
|
|
@@ -192,6 +176,6 @@ function combineAborts(abortController, signal) {
|
|
|
192
176
|
abort();
|
|
193
177
|
else
|
|
194
178
|
sig.addEventListener("abort", abort);
|
|
195
|
-
return abort;
|
|
179
|
+
return { abort, signal: abortController.signal };
|
|
196
180
|
}
|
|
197
181
|
const shim_node_js_1 = require("../shim.node.js");
|
package/out/core/error.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApiError, ResponseParameters } from "../platform.node.js";
|
|
1
|
+
import { type ApiError, type ResponseParameters } from "../platform.node.js";
|
|
2
2
|
/**
|
|
3
3
|
* This class represents errors that are thrown by grammY because the Telegram
|
|
4
4
|
* Bot API responded with an error.
|
|
@@ -29,6 +29,7 @@ export declare class GrammyError extends Error implements ApiError {
|
|
|
29
29
|
/** The payload that was passed when calling the method. */
|
|
30
30
|
payload: Record<string, unknown>);
|
|
31
31
|
}
|
|
32
|
+
export declare function toGrammyError(err: ApiError, method: string, payload: Record<string, unknown>): GrammyError;
|
|
32
33
|
/**
|
|
33
34
|
* This class represents errors that are thrown by grammY because an HTTP call
|
|
34
35
|
* to the Telegram Bot API failed.
|
|
@@ -49,3 +50,4 @@ export declare class HttpError extends Error {
|
|
|
49
50
|
/** The thrown error object. */
|
|
50
51
|
error: unknown);
|
|
51
52
|
}
|
|
53
|
+
export declare function toHttpError(method: string, sensitiveLogs: boolean): (err: unknown) => never;
|
package/out/core/error.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpError = exports.GrammyError = void 0;
|
|
3
|
+
exports.toHttpError = exports.HttpError = exports.toGrammyError = exports.GrammyError = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* This class represents errors that are thrown by grammY because the Telegram
|
|
6
6
|
* Bot API responded with an error.
|
|
@@ -20,46 +20,10 @@ class GrammyError extends Error {
|
|
|
20
20
|
payload) {
|
|
21
21
|
var _a;
|
|
22
22
|
super(`${message} (${err.error_code}: ${err.description})`);
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
configurable: true,
|
|
26
|
-
writable: true,
|
|
27
|
-
value: method
|
|
28
|
-
});
|
|
29
|
-
Object.defineProperty(this, "payload", {
|
|
30
|
-
enumerable: true,
|
|
31
|
-
configurable: true,
|
|
32
|
-
writable: true,
|
|
33
|
-
value: payload
|
|
34
|
-
});
|
|
23
|
+
this.method = method;
|
|
24
|
+
this.payload = payload;
|
|
35
25
|
/** Flag that this request was unsuccessful. Always `false`. */
|
|
36
|
-
|
|
37
|
-
enumerable: true,
|
|
38
|
-
configurable: true,
|
|
39
|
-
writable: true,
|
|
40
|
-
value: false
|
|
41
|
-
});
|
|
42
|
-
/** An integer holding Telegram's error code. Subject to change. */
|
|
43
|
-
Object.defineProperty(this, "error_code", {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
configurable: true,
|
|
46
|
-
writable: true,
|
|
47
|
-
value: void 0
|
|
48
|
-
});
|
|
49
|
-
/** A human-readable description of the error. */
|
|
50
|
-
Object.defineProperty(this, "description", {
|
|
51
|
-
enumerable: true,
|
|
52
|
-
configurable: true,
|
|
53
|
-
writable: true,
|
|
54
|
-
value: void 0
|
|
55
|
-
});
|
|
56
|
-
/** Further parameters that may help to automatically handle the error. */
|
|
57
|
-
Object.defineProperty(this, "parameters", {
|
|
58
|
-
enumerable: true,
|
|
59
|
-
configurable: true,
|
|
60
|
-
writable: true,
|
|
61
|
-
value: void 0
|
|
62
|
-
});
|
|
26
|
+
this.ok = false;
|
|
63
27
|
this.name = "GrammyError";
|
|
64
28
|
this.error_code = err.error_code;
|
|
65
29
|
this.description = err.description;
|
|
@@ -67,6 +31,10 @@ class GrammyError extends Error {
|
|
|
67
31
|
}
|
|
68
32
|
}
|
|
69
33
|
exports.GrammyError = GrammyError;
|
|
34
|
+
function toGrammyError(err, method, payload) {
|
|
35
|
+
return new GrammyError(`Call to '${method}' failed!`, err, method, payload);
|
|
36
|
+
}
|
|
37
|
+
exports.toGrammyError = toGrammyError;
|
|
70
38
|
/**
|
|
71
39
|
* This class represents errors that are thrown by grammY because an HTTP call
|
|
72
40
|
* to the Telegram Bot API failed.
|
|
@@ -85,13 +53,25 @@ class HttpError extends Error {
|
|
|
85
53
|
/** The thrown error object. */
|
|
86
54
|
error) {
|
|
87
55
|
super(message);
|
|
88
|
-
|
|
89
|
-
enumerable: true,
|
|
90
|
-
configurable: true,
|
|
91
|
-
writable: true,
|
|
92
|
-
value: error
|
|
93
|
-
});
|
|
56
|
+
this.error = error;
|
|
94
57
|
this.name = "HttpError";
|
|
95
58
|
}
|
|
96
59
|
}
|
|
97
60
|
exports.HttpError = HttpError;
|
|
61
|
+
function isTelegramError(err) {
|
|
62
|
+
return (typeof err === "object" &&
|
|
63
|
+
err !== null &&
|
|
64
|
+
"status" in err &&
|
|
65
|
+
"statusText" in err);
|
|
66
|
+
}
|
|
67
|
+
function toHttpError(method, sensitiveLogs) {
|
|
68
|
+
return (err) => {
|
|
69
|
+
let msg = `Network request for '${method}' failed!`;
|
|
70
|
+
if (isTelegramError(err))
|
|
71
|
+
msg += ` (${err.status}: ${err.statusText})`;
|
|
72
|
+
if (sensitiveLogs && err instanceof Error)
|
|
73
|
+
msg += ` ${err.message}`;
|
|
74
|
+
throw new HttpError(msg, err);
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
exports.toHttpError = toHttpError;
|