gramio 0.0.26 → 0.0.27
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 -2
- package/dist/bot.js +43 -2
- package/dist/errors.d.ts +4 -3
- package/dist/plugin.d.ts +3 -3
- package/dist/types.d.ts +35 -6
- package/dist/updates.js +14 -7
- package/package.json +5 -5
package/dist/bot.d.ts
CHANGED
|
@@ -2,14 +2,14 @@ import { CallbackData } from "@gramio/callback-data";
|
|
|
2
2
|
import type { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
3
3
|
import type { APIMethodParams, APIMethods, SetMyCommandsParams, TelegramBotCommand, TelegramReactionTypeEmojiEmoji, TelegramUser } from "@gramio/types";
|
|
4
4
|
import { Plugin } from "./plugin";
|
|
5
|
-
import type { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise } from "./types";
|
|
5
|
+
import type { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise, SuppressedAPIMethods } from "./types";
|
|
6
6
|
import { Updates } from "./updates";
|
|
7
7
|
export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
|
|
8
8
|
/** @internal */
|
|
9
9
|
__Derives: Derives;
|
|
10
10
|
readonly options: BotOptions;
|
|
11
11
|
info: TelegramUser | undefined;
|
|
12
|
-
readonly api:
|
|
12
|
+
readonly api: SuppressedAPIMethods;
|
|
13
13
|
private lazyloadPlugins;
|
|
14
14
|
private dependencies;
|
|
15
15
|
private errorsDefinitions;
|
|
@@ -86,6 +86,16 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
|
|
|
86
86
|
callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context: Omit<ContextType<typeof this, "callback_query">, "data"> & Derives["global"] & Derives["callback_query"] & {
|
|
87
87
|
queryData: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : RegExpMatchArray | null;
|
|
88
88
|
}) => unknown): this;
|
|
89
|
+
chosenInlineResult<Ctx = ContextType<typeof this, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
|
|
90
|
+
args: RegExpMatchArray | null;
|
|
91
|
+
}) => unknown): this;
|
|
92
|
+
inlineQuery<Ctx = ContextType<typeof this, "inline_query"> & Derives["global"] & Derives["inline_query"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
|
|
93
|
+
args: RegExpMatchArray | null;
|
|
94
|
+
}) => unknown, options?: {
|
|
95
|
+
onResult?: (context: ContextType<Bot, "chosen_inline_result"> & Derives["global"] & Derives["chosen_inline_result"] & {
|
|
96
|
+
args: RegExpMatchArray | null;
|
|
97
|
+
}) => unknown;
|
|
98
|
+
}): this;
|
|
89
99
|
hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
|
|
90
100
|
args: RegExpMatchArray | null;
|
|
91
101
|
}) => unknown): this;
|
package/dist/bot.js
CHANGED
|
@@ -149,7 +149,9 @@ let Bot = (() => {
|
|
|
149
149
|
const err = new errors_1.TelegramError(data, method, params);
|
|
150
150
|
// @ts-expect-error
|
|
151
151
|
this.runImmutableHooks("onResponseError", err, this.api);
|
|
152
|
-
|
|
152
|
+
if (!params?.suppress)
|
|
153
|
+
throw err;
|
|
154
|
+
return err;
|
|
153
155
|
}
|
|
154
156
|
this.runImmutableHooks("onResponse",
|
|
155
157
|
// TODO: fix type error
|
|
@@ -396,8 +398,47 @@ let Bot = (() => {
|
|
|
396
398
|
return handler(context);
|
|
397
399
|
});
|
|
398
400
|
}
|
|
401
|
+
chosenInlineResult(trigger, handler) {
|
|
402
|
+
return this.on("chosen_inline_result", (context, next) => {
|
|
403
|
+
if ((typeof trigger === "string" && context.query === trigger) ||
|
|
404
|
+
// @ts-expect-error
|
|
405
|
+
(typeof trigger === "function" && trigger(context)) ||
|
|
406
|
+
(trigger instanceof RegExp &&
|
|
407
|
+
context.query &&
|
|
408
|
+
trigger.test(context.query))) {
|
|
409
|
+
//@ts-expect-error
|
|
410
|
+
context.args =
|
|
411
|
+
trigger instanceof RegExp ? context.query?.match(trigger) : null;
|
|
412
|
+
// TODO: remove
|
|
413
|
+
//@ts-expect-error
|
|
414
|
+
return handler(context);
|
|
415
|
+
}
|
|
416
|
+
return next();
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
inlineQuery(trigger, handler, options = {}) {
|
|
420
|
+
// @ts-expect-error fix later...
|
|
421
|
+
if (options.onResult)
|
|
422
|
+
this.chosenInlineResult(trigger, options.onResult);
|
|
423
|
+
return this.on("inline_query", (context, next) => {
|
|
424
|
+
if ((typeof trigger === "string" && context.query === trigger) ||
|
|
425
|
+
// @ts-expect-error
|
|
426
|
+
(typeof trigger === "function" && trigger(context)) ||
|
|
427
|
+
(trigger instanceof RegExp &&
|
|
428
|
+
context.query &&
|
|
429
|
+
trigger.test(context.query))) {
|
|
430
|
+
//@ts-expect-error
|
|
431
|
+
context.args =
|
|
432
|
+
trigger instanceof RegExp ? context.query?.match(trigger) : null;
|
|
433
|
+
// TODO: remove
|
|
434
|
+
//@ts-expect-error
|
|
435
|
+
return handler(context);
|
|
436
|
+
}
|
|
437
|
+
return next();
|
|
438
|
+
});
|
|
439
|
+
}
|
|
399
440
|
hears(trigger, handler) {
|
|
400
|
-
return this.on(
|
|
441
|
+
return this.on("message", (context, next) => {
|
|
401
442
|
if ((typeof trigger === "string" && context.text === trigger) ||
|
|
402
443
|
// @ts-expect-error
|
|
403
444
|
(typeof trigger === "function" && trigger(context)) ||
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { APIMethods, TelegramAPIResponseError, TelegramResponseParameters } from "@gramio/types";
|
|
2
|
+
import type { MaybeSuppressedParams } from "./types";
|
|
2
3
|
export declare const ErrorKind: unique symbol;
|
|
3
4
|
export declare class TelegramError<T extends keyof APIMethods> extends Error {
|
|
4
5
|
method: T;
|
|
5
|
-
params:
|
|
6
|
+
params: MaybeSuppressedParams<T>;
|
|
6
7
|
code: number;
|
|
7
8
|
payload?: TelegramResponseParameters;
|
|
8
|
-
constructor(error: TelegramAPIResponseError, method: T, params:
|
|
9
|
+
constructor(error: TelegramAPIResponseError, method: T, params: MaybeSuppressedParams<T>);
|
|
9
10
|
}
|
package/dist/plugin.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
|
|
2
2
|
import type { APIMethods } from "@gramio/types";
|
|
3
3
|
import type { DeriveDefinitions, ErrorDefinitions, Hooks } from "./types";
|
|
4
4
|
import type { Bot } from "./bot";
|
|
@@ -37,10 +37,10 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
|
|
|
37
37
|
new (...args: any): any;
|
|
38
38
|
prototype: Error;
|
|
39
39
|
}>(kind: Name, error: NewError): Plugin<Errors & { [name in Name]: InstanceType<NewError>; }, Derives>;
|
|
40
|
-
derive<Handler extends Hooks.Derive<Context<
|
|
40
|
+
derive<Handler extends Hooks.Derive<Context<Bot>>>(handler: Handler): Plugin<Errors, Derives & {
|
|
41
41
|
global: Awaited<ReturnType<Handler>>;
|
|
42
42
|
}>;
|
|
43
|
-
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<
|
|
43
|
+
derive<Update extends UpdateName, Handler extends Hooks.Derive<ContextType<Bot, Update>>>(updateName: MaybeArray<Update>, handler: Handler): Plugin<Errors, Derives & {
|
|
44
44
|
[K in Update]: Awaited<ReturnType<Handler>>;
|
|
45
45
|
}>;
|
|
46
46
|
preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Context, UpdateName } from "@gramio/contexts";
|
|
2
2
|
import type { APIMethodParams, APIMethodReturn, APIMethods, TelegramUser } from "@gramio/types";
|
|
3
3
|
import type { NextMiddleware } from "middleware-io";
|
|
4
|
+
import type { Bot } from "./bot";
|
|
4
5
|
import type { TelegramError } from "./errors";
|
|
5
6
|
export interface BotOptions {
|
|
6
7
|
token?: string;
|
|
@@ -9,7 +10,7 @@ export interface BotOptions {
|
|
|
9
10
|
};
|
|
10
11
|
}
|
|
11
12
|
export type Handler<T> = (context: T, next: NextMiddleware) => unknown;
|
|
12
|
-
interface ErrorHandlerParams<Ctx extends Context<
|
|
13
|
+
interface ErrorHandlerParams<Ctx extends Context<Bot>, Kind extends string, Err> {
|
|
13
14
|
context: Ctx;
|
|
14
15
|
kind: Kind;
|
|
15
16
|
error: Err;
|
|
@@ -20,9 +21,37 @@ type AnyTelegramError<Methods extends keyof APIMethods = keyof APIMethods> = {
|
|
|
20
21
|
type AnyTelegramMethod<Methods extends keyof APIMethods> = {
|
|
21
22
|
[APIMethod in Methods]: {
|
|
22
23
|
method: APIMethod;
|
|
23
|
-
params:
|
|
24
|
+
params: MaybeSuppressedParams<APIMethod>;
|
|
24
25
|
};
|
|
25
26
|
}[Methods];
|
|
27
|
+
export interface Suppress<IsSuppressed extends boolean | undefined = undefined> {
|
|
28
|
+
/**
|
|
29
|
+
* Pass `true` if you want to suppress throwing errors of this method.
|
|
30
|
+
*
|
|
31
|
+
* **But this does not undo getting into the `onResponseError` hook**.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const response = await bot.api.sendMessage({
|
|
36
|
+
* suppress: true,
|
|
37
|
+
* chat_id: "@not_found",
|
|
38
|
+
* text: "Suppressed method"
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* if(response instanceof TelegramError) console.error("sendMessage returns an error...")
|
|
42
|
+
* else console.log("Message has been sent successfully");
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* */
|
|
46
|
+
suppress?: IsSuppressed;
|
|
47
|
+
}
|
|
48
|
+
export type MaybeSuppressedParams<Method extends keyof APIMethods, IsSuppressed extends boolean | undefined = undefined> = APIMethodParams<Method> & Suppress<IsSuppressed>;
|
|
49
|
+
export type SuppressedAPIMethodParams<Method extends keyof APIMethods> = undefined extends APIMethodParams<Method> ? Suppress<true> : MaybeSuppressedParams<Method, true>;
|
|
50
|
+
type MaybeSuppressedReturn<Method extends keyof APIMethods, IsSuppressed extends boolean | undefined = undefined> = true extends IsSuppressed ? TelegramError<Method> | APIMethodReturn<Method> : APIMethodReturn<Method>;
|
|
51
|
+
export type SuppressedAPIMethodReturn<Method extends keyof APIMethods> = MaybeSuppressedReturn<Method, true>;
|
|
52
|
+
export type SuppressedAPIMethods<Methods extends keyof APIMethods = keyof APIMethods> = {
|
|
53
|
+
[APIMethod in Methods]: APIMethodParams<APIMethod> extends undefined ? <IsSuppressed extends boolean | undefined = undefined>(params?: Suppress<IsSuppressed>) => Promise<MaybeSuppressedReturn<APIMethod, IsSuppressed>> : undefined extends APIMethodParams<APIMethod> ? <IsSuppressed extends boolean | undefined = undefined>(params?: MaybeSuppressedParams<APIMethod, IsSuppressed>) => Promise<MaybeSuppressedReturn<APIMethod, IsSuppressed>> : <IsSuppressed extends boolean | undefined = undefined>(params: MaybeSuppressedParams<APIMethod, IsSuppressed>) => Promise<MaybeSuppressedReturn<APIMethod, IsSuppressed>>;
|
|
54
|
+
};
|
|
26
55
|
type AnyTelegramMethodWithReturn<Methods extends keyof APIMethods> = {
|
|
27
56
|
[APIMethod in Methods]: {
|
|
28
57
|
method: APIMethod;
|
|
@@ -35,10 +64,10 @@ export declare namespace Hooks {
|
|
|
35
64
|
type Derive<Ctx> = (context: Ctx) => MaybePromise<Record<string, unknown>>;
|
|
36
65
|
type PreRequestContext<Methods extends keyof APIMethods> = AnyTelegramMethod<Methods>;
|
|
37
66
|
type PreRequest<Methods extends keyof APIMethods = keyof APIMethods> = (ctx: PreRequestContext<Methods>) => MaybePromise<PreRequestContext<Methods>>;
|
|
38
|
-
type OnErrorContext<Ctx extends Context<
|
|
67
|
+
type OnErrorContext<Ctx extends Context<Bot>, T extends ErrorDefinitions> = ErrorHandlerParams<Ctx, "TELEGRAM", AnyTelegramError> | ErrorHandlerParams<Ctx, "UNKNOWN", Error> | {
|
|
39
68
|
[K in keyof T]: ErrorHandlerParams<Ctx, K & string, T[K & string]>;
|
|
40
69
|
}[keyof T];
|
|
41
|
-
type OnError<T extends ErrorDefinitions, Ctx extends Context<
|
|
70
|
+
type OnError<T extends ErrorDefinitions, Ctx extends Context<any> = Context<Bot>> = (options: OnErrorContext<Ctx, T>) => unknown;
|
|
42
71
|
type OnStart = (context: {
|
|
43
72
|
plugins: string[];
|
|
44
73
|
info: TelegramUser;
|
|
@@ -48,7 +77,7 @@ export declare namespace Hooks {
|
|
|
48
77
|
plugins: string[];
|
|
49
78
|
info: TelegramUser;
|
|
50
79
|
}) => unknown;
|
|
51
|
-
type OnResponseError<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramError<Methods>, api:
|
|
80
|
+
type OnResponseError<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramError<Methods>, api: Bot["api"]) => unknown;
|
|
52
81
|
type OnResponse<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramMethodWithReturn<Methods>) => unknown;
|
|
53
82
|
interface Store<T extends ErrorDefinitions> {
|
|
54
83
|
preRequest: PreRequest[];
|
package/dist/updates.js
CHANGED
|
@@ -33,24 +33,31 @@ class Updates {
|
|
|
33
33
|
const UpdateContext = contexts_1.contextsMappings[updateType];
|
|
34
34
|
if (!UpdateContext)
|
|
35
35
|
throw new Error(updateType);
|
|
36
|
+
const updatePayload = data[updateType];
|
|
37
|
+
if (!updatePayload)
|
|
38
|
+
throw new Error("");
|
|
36
39
|
try {
|
|
37
40
|
let context = new UpdateContext({
|
|
38
41
|
bot: this.bot,
|
|
39
42
|
update: data,
|
|
40
|
-
|
|
43
|
+
// @ts-expect-error
|
|
44
|
+
payload: updatePayload,
|
|
41
45
|
type: updateType,
|
|
42
46
|
updateId: data.update_id,
|
|
43
47
|
});
|
|
44
48
|
if ("isEvent" in context && context.isEvent() && context.eventType) {
|
|
45
|
-
|
|
49
|
+
const payload = data.message ??
|
|
50
|
+
data.edited_message ??
|
|
51
|
+
data.channel_post ??
|
|
52
|
+
data.edited_channel_post ??
|
|
53
|
+
data.business_message;
|
|
54
|
+
if (!payload)
|
|
55
|
+
throw new Error("Unsupported event??");
|
|
46
56
|
context = new contexts_1.contextsMappings[context.eventType]({
|
|
47
57
|
bot: this.bot,
|
|
48
58
|
update: data,
|
|
49
|
-
payload
|
|
50
|
-
|
|
51
|
-
data.channel_post ??
|
|
52
|
-
data.edited_channel_post ??
|
|
53
|
-
data.business_message,
|
|
59
|
+
payload,
|
|
60
|
+
// @ts-expect-error
|
|
54
61
|
type: context.eventType,
|
|
55
62
|
updateId: data.update_id,
|
|
56
63
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.27",
|
|
4
4
|
"description": "Powerful Telegram Bot API framework",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -25,15 +25,15 @@
|
|
|
25
25
|
"license": "ISC",
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@biomejs/biome": "1.6.4",
|
|
28
|
-
"@types/node": "^20.12.
|
|
29
|
-
"typescript": "^5.4.
|
|
28
|
+
"@types/node": "^20.12.5",
|
|
29
|
+
"typescript": "^5.4.4"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@gramio/callback-data": "^0.0.2",
|
|
33
|
-
"@gramio/contexts": "^0.0.
|
|
33
|
+
"@gramio/contexts": "^0.0.9",
|
|
34
34
|
"@gramio/files": "^0.0.4",
|
|
35
35
|
"@gramio/format": "^0.0.8",
|
|
36
|
-
"@gramio/keyboards": "^0.
|
|
36
|
+
"@gramio/keyboards": "^0.3.0",
|
|
37
37
|
"@gramio/types": "^7.2.1",
|
|
38
38
|
"inspectable": "^3.0.0",
|
|
39
39
|
"middleware-io": "^2.8.1",
|