gramio 0.0.20 → 0.0.22
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 +12 -5
- package/dist/bot.js +42 -15
- package/dist/errors.d.ts +1 -1
- package/dist/plugin.d.ts +14 -3
- package/dist/plugin.js +19 -1
- package/dist/types.d.ts +7 -4
- package/dist/updates.d.ts +3 -3
- package/dist/webhook/adapters.d.ts +2 -2
- package/dist/webhook/adapters.js +3 -3
- package/dist/webhook/index.d.ts +1 -1
- package/package.json +3 -3
package/dist/bot.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types";
|
|
1
|
+
import type { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
|
+
import type { APIMethodParams, APIMethods, SetMyCommandsParams, TelegramBotCommand, TelegramUser } from "@gramio/types";
|
|
3
3
|
import { Plugin } from "./plugin";
|
|
4
|
-
import { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise } from "./types";
|
|
4
|
+
import type { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise } from "./types";
|
|
5
5
|
import { Updates } from "./updates";
|
|
6
6
|
export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
|
|
7
|
+
/** @internal */
|
|
7
8
|
__Derives: Derives;
|
|
8
9
|
readonly options: BotOptions;
|
|
9
10
|
info: TelegramUser | undefined;
|
|
@@ -75,8 +76,14 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
|
|
|
75
76
|
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
|
|
76
77
|
use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
|
|
77
78
|
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
|
|
79
|
-
|
|
79
|
+
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
|
|
80
|
+
args: RegExpMatchArray | null;
|
|
81
|
+
}) => unknown): this;
|
|
82
|
+
command(command: string, handler: (context: ContextType<typeof this, "message"> & Derives["global"] & Derives["message"] & {
|
|
83
|
+
args: string | null;
|
|
84
|
+
}) => unknown, options?: Omit<SetMyCommandsParams, "commands"> & Omit<TelegramBotCommand, "command">): this;
|
|
85
|
+
/** Currently not isolated!!! */
|
|
86
|
+
group(grouped: (bot: typeof this) => Bot<any, any>): Bot<any, any>;
|
|
80
87
|
start({ webhook, dropPendingUpdates, allowedUpdates, }?: {
|
|
81
88
|
webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">;
|
|
82
89
|
dropPendingUpdates?: boolean;
|
package/dist/bot.js
CHANGED
|
@@ -39,6 +39,7 @@ const files_1 = require("@gramio/files");
|
|
|
39
39
|
const format_1 = require("@gramio/format");
|
|
40
40
|
const inspectable_1 = require("inspectable");
|
|
41
41
|
const undici_1 = require("undici");
|
|
42
|
+
const _plugin_1 = require("./plugin");
|
|
42
43
|
const errors_1 = require("./errors");
|
|
43
44
|
const updates_1 = require("./updates");
|
|
44
45
|
let Bot = (() => {
|
|
@@ -57,6 +58,7 @@ let Bot = (() => {
|
|
|
57
58
|
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
58
59
|
__runInitializers(_classThis, _classExtraInitializers);
|
|
59
60
|
}
|
|
61
|
+
/** @internal */
|
|
60
62
|
__Derives;
|
|
61
63
|
options = {};
|
|
62
64
|
info;
|
|
@@ -78,17 +80,7 @@ let Bot = (() => {
|
|
|
78
80
|
}
|
|
79
81
|
updates = new updates_1.Updates(this, this.errorHandler.bind(this));
|
|
80
82
|
hooks = {
|
|
81
|
-
preRequest: [
|
|
82
|
-
(context) => {
|
|
83
|
-
if (!context.params)
|
|
84
|
-
return context;
|
|
85
|
-
const formattable = format_1.FormattableMap[context.method];
|
|
86
|
-
// @ts-ignore add AnyTelegramMethod to @gramio/format
|
|
87
|
-
if (formattable)
|
|
88
|
-
context.params = formattable(context.params);
|
|
89
|
-
return context;
|
|
90
|
-
},
|
|
91
|
-
],
|
|
83
|
+
preRequest: [],
|
|
92
84
|
onError: [],
|
|
93
85
|
onStart: [],
|
|
94
86
|
};
|
|
@@ -96,6 +88,18 @@ let Bot = (() => {
|
|
|
96
88
|
if (!token || typeof token !== "string")
|
|
97
89
|
throw new Error(`Token is ${typeof token} but it should be a string!`);
|
|
98
90
|
this.options = { ...options, token };
|
|
91
|
+
if (!(options?.plugins &&
|
|
92
|
+
"format" in options.plugins &&
|
|
93
|
+
!options.plugins.format))
|
|
94
|
+
this.extend(new _plugin_1.Plugin("@gramio/format").preRequest((context) => {
|
|
95
|
+
if (!context.params)
|
|
96
|
+
return context;
|
|
97
|
+
const formattable = format_1.FormattableMap[context.method];
|
|
98
|
+
// @ts-ignore add AnyTelegramMethod to @gramio/format
|
|
99
|
+
if (formattable)
|
|
100
|
+
context.params = formattable(context.params);
|
|
101
|
+
return context;
|
|
102
|
+
}));
|
|
99
103
|
}
|
|
100
104
|
async runHooks(type, context) {
|
|
101
105
|
let data = context;
|
|
@@ -264,17 +268,30 @@ let Bot = (() => {
|
|
|
264
268
|
else
|
|
265
269
|
this.derive(updateName, derive);
|
|
266
270
|
}
|
|
271
|
+
for (const value of plugin.preRequests) {
|
|
272
|
+
const [preRequest, updateName] = value;
|
|
273
|
+
if (!updateName)
|
|
274
|
+
this.preRequest(preRequest);
|
|
275
|
+
else
|
|
276
|
+
this.preRequest(updateName, preRequest);
|
|
277
|
+
}
|
|
278
|
+
for (const handler of plugin.groups) {
|
|
279
|
+
this.group(handler);
|
|
280
|
+
}
|
|
267
281
|
this.dependencies.push(plugin.name);
|
|
268
282
|
return this;
|
|
269
283
|
}
|
|
270
284
|
hears(trigger, handler) {
|
|
271
|
-
this.on("message", (context, next) => {
|
|
285
|
+
return this.on("message", (context, next) => {
|
|
272
286
|
if ((typeof trigger === "string" && context.text === trigger) ||
|
|
273
287
|
// @ts-expect-error
|
|
274
288
|
(typeof trigger === "function" && trigger(context)) ||
|
|
275
289
|
(trigger instanceof RegExp &&
|
|
276
290
|
context.text &&
|
|
277
291
|
trigger.test(context.text))) {
|
|
292
|
+
//@ts-expect-error
|
|
293
|
+
context.args =
|
|
294
|
+
trigger instanceof RegExp ? context.text?.match(trigger) : null;
|
|
278
295
|
// TODO: remove
|
|
279
296
|
//@ts-expect-error
|
|
280
297
|
return handler(context);
|
|
@@ -282,8 +299,11 @@ let Bot = (() => {
|
|
|
282
299
|
return next();
|
|
283
300
|
});
|
|
284
301
|
}
|
|
285
|
-
command(command, handler) {
|
|
286
|
-
|
|
302
|
+
command(command, handler, options) {
|
|
303
|
+
if (command.startsWith("/"))
|
|
304
|
+
throw new Error("Do not use / in command name");
|
|
305
|
+
return this.on("message", (context, next) => {
|
|
306
|
+
// TODO: change to find
|
|
287
307
|
if (context.entities?.some((entity) => {
|
|
288
308
|
if (entity.type !== "bot_command" || entity.offset > 0)
|
|
289
309
|
return false;
|
|
@@ -291,12 +311,19 @@ let Bot = (() => {
|
|
|
291
311
|
?.slice(1, entity.length)
|
|
292
312
|
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
293
313
|
?.replace(`@${this.info.username}`, "");
|
|
294
|
-
|
|
314
|
+
// @ts-expect-error
|
|
315
|
+
context.args = context.text?.slice(entity.length).trim() || null;
|
|
316
|
+
return cmd?.startsWith(command);
|
|
295
317
|
}))
|
|
318
|
+
// @ts-expect-error
|
|
296
319
|
return handler(context);
|
|
297
320
|
return next();
|
|
298
321
|
});
|
|
299
322
|
}
|
|
323
|
+
/** Currently not isolated!!! */
|
|
324
|
+
group(grouped) {
|
|
325
|
+
return grouped(this);
|
|
326
|
+
}
|
|
300
327
|
async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) {
|
|
301
328
|
await Promise.all(this.lazyloadPlugins.map(async (plugin) => this.extend(await plugin)));
|
|
302
329
|
this.info = await this.api.getMe();
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { APIMethodParams, APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types";
|
|
1
|
+
import type { APIMethodParams, APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types";
|
|
2
2
|
export declare const ErrorKind: unique symbol;
|
|
3
3
|
export declare class TelegramError<T extends keyof APIMethods> extends Error {
|
|
4
4
|
method: T;
|
package/dist/plugin.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import { BotLike, Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import {
|
|
1
|
+
import type { BotLike, Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
|
+
import type { APIMethods } from "@gramio/types";
|
|
3
|
+
import type { DeriveDefinitions, ErrorDefinitions, Hooks } from "./types";
|
|
4
|
+
import type { Bot } from "./bot";
|
|
3
5
|
export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
|
|
4
6
|
Errors: Errors;
|
|
5
7
|
Derives: Derives;
|
|
6
|
-
derives: [Hooks.Derive<any>, UpdateName | undefined][];
|
|
8
|
+
derives: [Hooks.Derive<any>, MaybeArray<UpdateName> | undefined][];
|
|
9
|
+
preRequests: [
|
|
10
|
+
Hooks.PreRequest<any>,
|
|
11
|
+
MaybeArray<keyof APIMethods> | undefined
|
|
12
|
+
][];
|
|
13
|
+
groups: ((bot: Bot<any, any>) => Bot<any, any>)[];
|
|
7
14
|
name: string;
|
|
8
15
|
errorsDefinitions: Record<string, {
|
|
9
16
|
new (...args: any): any;
|
|
@@ -13,6 +20,8 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
|
|
|
13
20
|
constructor(name: string, { dependencies }?: {
|
|
14
21
|
dependencies?: string[];
|
|
15
22
|
});
|
|
23
|
+
/** Currently not isolated!!! */
|
|
24
|
+
group(grouped: (bot: Bot<Errors, Derives>) => Bot<any, any>): this;
|
|
16
25
|
/**
|
|
17
26
|
* Register custom class-error in plugin
|
|
18
27
|
**/
|
|
@@ -26,4 +35,6 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
|
|
|
26
35
|
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<BotLike, Update>>>(updateName: MaybeArray<Update>, handler: Handler): Plugin<Errors, Derives & {
|
|
27
36
|
[K in Update]: Awaited<ReturnType<Handler>>;
|
|
28
37
|
}>;
|
|
38
|
+
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
39
|
+
preRequest(handler: Hooks.PreRequest): this;
|
|
29
40
|
}
|
package/dist/plugin.js
CHANGED
|
@@ -60,6 +60,8 @@ let Plugin = (() => {
|
|
|
60
60
|
Errors;
|
|
61
61
|
Derives;
|
|
62
62
|
derives = [];
|
|
63
|
+
preRequests = [];
|
|
64
|
+
groups = [];
|
|
63
65
|
name;
|
|
64
66
|
errorsDefinitions = {};
|
|
65
67
|
dependencies = [];
|
|
@@ -68,6 +70,11 @@ let Plugin = (() => {
|
|
|
68
70
|
if (dependencies)
|
|
69
71
|
this.dependencies = dependencies;
|
|
70
72
|
}
|
|
73
|
+
/** Currently not isolated!!! */
|
|
74
|
+
group(grouped) {
|
|
75
|
+
this.groups.push(grouped);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
71
78
|
/**
|
|
72
79
|
* Register custom class-error in plugin
|
|
73
80
|
**/
|
|
@@ -78,12 +85,23 @@ let Plugin = (() => {
|
|
|
78
85
|
return this;
|
|
79
86
|
}
|
|
80
87
|
derive(updateNameOrHandler, handler) {
|
|
81
|
-
if (typeof updateNameOrHandler === "string"
|
|
88
|
+
if ((typeof updateNameOrHandler === "string" ||
|
|
89
|
+
Array.isArray(updateNameOrHandler)) &&
|
|
90
|
+
handler)
|
|
82
91
|
this.derives.push([handler, updateNameOrHandler]);
|
|
83
92
|
else if (typeof updateNameOrHandler === "function")
|
|
84
93
|
this.derives.push([updateNameOrHandler, undefined]);
|
|
85
94
|
return this;
|
|
86
95
|
}
|
|
96
|
+
preRequest(methodsOrHandler, handler) {
|
|
97
|
+
if ((typeof methodsOrHandler === "string" ||
|
|
98
|
+
Array.isArray(methodsOrHandler)) &&
|
|
99
|
+
handler)
|
|
100
|
+
this.preRequests.push([handler, methodsOrHandler]);
|
|
101
|
+
else if (typeof methodsOrHandler === "function")
|
|
102
|
+
this.preRequests.push([methodsOrHandler, undefined]);
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
87
105
|
};
|
|
88
106
|
return Plugin = _classThis;
|
|
89
107
|
})();
|
package/dist/types.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { BotLike, Context, UpdateName } from "@gramio/contexts";
|
|
2
|
-
import { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types";
|
|
3
|
-
import { NextMiddleware } from "middleware-io";
|
|
4
|
-
import { TelegramError } from "./errors";
|
|
1
|
+
import type { BotLike, Context, UpdateName } from "@gramio/contexts";
|
|
2
|
+
import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types";
|
|
3
|
+
import type { NextMiddleware } from "middleware-io";
|
|
4
|
+
import type { TelegramError } from "./errors";
|
|
5
5
|
export interface BotOptions {
|
|
6
6
|
token?: string;
|
|
7
|
+
plugins?: {
|
|
8
|
+
format?: boolean;
|
|
9
|
+
};
|
|
7
10
|
}
|
|
8
11
|
export type Handler<T> = (context: T, next: NextMiddleware) => unknown;
|
|
9
12
|
interface ErrorHandlerParams<Ctx extends Context<BotLike>, Kind extends string, Err> {
|
package/dist/updates.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Context, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
1
|
+
import { type Context, type MaybeArray, type UpdateName } from "@gramio/contexts";
|
|
2
2
|
import type { APIMethodParams, TelegramUpdate } from "@gramio/types";
|
|
3
|
-
import { CaughtMiddlewareHandler } from "middleware-io";
|
|
3
|
+
import { type CaughtMiddlewareHandler } from "middleware-io";
|
|
4
4
|
import type { Bot } from "./bot";
|
|
5
|
-
import { Handler } from "./types";
|
|
5
|
+
import type { Handler } from "./types";
|
|
6
6
|
export declare class Updates {
|
|
7
7
|
private readonly bot;
|
|
8
8
|
isStarted: boolean;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { TelegramUpdate } from "@gramio/types";
|
|
2
|
-
import { MaybePromise } from "../types";
|
|
1
|
+
import type { TelegramUpdate } from "@gramio/types";
|
|
2
|
+
import type { MaybePromise } from "../types";
|
|
3
3
|
export interface FrameworkHandler {
|
|
4
4
|
update: MaybePromise<TelegramUpdate>;
|
|
5
5
|
header?: string;
|
package/dist/webhook/adapters.js
CHANGED
|
@@ -13,10 +13,10 @@ exports.frameworks = {
|
|
|
13
13
|
}),
|
|
14
14
|
hono: (c) => ({
|
|
15
15
|
update: c.req.json(),
|
|
16
|
-
header: c.req.header(SECRET_TOKEN_HEADER)
|
|
16
|
+
header: c.req.header(SECRET_TOKEN_HEADER),
|
|
17
17
|
}),
|
|
18
18
|
express: (req) => ({
|
|
19
19
|
update: req.body,
|
|
20
|
-
header: req.header(SECRET_TOKEN_HEADER)
|
|
21
|
-
})
|
|
20
|
+
header: req.header(SECRET_TOKEN_HEADER),
|
|
21
|
+
}),
|
|
22
22
|
};
|
package/dist/webhook/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.22",
|
|
4
4
|
"description": "Powerful Telegram Bot API framework",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"author": "kravets",
|
|
25
25
|
"license": "ISC",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@biomejs/biome": "1.
|
|
28
|
-
"@gramio/types": "^7.1.
|
|
27
|
+
"@biomejs/biome": "1.6.0",
|
|
28
|
+
"@gramio/types": "^7.1.7",
|
|
29
29
|
"@types/node": "^20.11.25",
|
|
30
30
|
"typescript": "^5.4.2"
|
|
31
31
|
},
|