gramio 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +29 -65
- package/dist/index.d.cts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +15 -51
- package/dist/utils-BbVj3DtT.cjs +72 -0
- package/dist/utils-DTFsIb2X.js +65 -0
- package/dist/utils.cjs +7 -0
- package/dist/utils.d.cts +8 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.js +1 -0
- package/package.json +15 -5
package/dist/index.cjs
CHANGED
|
@@ -8,31 +8,10 @@ var files = require('@gramio/files');
|
|
|
8
8
|
var format = require('@gramio/format');
|
|
9
9
|
var debug = require('debug');
|
|
10
10
|
var inspectable = require('inspectable');
|
|
11
|
+
var utils = require('./utils-BbVj3DtT.cjs');
|
|
11
12
|
var middlewareIo = require('middleware-io');
|
|
12
13
|
var keyboards = require('@gramio/keyboards');
|
|
13
14
|
|
|
14
|
-
const ErrorKind = Symbol("ErrorKind");
|
|
15
|
-
class TelegramError extends Error {
|
|
16
|
-
/** Name of the API Method */
|
|
17
|
-
method;
|
|
18
|
-
/** Params that were sent */
|
|
19
|
-
params;
|
|
20
|
-
/** See {@link TelegramAPIResponseError.error_code}*/
|
|
21
|
-
code;
|
|
22
|
-
/** Describes why a request was unsuccessful. */
|
|
23
|
-
payload;
|
|
24
|
-
/** Construct new TelegramError */
|
|
25
|
-
constructor(error, method, params) {
|
|
26
|
-
super(error.description);
|
|
27
|
-
this.name = method;
|
|
28
|
-
this.method = method;
|
|
29
|
-
this.params = params;
|
|
30
|
-
this.code = error.error_code;
|
|
31
|
-
if (error.parameters) this.payload = error.parameters;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
TelegramError.constructor[ErrorKind] = "TELEGRAM";
|
|
35
|
-
|
|
36
15
|
class Composer {
|
|
37
16
|
composer = middlewareIo.Composer.builder();
|
|
38
17
|
length = 0;
|
|
@@ -197,7 +176,7 @@ class Plugin {
|
|
|
197
176
|
* Register custom class-error in plugin
|
|
198
177
|
**/
|
|
199
178
|
error(kind, error) {
|
|
200
|
-
error[ErrorKind] = kind;
|
|
179
|
+
error[utils.ErrorKind] = kind;
|
|
201
180
|
this._.errorsDefinitions[kind] = error;
|
|
202
181
|
return this;
|
|
203
182
|
}
|
|
@@ -316,25 +295,6 @@ _init$1 = __decoratorStart$1();
|
|
|
316
295
|
Plugin = __decorateElement$1(_init$1, 0, "Plugin", _Plugin_decorators, Plugin);
|
|
317
296
|
__runInitializers$1(_init$1, 1, Plugin);
|
|
318
297
|
|
|
319
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
320
|
-
function convertToString(value) {
|
|
321
|
-
const typeOfValue = typeof value;
|
|
322
|
-
if (typeOfValue === "string") return value;
|
|
323
|
-
if (typeOfValue === "object") return JSON.stringify(value);
|
|
324
|
-
return String(value);
|
|
325
|
-
}
|
|
326
|
-
function simplifyObject(obj) {
|
|
327
|
-
const result = {};
|
|
328
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
329
|
-
const typeOfValue = typeof value;
|
|
330
|
-
if (value === void 0 || value === null || typeOfValue === "function")
|
|
331
|
-
continue;
|
|
332
|
-
result[key] = convertToString(value);
|
|
333
|
-
}
|
|
334
|
-
return result;
|
|
335
|
-
}
|
|
336
|
-
const IS_BUN = typeof Bun !== "undefined";
|
|
337
|
-
|
|
338
298
|
class UpdateQueue {
|
|
339
299
|
updateQueue = [];
|
|
340
300
|
pendingUpdates = /* @__PURE__ */ new Set();
|
|
@@ -372,7 +332,7 @@ class UpdateQueue {
|
|
|
372
332
|
this.onIdlePromise = new Promise((resolve) => {
|
|
373
333
|
this.onIdleResolver = resolve;
|
|
374
334
|
});
|
|
375
|
-
await Promise.race([this.onIdlePromise, sleep(timeout)]);
|
|
335
|
+
await Promise.race([this.onIdlePromise, utils.sleep(timeout)]);
|
|
376
336
|
this.isActive = false;
|
|
377
337
|
}
|
|
378
338
|
}
|
|
@@ -442,9 +402,9 @@ class Updates {
|
|
|
442
402
|
}
|
|
443
403
|
} catch (error) {
|
|
444
404
|
console.error("Error received when fetching updates", error);
|
|
445
|
-
if (error instanceof TelegramError && error.payload?.retry_after) {
|
|
446
|
-
await sleep(error.payload.retry_after * 1e3);
|
|
447
|
-
} else await sleep(this.bot.options.api.retryGetUpdatesWait ?? 1e3);
|
|
405
|
+
if (error instanceof utils.TelegramError && error.payload?.retry_after) {
|
|
406
|
+
await utils.sleep(error.payload.retry_after * 1e3);
|
|
407
|
+
} else await utils.sleep(this.bot.options.api.retryGetUpdatesWait ?? 1e3);
|
|
448
408
|
}
|
|
449
409
|
}
|
|
450
410
|
}
|
|
@@ -529,7 +489,7 @@ class Bot {
|
|
|
529
489
|
lazyloadPlugins = [];
|
|
530
490
|
dependencies = [];
|
|
531
491
|
errorsDefinitions = {
|
|
532
|
-
TELEGRAM: TelegramError
|
|
492
|
+
TELEGRAM: utils.TelegramError
|
|
533
493
|
};
|
|
534
494
|
errorHandler(context, error) {
|
|
535
495
|
if (!this.hooks.onError.length)
|
|
@@ -537,7 +497,7 @@ class Bot {
|
|
|
537
497
|
return this.runImmutableHooks("onError", {
|
|
538
498
|
context,
|
|
539
499
|
//@ts-expect-error ErrorKind exists if user register error-class with .error("kind", SomeError);
|
|
540
|
-
kind: error.constructor[ErrorKind] ?? "UNKNOWN",
|
|
500
|
+
kind: error.constructor[utils.ErrorKind] ?? "UNKNOWN",
|
|
541
501
|
error
|
|
542
502
|
});
|
|
543
503
|
}
|
|
@@ -609,7 +569,7 @@ class Bot {
|
|
|
609
569
|
);
|
|
610
570
|
params = context.params;
|
|
611
571
|
if (params && files.isMediaUpload(method, params)) {
|
|
612
|
-
if (IS_BUN) {
|
|
572
|
+
if (utils.IS_BUN) {
|
|
613
573
|
const formData = await files.convertJsonToFormData(method, params);
|
|
614
574
|
reqOptions.body = formData;
|
|
615
575
|
} else {
|
|
@@ -618,7 +578,7 @@ class Bot {
|
|
|
618
578
|
params
|
|
619
579
|
);
|
|
620
580
|
reqOptions.body = formData;
|
|
621
|
-
const simplifiedParams = simplifyObject(paramsWithoutFiles);
|
|
581
|
+
const simplifiedParams = utils.simplifyObject(paramsWithoutFiles);
|
|
622
582
|
url += `?${new URLSearchParams(simplifiedParams).toString()}`;
|
|
623
583
|
}
|
|
624
584
|
} else {
|
|
@@ -630,7 +590,7 @@ class Bot {
|
|
|
630
590
|
const data = await response.json();
|
|
631
591
|
debug$api$method("response: %j", data);
|
|
632
592
|
if (!data.ok) {
|
|
633
|
-
const err = new TelegramError(data, method, params);
|
|
593
|
+
const err = new utils.TelegramError(data, method, params);
|
|
634
594
|
this.runImmutableHooks("onResponseError", err, this.api);
|
|
635
595
|
if (!params?.suppress) throw err;
|
|
636
596
|
return err;
|
|
@@ -701,7 +661,7 @@ class Bot {
|
|
|
701
661
|
* ```
|
|
702
662
|
*/
|
|
703
663
|
error(kind, error) {
|
|
704
|
-
error[ErrorKind] = kind;
|
|
664
|
+
error[utils.ErrorKind] = kind;
|
|
705
665
|
this.errorsDefinitions[kind] = error;
|
|
706
666
|
return this;
|
|
707
667
|
}
|
|
@@ -1103,7 +1063,7 @@ class Bot {
|
|
|
1103
1063
|
const info = await this.api.getMe({
|
|
1104
1064
|
suppress: true
|
|
1105
1065
|
});
|
|
1106
|
-
if (info instanceof TelegramError) {
|
|
1066
|
+
if (info instanceof utils.TelegramError) {
|
|
1107
1067
|
if (info.code === 404)
|
|
1108
1068
|
info.message = "The bot token is incorrect. Check it in BotFather.";
|
|
1109
1069
|
throw info;
|
|
@@ -1132,10 +1092,12 @@ class Bot {
|
|
|
1132
1092
|
} = {}) {
|
|
1133
1093
|
await this.init();
|
|
1134
1094
|
if (!webhook) {
|
|
1135
|
-
await
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1095
|
+
await utils.withRetries(
|
|
1096
|
+
() => this.api.deleteWebhook({
|
|
1097
|
+
drop_pending_updates: dropPendingUpdates
|
|
1098
|
+
// suppress: true,
|
|
1099
|
+
})
|
|
1100
|
+
);
|
|
1139
1101
|
this.updates.startPolling({
|
|
1140
1102
|
allowed_updates: allowedUpdates
|
|
1141
1103
|
});
|
|
@@ -1148,12 +1110,14 @@ class Bot {
|
|
|
1148
1110
|
return this.info;
|
|
1149
1111
|
}
|
|
1150
1112
|
if (this.updates.isStarted) this.updates.stopPolling();
|
|
1151
|
-
await
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1113
|
+
await utils.withRetries(
|
|
1114
|
+
async () => this.api.setWebhook({
|
|
1115
|
+
...webhook,
|
|
1116
|
+
drop_pending_updates: dropPendingUpdates,
|
|
1117
|
+
allowed_updates: allowedUpdates
|
|
1118
|
+
// suppress: true,
|
|
1119
|
+
})
|
|
1120
|
+
);
|
|
1157
1121
|
this.runImmutableHooks("onStart", {
|
|
1158
1122
|
plugins: this.dependencies,
|
|
1159
1123
|
// biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
|
|
@@ -1257,11 +1221,11 @@ function webhookHandler(bot, framework, secretToken) {
|
|
|
1257
1221
|
|
|
1258
1222
|
Symbol.metadata ??= Symbol("Symbol.metadata");
|
|
1259
1223
|
|
|
1224
|
+
exports.ErrorKind = utils.ErrorKind;
|
|
1225
|
+
exports.TelegramError = utils.TelegramError;
|
|
1260
1226
|
exports.Bot = Bot;
|
|
1261
1227
|
exports.Composer = Composer;
|
|
1262
|
-
exports.ErrorKind = ErrorKind;
|
|
1263
1228
|
exports.Plugin = Plugin;
|
|
1264
|
-
exports.TelegramError = TelegramError;
|
|
1265
1229
|
exports.Updates = Updates;
|
|
1266
1230
|
exports.webhookHandler = webhookHandler;
|
|
1267
1231
|
Object.keys(callbackData).forEach(function (k) {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CallbackData } from '@gramio/callback-data';
|
|
2
2
|
export * from '@gramio/callback-data';
|
|
3
|
-
import { Context, UpdateName, MaybeArray, ContextType, Attachment } from '@gramio/contexts';
|
|
3
|
+
import { Context, UpdateName, MaybeArray, ContextType, BotLike, Attachment } from '@gramio/contexts';
|
|
4
4
|
export * from '@gramio/contexts';
|
|
5
5
|
import { APIMethods, TelegramResponseParameters, TelegramAPIResponseError, TelegramUser, APIMethodParams, APIMethodReturn, TelegramUpdate, TelegramReactionTypeEmojiEmoji, SetMyCommandsParams, TelegramBotCommand } from '@gramio/types';
|
|
6
6
|
export * from '@gramio/types';
|
|
@@ -556,6 +556,9 @@ type FilterDefinitions = Record<string, (...args: any[]) => (context: Context<Bo
|
|
|
556
556
|
type AnyBot = Bot<any, any>;
|
|
557
557
|
/** Type of Bot that accepts any generics */
|
|
558
558
|
type AnyPlugin = Plugin<any, any>;
|
|
559
|
+
type CallbackQueryShorthandContext<BotType extends BotLike, Trigger extends CallbackData | string | RegExp> = Omit<ContextType<BotType, "callback_query">, "data"> & BotType["__Derives"]["global"] & BotType["__Derives"]["callback_query"] & {
|
|
560
|
+
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : Trigger extends RegExp ? RegExpMatchArray : never;
|
|
561
|
+
};
|
|
559
562
|
|
|
560
563
|
declare class UpdateQueue<Data = TelegramUpdate> {
|
|
561
564
|
private updateQueue;
|
|
@@ -888,9 +891,7 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
|
|
|
888
891
|
* });
|
|
889
892
|
* ```
|
|
890
893
|
*/
|
|
891
|
-
callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context:
|
|
892
|
-
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : Trigger extends RegExp ? RegExpMatchArray : never;
|
|
893
|
-
}) => unknown): this;
|
|
894
|
+
callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context: CallbackQueryShorthandContext<typeof this, Trigger>) => unknown): this;
|
|
894
895
|
/** Register handler to `chosen_inline_result` update */
|
|
895
896
|
chosenInlineResult<Ctx = ContextType<typeof this, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
|
|
896
897
|
args: RegExpMatchArray | null;
|
|
@@ -1068,4 +1069,4 @@ declare function webhookHandler<Framework extends keyof typeof frameworks>(bot:
|
|
|
1068
1069
|
response: () => any;
|
|
1069
1070
|
} ? (...args: Parameters<(typeof frameworks)[Framework]>) => ReturnType<ReturnType<(typeof frameworks)[Framework]>["response"]> : (...args: Parameters<(typeof frameworks)[Framework]>) => void;
|
|
1070
1071
|
|
|
1071
|
-
export { type AnyBot, type AnyPlugin, Bot, type BotOptions, Composer, type DeriveDefinitions, type ErrorDefinitions, ErrorKind, type FilterDefinitions, type Handler, Hooks, type MaybePromise, type MaybeSuppressedParams, type MaybeSuppressedReturn, Plugin, type Suppress, type SuppressedAPIMethodParams, type SuppressedAPIMethodReturn, type SuppressedAPIMethods, TelegramError, Updates, type WebhookHandlers, webhookHandler };
|
|
1072
|
+
export { type AnyBot, type AnyPlugin, Bot, type BotOptions, type CallbackQueryShorthandContext, Composer, type DeriveDefinitions, type ErrorDefinitions, ErrorKind, type FilterDefinitions, type Handler, Hooks, type MaybePromise, type MaybeSuppressedParams, type MaybeSuppressedReturn, Plugin, type Suppress, type SuppressedAPIMethodParams, type SuppressedAPIMethodReturn, type SuppressedAPIMethods, TelegramError, Updates, type WebhookHandlers, webhookHandler };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CallbackData } from '@gramio/callback-data';
|
|
2
2
|
export * from '@gramio/callback-data';
|
|
3
|
-
import { Context, UpdateName, MaybeArray, ContextType, Attachment } from '@gramio/contexts';
|
|
3
|
+
import { Context, UpdateName, MaybeArray, ContextType, BotLike, Attachment } from '@gramio/contexts';
|
|
4
4
|
export * from '@gramio/contexts';
|
|
5
5
|
import { APIMethods, TelegramResponseParameters, TelegramAPIResponseError, TelegramUser, APIMethodParams, APIMethodReturn, TelegramUpdate, TelegramReactionTypeEmojiEmoji, SetMyCommandsParams, TelegramBotCommand } from '@gramio/types';
|
|
6
6
|
export * from '@gramio/types';
|
|
@@ -556,6 +556,9 @@ type FilterDefinitions = Record<string, (...args: any[]) => (context: Context<Bo
|
|
|
556
556
|
type AnyBot = Bot<any, any>;
|
|
557
557
|
/** Type of Bot that accepts any generics */
|
|
558
558
|
type AnyPlugin = Plugin<any, any>;
|
|
559
|
+
type CallbackQueryShorthandContext<BotType extends BotLike, Trigger extends CallbackData | string | RegExp> = Omit<ContextType<BotType, "callback_query">, "data"> & BotType["__Derives"]["global"] & BotType["__Derives"]["callback_query"] & {
|
|
560
|
+
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : Trigger extends RegExp ? RegExpMatchArray : never;
|
|
561
|
+
};
|
|
559
562
|
|
|
560
563
|
declare class UpdateQueue<Data = TelegramUpdate> {
|
|
561
564
|
private updateQueue;
|
|
@@ -888,9 +891,7 @@ declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDe
|
|
|
888
891
|
* });
|
|
889
892
|
* ```
|
|
890
893
|
*/
|
|
891
|
-
callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context:
|
|
892
|
-
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : Trigger extends RegExp ? RegExpMatchArray : never;
|
|
893
|
-
}) => unknown): this;
|
|
894
|
+
callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context: CallbackQueryShorthandContext<typeof this, Trigger>) => unknown): this;
|
|
894
895
|
/** Register handler to `chosen_inline_result` update */
|
|
895
896
|
chosenInlineResult<Ctx = ContextType<typeof this, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
|
|
896
897
|
args: RegExpMatchArray | null;
|
|
@@ -1068,4 +1069,4 @@ declare function webhookHandler<Framework extends keyof typeof frameworks>(bot:
|
|
|
1068
1069
|
response: () => any;
|
|
1069
1070
|
} ? (...args: Parameters<(typeof frameworks)[Framework]>) => ReturnType<ReturnType<(typeof frameworks)[Framework]>["response"]> : (...args: Parameters<(typeof frameworks)[Framework]>) => void;
|
|
1070
1071
|
|
|
1071
|
-
export { type AnyBot, type AnyPlugin, Bot, type BotOptions, Composer, type DeriveDefinitions, type ErrorDefinitions, ErrorKind, type FilterDefinitions, type Handler, Hooks, type MaybePromise, type MaybeSuppressedParams, type MaybeSuppressedReturn, Plugin, type Suppress, type SuppressedAPIMethodParams, type SuppressedAPIMethodReturn, type SuppressedAPIMethods, TelegramError, Updates, type WebhookHandlers, webhookHandler };
|
|
1072
|
+
export { type AnyBot, type AnyPlugin, Bot, type BotOptions, type CallbackQueryShorthandContext, Composer, type DeriveDefinitions, type ErrorDefinitions, ErrorKind, type FilterDefinitions, type Handler, Hooks, type MaybePromise, type MaybeSuppressedParams, type MaybeSuppressedReturn, Plugin, type Suppress, type SuppressedAPIMethodParams, type SuppressedAPIMethodReturn, type SuppressedAPIMethods, TelegramError, Updates, type WebhookHandlers, webhookHandler };
|
package/dist/index.js
CHANGED
|
@@ -10,31 +10,10 @@ import { FormattableMap } from '@gramio/format';
|
|
|
10
10
|
export * from '@gramio/format';
|
|
11
11
|
import debug from 'debug';
|
|
12
12
|
import { Inspectable } from 'inspectable';
|
|
13
|
+
import { E as ErrorKind, s as sleep, T as TelegramError, I as IS_BUN, a as simplifyObject, w as withRetries } from './utils-DTFsIb2X.js';
|
|
13
14
|
import { Composer as Composer$1, noopNext } from 'middleware-io';
|
|
14
15
|
export * from '@gramio/keyboards';
|
|
15
16
|
|
|
16
|
-
const ErrorKind = Symbol("ErrorKind");
|
|
17
|
-
class TelegramError extends Error {
|
|
18
|
-
/** Name of the API Method */
|
|
19
|
-
method;
|
|
20
|
-
/** Params that were sent */
|
|
21
|
-
params;
|
|
22
|
-
/** See {@link TelegramAPIResponseError.error_code}*/
|
|
23
|
-
code;
|
|
24
|
-
/** Describes why a request was unsuccessful. */
|
|
25
|
-
payload;
|
|
26
|
-
/** Construct new TelegramError */
|
|
27
|
-
constructor(error, method, params) {
|
|
28
|
-
super(error.description);
|
|
29
|
-
this.name = method;
|
|
30
|
-
this.method = method;
|
|
31
|
-
this.params = params;
|
|
32
|
-
this.code = error.error_code;
|
|
33
|
-
if (error.parameters) this.payload = error.parameters;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
TelegramError.constructor[ErrorKind] = "TELEGRAM";
|
|
37
|
-
|
|
38
17
|
class Composer {
|
|
39
18
|
composer = Composer$1.builder();
|
|
40
19
|
length = 0;
|
|
@@ -318,25 +297,6 @@ _init$1 = __decoratorStart$1();
|
|
|
318
297
|
Plugin = __decorateElement$1(_init$1, 0, "Plugin", _Plugin_decorators, Plugin);
|
|
319
298
|
__runInitializers$1(_init$1, 1, Plugin);
|
|
320
299
|
|
|
321
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
322
|
-
function convertToString(value) {
|
|
323
|
-
const typeOfValue = typeof value;
|
|
324
|
-
if (typeOfValue === "string") return value;
|
|
325
|
-
if (typeOfValue === "object") return JSON.stringify(value);
|
|
326
|
-
return String(value);
|
|
327
|
-
}
|
|
328
|
-
function simplifyObject(obj) {
|
|
329
|
-
const result = {};
|
|
330
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
331
|
-
const typeOfValue = typeof value;
|
|
332
|
-
if (value === void 0 || value === null || typeOfValue === "function")
|
|
333
|
-
continue;
|
|
334
|
-
result[key] = convertToString(value);
|
|
335
|
-
}
|
|
336
|
-
return result;
|
|
337
|
-
}
|
|
338
|
-
const IS_BUN = typeof Bun !== "undefined";
|
|
339
|
-
|
|
340
300
|
class UpdateQueue {
|
|
341
301
|
updateQueue = [];
|
|
342
302
|
pendingUpdates = /* @__PURE__ */ new Set();
|
|
@@ -1134,10 +1094,12 @@ class Bot {
|
|
|
1134
1094
|
} = {}) {
|
|
1135
1095
|
await this.init();
|
|
1136
1096
|
if (!webhook) {
|
|
1137
|
-
await
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1097
|
+
await withRetries(
|
|
1098
|
+
() => this.api.deleteWebhook({
|
|
1099
|
+
drop_pending_updates: dropPendingUpdates
|
|
1100
|
+
// suppress: true,
|
|
1101
|
+
})
|
|
1102
|
+
);
|
|
1141
1103
|
this.updates.startPolling({
|
|
1142
1104
|
allowed_updates: allowedUpdates
|
|
1143
1105
|
});
|
|
@@ -1150,12 +1112,14 @@ class Bot {
|
|
|
1150
1112
|
return this.info;
|
|
1151
1113
|
}
|
|
1152
1114
|
if (this.updates.isStarted) this.updates.stopPolling();
|
|
1153
|
-
await
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1115
|
+
await withRetries(
|
|
1116
|
+
async () => this.api.setWebhook({
|
|
1117
|
+
...webhook,
|
|
1118
|
+
drop_pending_updates: dropPendingUpdates,
|
|
1119
|
+
allowed_updates: allowedUpdates
|
|
1120
|
+
// suppress: true,
|
|
1121
|
+
})
|
|
1122
|
+
);
|
|
1159
1123
|
this.runImmutableHooks("onStart", {
|
|
1160
1124
|
plugins: this.dependencies,
|
|
1161
1125
|
// biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ErrorKind = Symbol("ErrorKind");
|
|
4
|
+
class TelegramError extends Error {
|
|
5
|
+
/** Name of the API Method */
|
|
6
|
+
method;
|
|
7
|
+
/** Params that were sent */
|
|
8
|
+
params;
|
|
9
|
+
/** See {@link TelegramAPIResponseError.error_code}*/
|
|
10
|
+
code;
|
|
11
|
+
/** Describes why a request was unsuccessful. */
|
|
12
|
+
payload;
|
|
13
|
+
/** Construct new TelegramError */
|
|
14
|
+
constructor(error, method, params) {
|
|
15
|
+
super(error.description);
|
|
16
|
+
this.name = method;
|
|
17
|
+
this.method = method;
|
|
18
|
+
this.params = params;
|
|
19
|
+
this.code = error.error_code;
|
|
20
|
+
if (error.parameters) this.payload = error.parameters;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
TelegramError.constructor[ErrorKind] = "TELEGRAM";
|
|
24
|
+
|
|
25
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
26
|
+
function convertToString(value) {
|
|
27
|
+
const typeOfValue = typeof value;
|
|
28
|
+
if (typeOfValue === "string") return value;
|
|
29
|
+
if (typeOfValue === "object") return JSON.stringify(value);
|
|
30
|
+
return String(value);
|
|
31
|
+
}
|
|
32
|
+
function simplifyObject(obj) {
|
|
33
|
+
const result = {};
|
|
34
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
35
|
+
const typeOfValue = typeof value;
|
|
36
|
+
if (value === void 0 || value === null || typeOfValue === "function")
|
|
37
|
+
continue;
|
|
38
|
+
result[key] = convertToString(value);
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
const IS_BUN = typeof Bun !== "undefined";
|
|
43
|
+
|
|
44
|
+
async function suppressError(fn) {
|
|
45
|
+
try {
|
|
46
|
+
return [await fn(), false];
|
|
47
|
+
} catch (error) {
|
|
48
|
+
return [error, true];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async function withRetries(resultPromise) {
|
|
52
|
+
let [result, isFromCatch] = await suppressError(resultPromise);
|
|
53
|
+
while (result instanceof TelegramError) {
|
|
54
|
+
const retryAfter = result.payload?.retry_after;
|
|
55
|
+
if (retryAfter) {
|
|
56
|
+
await sleep(retryAfter * 1e3);
|
|
57
|
+
[result, isFromCatch] = await suppressError(resultPromise);
|
|
58
|
+
} else {
|
|
59
|
+
if (isFromCatch) throw result;
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (result instanceof Error && isFromCatch) throw result;
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
exports.ErrorKind = ErrorKind;
|
|
68
|
+
exports.IS_BUN = IS_BUN;
|
|
69
|
+
exports.TelegramError = TelegramError;
|
|
70
|
+
exports.simplifyObject = simplifyObject;
|
|
71
|
+
exports.sleep = sleep;
|
|
72
|
+
exports.withRetries = withRetries;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const ErrorKind = Symbol("ErrorKind");
|
|
2
|
+
class TelegramError extends Error {
|
|
3
|
+
/** Name of the API Method */
|
|
4
|
+
method;
|
|
5
|
+
/** Params that were sent */
|
|
6
|
+
params;
|
|
7
|
+
/** See {@link TelegramAPIResponseError.error_code}*/
|
|
8
|
+
code;
|
|
9
|
+
/** Describes why a request was unsuccessful. */
|
|
10
|
+
payload;
|
|
11
|
+
/** Construct new TelegramError */
|
|
12
|
+
constructor(error, method, params) {
|
|
13
|
+
super(error.description);
|
|
14
|
+
this.name = method;
|
|
15
|
+
this.method = method;
|
|
16
|
+
this.params = params;
|
|
17
|
+
this.code = error.error_code;
|
|
18
|
+
if (error.parameters) this.payload = error.parameters;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
TelegramError.constructor[ErrorKind] = "TELEGRAM";
|
|
22
|
+
|
|
23
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
24
|
+
function convertToString(value) {
|
|
25
|
+
const typeOfValue = typeof value;
|
|
26
|
+
if (typeOfValue === "string") return value;
|
|
27
|
+
if (typeOfValue === "object") return JSON.stringify(value);
|
|
28
|
+
return String(value);
|
|
29
|
+
}
|
|
30
|
+
function simplifyObject(obj) {
|
|
31
|
+
const result = {};
|
|
32
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
33
|
+
const typeOfValue = typeof value;
|
|
34
|
+
if (value === void 0 || value === null || typeOfValue === "function")
|
|
35
|
+
continue;
|
|
36
|
+
result[key] = convertToString(value);
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
const IS_BUN = typeof Bun !== "undefined";
|
|
41
|
+
|
|
42
|
+
async function suppressError(fn) {
|
|
43
|
+
try {
|
|
44
|
+
return [await fn(), false];
|
|
45
|
+
} catch (error) {
|
|
46
|
+
return [error, true];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async function withRetries(resultPromise) {
|
|
50
|
+
let [result, isFromCatch] = await suppressError(resultPromise);
|
|
51
|
+
while (result instanceof TelegramError) {
|
|
52
|
+
const retryAfter = result.payload?.retry_after;
|
|
53
|
+
if (retryAfter) {
|
|
54
|
+
await sleep(retryAfter * 1e3);
|
|
55
|
+
[result, isFromCatch] = await suppressError(resultPromise);
|
|
56
|
+
} else {
|
|
57
|
+
if (isFromCatch) throw result;
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (result instanceof Error && isFromCatch) throw result;
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { ErrorKind as E, IS_BUN as I, TelegramError as T, simplifyObject as a, sleep as s, withRetries as w };
|
package/dist/utils.cjs
ADDED
package/dist/utils.d.cts
ADDED
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { w as withRetries } from './utils-DTFsIb2X.js';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.6",
|
|
5
5
|
"description": "Powerful, extensible and really type-safe Telegram Bot API framework",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -16,6 +16,16 @@
|
|
|
16
16
|
"types": "./dist/index.d.cts",
|
|
17
17
|
"default": "./dist/index.cjs"
|
|
18
18
|
}
|
|
19
|
+
},
|
|
20
|
+
"./utils": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/utils.d.ts",
|
|
23
|
+
"default": "./dist/utils.js"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/utils.d.cts",
|
|
27
|
+
"default": "./dist/utils.cjs"
|
|
28
|
+
}
|
|
19
29
|
}
|
|
20
30
|
},
|
|
21
31
|
"keywords": [
|
|
@@ -41,14 +51,14 @@
|
|
|
41
51
|
"license": "MIT",
|
|
42
52
|
"devDependencies": {
|
|
43
53
|
"@biomejs/biome": "1.9.4",
|
|
44
|
-
"@types/bun": "^1.2.
|
|
54
|
+
"@types/bun": "^1.2.8",
|
|
45
55
|
"@types/debug": "^4.1.12",
|
|
46
|
-
"pkgroll": "^2.
|
|
47
|
-
"typescript": "^5.8.
|
|
56
|
+
"pkgroll": "^2.12.1",
|
|
57
|
+
"typescript": "^5.8.3"
|
|
48
58
|
},
|
|
49
59
|
"dependencies": {
|
|
50
60
|
"@gramio/callback-data": "^0.0.3",
|
|
51
|
-
"@gramio/contexts": "^0.1.
|
|
61
|
+
"@gramio/contexts": "^0.1.5",
|
|
52
62
|
"@gramio/files": "^0.2.0",
|
|
53
63
|
"@gramio/format": "^0.1.5",
|
|
54
64
|
"@gramio/keyboards": "^1.1.0",
|