gramio 0.0.19 → 0.0.21
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 -3
- package/dist/bot.js +77 -11
- package/dist/errors.d.ts +1 -1
- package/dist/plugin.d.ts +10 -3
- package/dist/plugin.js +13 -1
- package/dist/types.d.ts +12 -9
- package/dist/updates.d.ts +3 -3
- package/dist/updates.js +2 -2
- 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 +6 -6
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;
|
|
@@ -70,9 +71,17 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
|
|
|
70
71
|
[K in Update]: Awaited<ReturnType<Handler>>;
|
|
71
72
|
}>;
|
|
72
73
|
onStart(handler: Hooks.OnStart): this;
|
|
74
|
+
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
75
|
+
preRequest(handler: Hooks.PreRequest): this;
|
|
73
76
|
on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
|
|
74
77
|
use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
|
|
75
78
|
extend<NewPlugin extends Plugin>(plugin: MaybePromise<NewPlugin>): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
|
|
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;
|
|
76
85
|
start({ webhook, dropPendingUpdates, allowedUpdates, }?: {
|
|
77
86
|
webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">;
|
|
78
87
|
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
|
-
(ctx) => {
|
|
83
|
-
if (!ctx.params)
|
|
84
|
-
return ctx;
|
|
85
|
-
const formattable = format_1.FormattableMap[ctx.method];
|
|
86
|
-
// @ts-ignore add AnyTelegramMethod to @gramio/format
|
|
87
|
-
if (formattable)
|
|
88
|
-
ctx.params = formattable(ctx.params);
|
|
89
|
-
return ctx;
|
|
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;
|
|
@@ -213,6 +217,29 @@ let Bot = (() => {
|
|
|
213
217
|
this.hooks.onStart.push(handler);
|
|
214
218
|
return this;
|
|
215
219
|
}
|
|
220
|
+
preRequest(methodsOrHandler, handler) {
|
|
221
|
+
if (typeof methodsOrHandler === "string" ||
|
|
222
|
+
Array.isArray(methodsOrHandler)) {
|
|
223
|
+
// TODO: error
|
|
224
|
+
if (!handler)
|
|
225
|
+
throw new Error("TODO:");
|
|
226
|
+
const methods = typeof methodsOrHandler === "string"
|
|
227
|
+
? [methodsOrHandler]
|
|
228
|
+
: methodsOrHandler;
|
|
229
|
+
// TODO: remove error
|
|
230
|
+
// @ts-expect-error
|
|
231
|
+
this.hooks.preRequest.push(async (context) => {
|
|
232
|
+
// TODO: remove ts-ignore
|
|
233
|
+
// @ts-expect-error
|
|
234
|
+
if (methods.includes(context.method))
|
|
235
|
+
return handler(context);
|
|
236
|
+
return context;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
else
|
|
240
|
+
this.hooks.preRequest.push(methodsOrHandler);
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
216
243
|
on(updateName, handler) {
|
|
217
244
|
this.updates.on(updateName, handler);
|
|
218
245
|
return this;
|
|
@@ -244,6 +271,45 @@ let Bot = (() => {
|
|
|
244
271
|
this.dependencies.push(plugin.name);
|
|
245
272
|
return this;
|
|
246
273
|
}
|
|
274
|
+
hears(trigger, handler) {
|
|
275
|
+
return this.on("message", (context, next) => {
|
|
276
|
+
if ((typeof trigger === "string" && context.text === trigger) ||
|
|
277
|
+
// @ts-expect-error
|
|
278
|
+
(typeof trigger === "function" && trigger(context)) ||
|
|
279
|
+
(trigger instanceof RegExp &&
|
|
280
|
+
context.text &&
|
|
281
|
+
trigger.test(context.text))) {
|
|
282
|
+
//@ts-expect-error
|
|
283
|
+
context.args =
|
|
284
|
+
trigger instanceof RegExp ? context.text?.match(trigger) : null;
|
|
285
|
+
// TODO: remove
|
|
286
|
+
//@ts-expect-error
|
|
287
|
+
return handler(context);
|
|
288
|
+
}
|
|
289
|
+
return next();
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
command(command, handler, options) {
|
|
293
|
+
if (command.startsWith("/"))
|
|
294
|
+
throw new Error("Do not use / in command name");
|
|
295
|
+
return this.on("message", (context, next) => {
|
|
296
|
+
// TODO: change to find
|
|
297
|
+
if (context.entities?.some((entity) => {
|
|
298
|
+
if (entity.type !== "bot_command" || entity.offset > 0)
|
|
299
|
+
return false;
|
|
300
|
+
const cmd = context.text
|
|
301
|
+
?.slice(1, entity.length)
|
|
302
|
+
// biome-ignore lint/style/noNonNullAssertion: <explanation>
|
|
303
|
+
?.replace(`@${this.info.username}`, "");
|
|
304
|
+
// @ts-expect-error
|
|
305
|
+
context.args = context.text?.slice(entity.length).trim() || null;
|
|
306
|
+
return cmd?.startsWith(command);
|
|
307
|
+
}))
|
|
308
|
+
// @ts-expect-error
|
|
309
|
+
return handler(context);
|
|
310
|
+
return next();
|
|
311
|
+
});
|
|
312
|
+
}
|
|
247
313
|
async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) {
|
|
248
314
|
await Promise.all(this.lazyloadPlugins.map(async (plugin) => this.extend(await plugin)));
|
|
249
315
|
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,14 @@
|
|
|
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";
|
|
3
4
|
export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
|
|
4
5
|
Errors: Errors;
|
|
5
6
|
Derives: Derives;
|
|
6
|
-
derives: [Hooks.Derive<any>, UpdateName | undefined][];
|
|
7
|
+
derives: [Hooks.Derive<any>, MaybeArray<UpdateName> | undefined][];
|
|
8
|
+
preRequests: [
|
|
9
|
+
Hooks.PreRequest<any>,
|
|
10
|
+
MaybeArray<keyof APIMethods> | undefined
|
|
11
|
+
][];
|
|
7
12
|
name: string;
|
|
8
13
|
errorsDefinitions: Record<string, {
|
|
9
14
|
new (...args: any): any;
|
|
@@ -26,4 +31,6 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
|
|
|
26
31
|
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<BotLike, Update>>>(updateName: MaybeArray<Update>, handler: Handler): Plugin<Errors, Derives & {
|
|
27
32
|
[K in Update]: Awaited<ReturnType<Handler>>;
|
|
28
33
|
}>;
|
|
34
|
+
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
|
35
|
+
preRequest(handler: Hooks.PreRequest): this;
|
|
29
36
|
}
|
package/dist/plugin.js
CHANGED
|
@@ -60,6 +60,7 @@ let Plugin = (() => {
|
|
|
60
60
|
Errors;
|
|
61
61
|
Derives;
|
|
62
62
|
derives = [];
|
|
63
|
+
preRequests = [];
|
|
63
64
|
name;
|
|
64
65
|
errorsDefinitions = {};
|
|
65
66
|
dependencies = [];
|
|
@@ -78,12 +79,23 @@ let Plugin = (() => {
|
|
|
78
79
|
return this;
|
|
79
80
|
}
|
|
80
81
|
derive(updateNameOrHandler, handler) {
|
|
81
|
-
if (typeof updateNameOrHandler === "string"
|
|
82
|
+
if ((typeof updateNameOrHandler === "string" ||
|
|
83
|
+
Array.isArray(updateNameOrHandler)) &&
|
|
84
|
+
handler)
|
|
82
85
|
this.derives.push([handler, updateNameOrHandler]);
|
|
83
86
|
else if (typeof updateNameOrHandler === "function")
|
|
84
87
|
this.derives.push([updateNameOrHandler, undefined]);
|
|
85
88
|
return this;
|
|
86
89
|
}
|
|
90
|
+
preRequest(methodsOrHandler, handler) {
|
|
91
|
+
if ((typeof methodsOrHandler === "string" ||
|
|
92
|
+
Array.isArray(methodsOrHandler)) &&
|
|
93
|
+
handler)
|
|
94
|
+
this.preRequests.push([handler, methodsOrHandler]);
|
|
95
|
+
else if (typeof methodsOrHandler === "function")
|
|
96
|
+
this.preRequests.push([methodsOrHandler, undefined]);
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
87
99
|
};
|
|
88
100
|
return Plugin = _classThis;
|
|
89
101
|
})();
|
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> {
|
|
@@ -14,17 +17,17 @@ interface ErrorHandlerParams<Ctx extends Context<BotLike>, Kind extends string,
|
|
|
14
17
|
type AnyTelegramError = {
|
|
15
18
|
[APIMethod in keyof APIMethods]: TelegramError<APIMethod>;
|
|
16
19
|
}[keyof APIMethods];
|
|
17
|
-
type AnyTelegramMethod = {
|
|
18
|
-
[APIMethod in
|
|
20
|
+
type AnyTelegramMethod<Methods extends keyof APIMethods> = {
|
|
21
|
+
[APIMethod in Methods]: {
|
|
19
22
|
method: APIMethod;
|
|
20
23
|
params: APIMethodParams<APIMethod>;
|
|
21
24
|
};
|
|
22
|
-
}[
|
|
25
|
+
}[Methods];
|
|
23
26
|
export type MaybePromise<T> = Promise<T> | T;
|
|
24
27
|
export declare namespace Hooks {
|
|
25
28
|
type Derive<Ctx> = (context: Ctx) => MaybePromise<Record<string, unknown>>;
|
|
26
|
-
type PreRequestContext = AnyTelegramMethod
|
|
27
|
-
type PreRequest = (ctx: PreRequestContext) => MaybePromise<PreRequestContext
|
|
29
|
+
type PreRequestContext<Methods extends keyof APIMethods> = AnyTelegramMethod<Methods>;
|
|
30
|
+
type PreRequest<Methods extends keyof APIMethods = keyof APIMethods> = (ctx: PreRequestContext<Methods>) => MaybePromise<PreRequestContext<Methods>>;
|
|
28
31
|
type OnErrorContext<Ctx extends Context<BotLike>, T extends ErrorDefinitions> = ErrorHandlerParams<Ctx, "TELEGRAM", AnyTelegramError> | ErrorHandlerParams<Ctx, "UNKNOWN", Error> | {
|
|
29
32
|
[K in keyof T]: ErrorHandlerParams<Ctx, K & string, T[K & string]>;
|
|
30
33
|
}[keyof T];
|
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;
|
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
|
|
@@ -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.21",
|
|
4
4
|
"description": "Powerful Telegram Bot API framework",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
"author": "kravets",
|
|
25
25
|
"license": "ISC",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@biomejs/biome": "1.
|
|
28
|
-
"@gramio/types": "^7.1.
|
|
29
|
-
"@types/node": "^20.11.
|
|
30
|
-
"typescript": "^5.
|
|
27
|
+
"@biomejs/biome": "1.6.0",
|
|
28
|
+
"@gramio/types": "^7.1.7",
|
|
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"
|