gramio 0.0.23 → 0.0.25
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/bot.d.ts +10 -2
- package/dist/bot.js +98 -6
- package/dist/plugin.d.ts +12 -0
- package/dist/plugin.js +20 -0
- package/dist/types.d.ts +20 -4
- package/package.json +2 -2
package/dist/bot.d.ts
CHANGED
|
@@ -72,14 +72,19 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
|
|
|
72
72
|
[K in Update]: Awaited<ReturnType<Handler>>;
|
|
73
73
|
}>;
|
|
74
74
|
onStart(handler: Hooks.OnStart): this;
|
|
75
|
+
onStop(handler: Hooks.OnStop): this;
|
|
75
76
|
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
76
77
|
preRequest(handler: Hooks.PreRequest): this;
|
|
78
|
+
onResponse<Methods extends keyof APIMethods, Handler extends Hooks.OnResponse<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
79
|
+
onResponse(handler: Hooks.OnResponse): this;
|
|
80
|
+
onResponseError<Methods extends keyof APIMethods, Handler extends Hooks.OnResponseError<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
81
|
+
onResponseError(handler: Hooks.OnResponseError): this;
|
|
77
82
|
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
|
|
78
83
|
use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
|
|
79
84
|
extend<NewPlugin extends Plugin<any, any>>(plugin: MaybePromise<NewPlugin>): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
|
|
80
85
|
reaction(trigger: MaybeArray<TelegramReactionTypeEmojiEmoji>, handler: (context: ContextType<typeof this, "message_reaction"> & Derives["global"] & Derives["message_reaction"]) => unknown): this;
|
|
81
86
|
callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context: Omit<ContextType<typeof this, "callback_query">, "data"> & Derives["global"] & Derives["callback_query"] & {
|
|
82
|
-
|
|
87
|
+
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : RegExpMatchArray | null;
|
|
83
88
|
}) => unknown): this;
|
|
84
89
|
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
|
|
85
90
|
args: RegExpMatchArray | null;
|
|
@@ -89,9 +94,12 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
|
|
|
89
94
|
}) => unknown, options?: Omit<SetMyCommandsParams, "commands"> & Omit<TelegramBotCommand, "command">): this;
|
|
90
95
|
/** Currently not isolated!!! */
|
|
91
96
|
group(grouped: (bot: typeof this) => Bot<any, any>): Bot<any, any>;
|
|
97
|
+
init(): Promise<void>;
|
|
92
98
|
start({ webhook, dropPendingUpdates, allowedUpdates, }?: {
|
|
93
99
|
webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">;
|
|
94
100
|
dropPendingUpdates?: boolean;
|
|
95
101
|
allowedUpdates?: NonNullable<APIMethodParams<"getUpdates">>["allowed_updates"];
|
|
96
|
-
}): Promise<TelegramUser>;
|
|
102
|
+
}): Promise<TelegramUser | undefined>;
|
|
103
|
+
/** Currently does not implement graceful shutdown */
|
|
104
|
+
stop(): Promise<void>;
|
|
97
105
|
}
|
package/dist/bot.js
CHANGED
|
@@ -82,8 +82,11 @@ let Bot = (() => {
|
|
|
82
82
|
updates = new updates_1.Updates(this, this.errorHandler.bind(this));
|
|
83
83
|
hooks = {
|
|
84
84
|
preRequest: [],
|
|
85
|
+
onResponse: [],
|
|
86
|
+
onResponseError: [],
|
|
85
87
|
onError: [],
|
|
86
88
|
onStart: [],
|
|
89
|
+
onStop: [],
|
|
87
90
|
};
|
|
88
91
|
constructor(token, options) {
|
|
89
92
|
if (!token || typeof token !== "string")
|
|
@@ -109,11 +112,11 @@ let Bot = (() => {
|
|
|
109
112
|
}
|
|
110
113
|
return data;
|
|
111
114
|
}
|
|
112
|
-
async runImmutableHooks(type, context) {
|
|
115
|
+
async runImmutableHooks(type, ...context) {
|
|
113
116
|
for await (const hook of this.hooks[type]) {
|
|
114
117
|
//TODO: solve that later
|
|
115
118
|
//@ts-expect-error
|
|
116
|
-
await hook(context);
|
|
119
|
+
await hook(...context);
|
|
117
120
|
}
|
|
118
121
|
}
|
|
119
122
|
async _callApi(method, params = {}) {
|
|
@@ -142,8 +145,20 @@ let Bot = (() => {
|
|
|
142
145
|
}
|
|
143
146
|
const response = await (0, undici_1.request)(url, reqOptions);
|
|
144
147
|
const data = (await response.body.json());
|
|
145
|
-
if (!data.ok)
|
|
146
|
-
|
|
148
|
+
if (!data.ok) {
|
|
149
|
+
const err = new errors_1.TelegramError(data, method, params);
|
|
150
|
+
// @ts-expect-error
|
|
151
|
+
this.runImmutableHooks("onResponseError", err, this.api);
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
154
|
+
this.runImmutableHooks("onResponse",
|
|
155
|
+
// TODO: fix type error
|
|
156
|
+
// @ts-expect-error
|
|
157
|
+
{
|
|
158
|
+
method,
|
|
159
|
+
params,
|
|
160
|
+
response: data.result,
|
|
161
|
+
});
|
|
147
162
|
return data.result;
|
|
148
163
|
}
|
|
149
164
|
/**
|
|
@@ -218,6 +233,10 @@ let Bot = (() => {
|
|
|
218
233
|
this.hooks.onStart.push(handler);
|
|
219
234
|
return this;
|
|
220
235
|
}
|
|
236
|
+
onStop(handler) {
|
|
237
|
+
this.hooks.onStop.push(handler);
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
221
240
|
preRequest(methodsOrHandler, handler) {
|
|
222
241
|
if (typeof methodsOrHandler === "string" ||
|
|
223
242
|
Array.isArray(methodsOrHandler)) {
|
|
@@ -241,6 +260,48 @@ let Bot = (() => {
|
|
|
241
260
|
this.hooks.preRequest.push(methodsOrHandler);
|
|
242
261
|
return this;
|
|
243
262
|
}
|
|
263
|
+
onResponse(methodsOrHandler, handler) {
|
|
264
|
+
if (typeof methodsOrHandler === "string" ||
|
|
265
|
+
Array.isArray(methodsOrHandler)) {
|
|
266
|
+
// TODO: error
|
|
267
|
+
if (!handler)
|
|
268
|
+
throw new Error("TODO:");
|
|
269
|
+
const methods = typeof methodsOrHandler === "string"
|
|
270
|
+
? [methodsOrHandler]
|
|
271
|
+
: methodsOrHandler;
|
|
272
|
+
this.hooks.onResponse.push(async (context) => {
|
|
273
|
+
// TODO: remove ts-ignore
|
|
274
|
+
// @ts-expect-error
|
|
275
|
+
if (methods.includes(context.method))
|
|
276
|
+
return handler(context);
|
|
277
|
+
return context;
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
else
|
|
281
|
+
this.hooks.onResponse.push(methodsOrHandler);
|
|
282
|
+
return this;
|
|
283
|
+
}
|
|
284
|
+
onResponseError(methodsOrHandler, handler) {
|
|
285
|
+
if (typeof methodsOrHandler === "string" ||
|
|
286
|
+
Array.isArray(methodsOrHandler)) {
|
|
287
|
+
// TODO: error
|
|
288
|
+
if (!handler)
|
|
289
|
+
throw new Error("TODO:");
|
|
290
|
+
const methods = typeof methodsOrHandler === "string"
|
|
291
|
+
? [methodsOrHandler]
|
|
292
|
+
: methodsOrHandler;
|
|
293
|
+
this.hooks.onResponseError.push(async (context) => {
|
|
294
|
+
// TODO: remove ts-ignore
|
|
295
|
+
// @ts-expect-error
|
|
296
|
+
if (methods.includes(context.method))
|
|
297
|
+
return handler(context);
|
|
298
|
+
return context;
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
else
|
|
302
|
+
this.hooks.onResponseError.push(methodsOrHandler);
|
|
303
|
+
return this;
|
|
304
|
+
}
|
|
244
305
|
on(updateName, handler) {
|
|
245
306
|
this.updates.on(updateName, handler);
|
|
246
307
|
return this;
|
|
@@ -276,6 +337,20 @@ let Bot = (() => {
|
|
|
276
337
|
else
|
|
277
338
|
this.preRequest(updateName, preRequest);
|
|
278
339
|
}
|
|
340
|
+
for (const value of plugin.onResponses) {
|
|
341
|
+
const [onResponse, updateName] = value;
|
|
342
|
+
if (!updateName)
|
|
343
|
+
this.onResponse(onResponse);
|
|
344
|
+
else
|
|
345
|
+
this.onResponse(updateName, onResponse);
|
|
346
|
+
}
|
|
347
|
+
for (const value of plugin.onResponseErrors) {
|
|
348
|
+
const [onResponseError, updateName] = value;
|
|
349
|
+
if (!updateName)
|
|
350
|
+
this.onResponseError(onResponseError);
|
|
351
|
+
else
|
|
352
|
+
this.onResponseError(updateName, onResponseError);
|
|
353
|
+
}
|
|
279
354
|
for (const handler of plugin.groups) {
|
|
280
355
|
this.group(handler);
|
|
281
356
|
}
|
|
@@ -316,7 +391,7 @@ let Bot = (() => {
|
|
|
316
391
|
if (trigger instanceof RegExp && !trigger.test(context.data))
|
|
317
392
|
return next();
|
|
318
393
|
// @ts-expect-error
|
|
319
|
-
context.
|
|
394
|
+
context.queryData = trigger.unpack(context.data);
|
|
320
395
|
//@ts-expect-error
|
|
321
396
|
return handler(context);
|
|
322
397
|
});
|
|
@@ -364,9 +439,12 @@ let Bot = (() => {
|
|
|
364
439
|
group(grouped) {
|
|
365
440
|
return grouped(this);
|
|
366
441
|
}
|
|
367
|
-
async
|
|
442
|
+
async init() {
|
|
368
443
|
await Promise.all(this.lazyloadPlugins.map(async (plugin) => this.extend(await plugin)));
|
|
369
444
|
this.info = await this.api.getMe();
|
|
445
|
+
}
|
|
446
|
+
async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) {
|
|
447
|
+
await this.init();
|
|
370
448
|
if (!webhook) {
|
|
371
449
|
await this.api.deleteWebhook({
|
|
372
450
|
drop_pending_updates: dropPendingUpdates,
|
|
@@ -376,6 +454,7 @@ let Bot = (() => {
|
|
|
376
454
|
});
|
|
377
455
|
this.runImmutableHooks("onStart", {
|
|
378
456
|
plugins: this.dependencies,
|
|
457
|
+
// biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
|
|
379
458
|
info: this.info,
|
|
380
459
|
updatesFrom: "long-polling",
|
|
381
460
|
});
|
|
@@ -390,11 +469,24 @@ let Bot = (() => {
|
|
|
390
469
|
});
|
|
391
470
|
this.runImmutableHooks("onStart", {
|
|
392
471
|
plugins: this.dependencies,
|
|
472
|
+
// biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
|
|
393
473
|
info: this.info,
|
|
394
474
|
updatesFrom: "webhook",
|
|
395
475
|
});
|
|
396
476
|
return this.info;
|
|
397
477
|
}
|
|
478
|
+
/** Currently does not implement graceful shutdown */
|
|
479
|
+
async stop() {
|
|
480
|
+
if (this.updates.isStarted)
|
|
481
|
+
this.updates.stopPolling();
|
|
482
|
+
else
|
|
483
|
+
await this.api.deleteWebhook();
|
|
484
|
+
await this.runImmutableHooks("onStop", {
|
|
485
|
+
plugins: this.dependencies,
|
|
486
|
+
// biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
|
|
487
|
+
info: this.info,
|
|
488
|
+
});
|
|
489
|
+
}
|
|
398
490
|
};
|
|
399
491
|
return Bot = _classThis;
|
|
400
492
|
})();
|
package/dist/plugin.d.ts
CHANGED
|
@@ -10,6 +10,14 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
|
|
|
10
10
|
Hooks.PreRequest<any>,
|
|
11
11
|
MaybeArray<keyof APIMethods> | undefined
|
|
12
12
|
][];
|
|
13
|
+
onResponses: [
|
|
14
|
+
Hooks.OnResponse<any>,
|
|
15
|
+
MaybeArray<keyof APIMethods> | undefined
|
|
16
|
+
][];
|
|
17
|
+
onResponseErrors: [
|
|
18
|
+
Hooks.OnResponseError<any>,
|
|
19
|
+
MaybeArray<keyof APIMethods> | undefined
|
|
20
|
+
][];
|
|
13
21
|
groups: ((bot: Bot<any, any>) => Bot<any, any>)[];
|
|
14
22
|
name: string;
|
|
15
23
|
errorsDefinitions: Record<string, {
|
|
@@ -37,4 +45,8 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
|
|
|
37
45
|
}>;
|
|
38
46
|
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
39
47
|
preRequest(handler: Hooks.PreRequest): this;
|
|
48
|
+
onResponse<Methods extends keyof APIMethods, Handler extends Hooks.OnResponse<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
49
|
+
onResponse(handler: Hooks.OnResponse): this;
|
|
50
|
+
onResponseError<Methods extends keyof APIMethods, Handler extends Hooks.OnResponseError<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
51
|
+
onResponseError(handler: Hooks.OnResponseError): this;
|
|
40
52
|
}
|
package/dist/plugin.js
CHANGED
|
@@ -61,6 +61,8 @@ let Plugin = (() => {
|
|
|
61
61
|
Derives;
|
|
62
62
|
derives = [];
|
|
63
63
|
preRequests = [];
|
|
64
|
+
onResponses = [];
|
|
65
|
+
onResponseErrors = [];
|
|
64
66
|
groups = [];
|
|
65
67
|
name;
|
|
66
68
|
errorsDefinitions = {};
|
|
@@ -102,6 +104,24 @@ let Plugin = (() => {
|
|
|
102
104
|
this.preRequests.push([methodsOrHandler, undefined]);
|
|
103
105
|
return this;
|
|
104
106
|
}
|
|
107
|
+
onResponse(methodsOrHandler, handler) {
|
|
108
|
+
if ((typeof methodsOrHandler === "string" ||
|
|
109
|
+
Array.isArray(methodsOrHandler)) &&
|
|
110
|
+
handler)
|
|
111
|
+
this.onResponses.push([handler, methodsOrHandler]);
|
|
112
|
+
else if (typeof methodsOrHandler === "function")
|
|
113
|
+
this.onResponses.push([methodsOrHandler, undefined]);
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
onResponseError(methodsOrHandler, handler) {
|
|
117
|
+
if ((typeof methodsOrHandler === "string" ||
|
|
118
|
+
Array.isArray(methodsOrHandler)) &&
|
|
119
|
+
handler)
|
|
120
|
+
this.onResponseErrors.push([handler, methodsOrHandler]);
|
|
121
|
+
else if (typeof methodsOrHandler === "function")
|
|
122
|
+
this.onResponseErrors.push([methodsOrHandler, undefined]);
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
105
125
|
};
|
|
106
126
|
return Plugin = _classThis;
|
|
107
127
|
})();
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { BotLike, Context, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types";
|
|
2
|
+
import type { APIMethodParams, APIMethodReturn, APIMethods, TelegramUser } from "@gramio/types";
|
|
3
3
|
import type { NextMiddleware } from "middleware-io";
|
|
4
4
|
import type { TelegramError } from "./errors";
|
|
5
5
|
export interface BotOptions {
|
|
@@ -14,15 +14,22 @@ interface ErrorHandlerParams<Ctx extends Context<BotLike>, Kind extends string,
|
|
|
14
14
|
kind: Kind;
|
|
15
15
|
error: Err;
|
|
16
16
|
}
|
|
17
|
-
type AnyTelegramError = {
|
|
18
|
-
[APIMethod in
|
|
19
|
-
}[
|
|
17
|
+
type AnyTelegramError<Methods extends keyof APIMethods = keyof APIMethods> = {
|
|
18
|
+
[APIMethod in Methods]: TelegramError<APIMethod>;
|
|
19
|
+
}[Methods];
|
|
20
20
|
type AnyTelegramMethod<Methods extends keyof APIMethods> = {
|
|
21
21
|
[APIMethod in Methods]: {
|
|
22
22
|
method: APIMethod;
|
|
23
23
|
params: APIMethodParams<APIMethod>;
|
|
24
24
|
};
|
|
25
25
|
}[Methods];
|
|
26
|
+
type AnyTelegramMethodWithReturn<Methods extends keyof APIMethods> = {
|
|
27
|
+
[APIMethod in Methods]: {
|
|
28
|
+
method: APIMethod;
|
|
29
|
+
params: APIMethodParams<APIMethod>;
|
|
30
|
+
response: APIMethodReturn<APIMethod>;
|
|
31
|
+
};
|
|
32
|
+
}[Methods];
|
|
26
33
|
export type MaybePromise<T> = Promise<T> | T;
|
|
27
34
|
export declare namespace Hooks {
|
|
28
35
|
type Derive<Ctx> = (context: Ctx) => MaybePromise<Record<string, unknown>>;
|
|
@@ -37,10 +44,19 @@ export declare namespace Hooks {
|
|
|
37
44
|
info: TelegramUser;
|
|
38
45
|
updatesFrom: "webhook" | "long-polling";
|
|
39
46
|
}) => unknown;
|
|
47
|
+
type OnStop = (context: {
|
|
48
|
+
plugins: string[];
|
|
49
|
+
info: TelegramUser;
|
|
50
|
+
}) => unknown;
|
|
51
|
+
type OnResponseError<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramError<Methods>, api: BotLike["api"]) => unknown;
|
|
52
|
+
type OnResponse<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramMethodWithReturn<Methods>) => unknown;
|
|
40
53
|
interface Store<T extends ErrorDefinitions> {
|
|
41
54
|
preRequest: PreRequest[];
|
|
55
|
+
onResponse: OnResponse[];
|
|
56
|
+
onResponseError: OnResponseError[];
|
|
42
57
|
onError: OnError<T>[];
|
|
43
58
|
onStart: OnStart[];
|
|
59
|
+
onStop: OnStop[];
|
|
44
60
|
}
|
|
45
61
|
}
|
|
46
62
|
export type ErrorDefinitions = Record<string, Error>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.25",
|
|
4
4
|
"description": "Powerful Telegram Bot API framework",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"typescript": "^5.4.3"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@gramio/callback-data": "^0.0.
|
|
33
|
+
"@gramio/callback-data": "^0.0.2",
|
|
34
34
|
"@gramio/contexts": "^0.0.7",
|
|
35
35
|
"@gramio/files": "^0.0.3",
|
|
36
36
|
"@gramio/format": "^0.0.8",
|