gramio 0.0.19 → 0.0.20
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 +4 -0
- package/dist/bot.js +59 -6
- package/dist/types.d.ts +5 -5
- package/dist/updates.js +2 -2
- package/package.json +4 -4
package/dist/bot.d.ts
CHANGED
|
@@ -70,9 +70,13 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
|
|
|
70
70
|
[K in Update]: Awaited<ReturnType<Handler>>;
|
|
71
71
|
}>;
|
|
72
72
|
onStart(handler: Hooks.OnStart): this;
|
|
73
|
+
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
74
|
+
preRequest(handler: Hooks.PreRequest): this;
|
|
73
75
|
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
|
|
74
76
|
use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
|
|
75
77
|
extend<NewPlugin extends Plugin>(plugin: MaybePromise<NewPlugin>): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
|
|
78
|
+
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx) => unknown): void;
|
|
79
|
+
command(command: string, handler: (context: ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]) => unknown): void;
|
|
76
80
|
start({ webhook, dropPendingUpdates, allowedUpdates, }?: {
|
|
77
81
|
webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">;
|
|
78
82
|
dropPendingUpdates?: boolean;
|
package/dist/bot.js
CHANGED
|
@@ -79,14 +79,14 @@ let Bot = (() => {
|
|
|
79
79
|
updates = new updates_1.Updates(this, this.errorHandler.bind(this));
|
|
80
80
|
hooks = {
|
|
81
81
|
preRequest: [
|
|
82
|
-
(
|
|
83
|
-
if (!
|
|
84
|
-
return
|
|
85
|
-
const formattable = format_1.FormattableMap[
|
|
82
|
+
(context) => {
|
|
83
|
+
if (!context.params)
|
|
84
|
+
return context;
|
|
85
|
+
const formattable = format_1.FormattableMap[context.method];
|
|
86
86
|
// @ts-ignore add AnyTelegramMethod to @gramio/format
|
|
87
87
|
if (formattable)
|
|
88
|
-
|
|
89
|
-
return
|
|
88
|
+
context.params = formattable(context.params);
|
|
89
|
+
return context;
|
|
90
90
|
},
|
|
91
91
|
],
|
|
92
92
|
onError: [],
|
|
@@ -213,6 +213,29 @@ let Bot = (() => {
|
|
|
213
213
|
this.hooks.onStart.push(handler);
|
|
214
214
|
return this;
|
|
215
215
|
}
|
|
216
|
+
preRequest(methodsOrHandler, handler) {
|
|
217
|
+
if (typeof methodsOrHandler === "string" ||
|
|
218
|
+
Array.isArray(methodsOrHandler)) {
|
|
219
|
+
// TODO: error
|
|
220
|
+
if (!handler)
|
|
221
|
+
throw new Error("TODO:");
|
|
222
|
+
const methods = typeof methodsOrHandler === "string"
|
|
223
|
+
? [methodsOrHandler]
|
|
224
|
+
: methodsOrHandler;
|
|
225
|
+
// TODO: remove error
|
|
226
|
+
// @ts-expect-error
|
|
227
|
+
this.hooks.preRequest.push(async (context) => {
|
|
228
|
+
// TODO: remove ts-ignore
|
|
229
|
+
// @ts-expect-error
|
|
230
|
+
if (methods.includes(context.method))
|
|
231
|
+
return handler(context);
|
|
232
|
+
return context;
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
else
|
|
236
|
+
this.hooks.preRequest.push(methodsOrHandler);
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
216
239
|
on(updateName, handler) {
|
|
217
240
|
this.updates.on(updateName, handler);
|
|
218
241
|
return this;
|
|
@@ -244,6 +267,36 @@ let Bot = (() => {
|
|
|
244
267
|
this.dependencies.push(plugin.name);
|
|
245
268
|
return this;
|
|
246
269
|
}
|
|
270
|
+
hears(trigger, handler) {
|
|
271
|
+
this.on("message", (context, next) => {
|
|
272
|
+
if ((typeof trigger === "string" && context.text === trigger) ||
|
|
273
|
+
// @ts-expect-error
|
|
274
|
+
(typeof trigger === "function" && trigger(context)) ||
|
|
275
|
+
(trigger instanceof RegExp &&
|
|
276
|
+
context.text &&
|
|
277
|
+
trigger.test(context.text))) {
|
|
278
|
+
// TODO: remove
|
|
279
|
+
//@ts-expect-error
|
|
280
|
+
return handler(context);
|
|
281
|
+
}
|
|
282
|
+
return next();
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
command(command, handler) {
|
|
286
|
+
this.on("message", (context, next) => {
|
|
287
|
+
if (context.entities?.some((entity) => {
|
|
288
|
+
if (entity.type !== "bot_command" || entity.offset > 0)
|
|
289
|
+
return false;
|
|
290
|
+
const cmd = context.text
|
|
291
|
+
?.slice(1, entity.length)
|
|
292
|
+
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
293
|
+
?.replace(`@${this.info.username}`, "");
|
|
294
|
+
return cmd && cmd === command;
|
|
295
|
+
}))
|
|
296
|
+
return handler(context);
|
|
297
|
+
return next();
|
|
298
|
+
});
|
|
299
|
+
}
|
|
247
300
|
async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) {
|
|
248
301
|
await Promise.all(this.lazyloadPlugins.map(async (plugin) => this.extend(await plugin)));
|
|
249
302
|
this.info = await this.api.getMe();
|
package/dist/types.d.ts
CHANGED
|
@@ -14,17 +14,17 @@ interface ErrorHandlerParams<Ctx extends Context<BotLike>, Kind extends string,
|
|
|
14
14
|
type AnyTelegramError = {
|
|
15
15
|
[APIMethod in keyof APIMethods]: TelegramError<APIMethod>;
|
|
16
16
|
}[keyof APIMethods];
|
|
17
|
-
type AnyTelegramMethod = {
|
|
18
|
-
[APIMethod in
|
|
17
|
+
type AnyTelegramMethod<Methods extends keyof APIMethods> = {
|
|
18
|
+
[APIMethod in Methods]: {
|
|
19
19
|
method: APIMethod;
|
|
20
20
|
params: APIMethodParams<APIMethod>;
|
|
21
21
|
};
|
|
22
|
-
}[
|
|
22
|
+
}[Methods];
|
|
23
23
|
export type MaybePromise<T> = Promise<T> | T;
|
|
24
24
|
export declare namespace Hooks {
|
|
25
25
|
type Derive<Ctx> = (context: Ctx) => MaybePromise<Record<string, unknown>>;
|
|
26
|
-
type PreRequestContext = AnyTelegramMethod
|
|
27
|
-
type PreRequest = (ctx: PreRequestContext) => MaybePromise<PreRequestContext
|
|
26
|
+
type PreRequestContext<Methods extends keyof APIMethods> = AnyTelegramMethod<Methods>;
|
|
27
|
+
type PreRequest<Methods extends keyof APIMethods = keyof APIMethods> = (ctx: PreRequestContext<Methods>) => MaybePromise<PreRequestContext<Methods>>;
|
|
28
28
|
type OnErrorContext<Ctx extends Context<BotLike>, T extends ErrorDefinitions> = ErrorHandlerParams<Ctx, "TELEGRAM", AnyTelegramError> | ErrorHandlerParams<Ctx, "UNKNOWN", Error> | {
|
|
29
29
|
[K in keyof T]: ErrorHandlerParams<Ctx, K & string, T[K & string]>;
|
|
30
30
|
}[keyof T];
|
package/dist/updates.js
CHANGED
|
@@ -32,7 +32,7 @@ class Updates {
|
|
|
32
32
|
this.offset = data.update_id + 1;
|
|
33
33
|
const UpdateContext = contexts_1.contextsMappings[updateType];
|
|
34
34
|
if (!UpdateContext)
|
|
35
|
-
|
|
35
|
+
throw new Error(updateType);
|
|
36
36
|
try {
|
|
37
37
|
let context = new UpdateContext({
|
|
38
38
|
bot: this.bot,
|
|
@@ -74,8 +74,8 @@ class Updates {
|
|
|
74
74
|
async startFetchLoop(params = {}) {
|
|
75
75
|
while (this.isStarted) {
|
|
76
76
|
const updates = await this.bot.api.getUpdates({
|
|
77
|
-
offset: this.offset,
|
|
78
77
|
...params,
|
|
78
|
+
offset: this.offset,
|
|
79
79
|
});
|
|
80
80
|
for await (const update of updates) {
|
|
81
81
|
//TODO: update errors
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"description": "Powerful Telegram Bot API framework",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@biomejs/biome": "1.5.3",
|
|
28
28
|
"@gramio/types": "^7.1.6",
|
|
29
|
-
"@types/node": "^20.11.
|
|
30
|
-
"typescript": "^5.
|
|
29
|
+
"@types/node": "^20.11.25",
|
|
30
|
+
"typescript": "^5.4.2"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@gramio/contexts": "^0.0.7",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"@gramio/keyboards": "^0.2.0",
|
|
37
37
|
"inspectable": "^3.0.0",
|
|
38
38
|
"middleware-io": "^2.8.1",
|
|
39
|
-
"undici": "^6.
|
|
39
|
+
"undici": "^6.7.1"
|
|
40
40
|
},
|
|
41
41
|
"files": [
|
|
42
42
|
"dist"
|