gramio 0.0.21 → 0.0.23

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 CHANGED
@@ -1,5 +1,6 @@
1
+ import { CallbackData } from "@gramio/callback-data";
1
2
  import type { Context, ContextType, MaybeArray, UpdateName } from "@gramio/contexts";
2
- import type { APIMethodParams, APIMethods, SetMyCommandsParams, TelegramBotCommand, TelegramUser } from "@gramio/types";
3
+ import type { APIMethodParams, APIMethods, SetMyCommandsParams, TelegramBotCommand, TelegramReactionTypeEmojiEmoji, TelegramUser } from "@gramio/types";
3
4
  import { Plugin } from "./plugin";
4
5
  import type { BotOptions, DeriveDefinitions, ErrorDefinitions, Handler, Hooks, MaybePromise } from "./types";
5
6
  import { Updates } from "./updates";
@@ -75,13 +76,19 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
75
76
  preRequest(handler: Hooks.PreRequest): this;
76
77
  on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
77
78
  use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
78
- extend<NewPlugin extends Plugin>(plugin: MaybePromise<NewPlugin>): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
79
+ extend<NewPlugin extends Plugin<any, any>>(plugin: MaybePromise<NewPlugin>): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
80
+ reaction(trigger: MaybeArray<TelegramReactionTypeEmojiEmoji>, handler: (context: ContextType<typeof this, "message_reaction"> & Derives["global"] & Derives["message_reaction"]) => unknown): this;
81
+ callbackQuery<Trigger extends CallbackData | string | RegExp>(trigger: Trigger, handler: (context: Omit<ContextType<typeof this, "callback_query">, "data"> & Derives["global"] & Derives["callback_query"] & {
82
+ data: Trigger extends CallbackData ? ReturnType<Trigger["unpack"]> : RegExpMatchArray | null;
83
+ }) => unknown): this;
79
84
  hears<Ctx = ContextType<typeof this, "message"> & Derives["global"] & Derives["message"]>(trigger: RegExp | string | ((context: Ctx) => boolean), handler: (context: Ctx & {
80
85
  args: RegExpMatchArray | null;
81
86
  }) => unknown): this;
82
87
  command(command: string, handler: (context: ContextType<typeof this, "message"> & Derives["global"] & Derives["message"] & {
83
88
  args: string | null;
84
89
  }) => unknown, options?: Omit<SetMyCommandsParams, "commands"> & Omit<TelegramBotCommand, "command">): this;
90
+ /** Currently not isolated!!! */
91
+ group(grouped: (bot: typeof this) => Bot<any, any>): Bot<any, any>;
85
92
  start({ webhook, dropPendingUpdates, allowedUpdates, }?: {
86
93
  webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">;
87
94
  dropPendingUpdates?: boolean;
package/dist/bot.js CHANGED
@@ -35,6 +35,7 @@ var __runInitializers = (this && this.__runInitializers) || function (thisArg, i
35
35
  };
36
36
  Object.defineProperty(exports, "__esModule", { value: true });
37
37
  exports.Bot = void 0;
38
+ const callback_data_1 = require("@gramio/callback-data");
38
39
  const files_1 = require("@gramio/files");
39
40
  const format_1 = require("@gramio/format");
40
41
  const inspectable_1 = require("inspectable");
@@ -268,9 +269,58 @@ let Bot = (() => {
268
269
  else
269
270
  this.derive(updateName, derive);
270
271
  }
272
+ for (const value of plugin.preRequests) {
273
+ const [preRequest, updateName] = value;
274
+ if (!updateName)
275
+ this.preRequest(preRequest);
276
+ else
277
+ this.preRequest(updateName, preRequest);
278
+ }
279
+ for (const handler of plugin.groups) {
280
+ this.group(handler);
281
+ }
271
282
  this.dependencies.push(plugin.name);
272
283
  return this;
273
284
  }
285
+ reaction(trigger, handler) {
286
+ const reactions = Array.isArray(trigger) ? trigger : [trigger];
287
+ return this.on("message_reaction", (context, next) => {
288
+ const newReactions = [];
289
+ for (const reaction of context.newReactions) {
290
+ if (reaction.type !== "emoji")
291
+ continue;
292
+ const foundIndex = context.oldReactions.findIndex((oldReaction) => oldReaction.type === "emoji" &&
293
+ oldReaction.emoji === reaction.emoji);
294
+ if (foundIndex === -1) {
295
+ newReactions.push(reaction);
296
+ }
297
+ else {
298
+ // TODO: REFACTOR
299
+ context.oldReactions.splice(foundIndex, 1);
300
+ }
301
+ }
302
+ if (!newReactions.some((x) => x.type === "emoji" && reactions.includes(x.emoji)))
303
+ return next();
304
+ return handler(context);
305
+ });
306
+ }
307
+ callbackQuery(trigger, handler) {
308
+ return this.on("callback_query", (context, next) => {
309
+ if (!context.data)
310
+ return next();
311
+ if (typeof trigger === "string" && context.data !== trigger)
312
+ return next();
313
+ if (trigger instanceof callback_data_1.CallbackData &&
314
+ !trigger.regexp().test(context.data))
315
+ return next();
316
+ if (trigger instanceof RegExp && !trigger.test(context.data))
317
+ return next();
318
+ // @ts-expect-error
319
+ context.data = trigger.unpack(context.data);
320
+ //@ts-expect-error
321
+ return handler(context);
322
+ });
323
+ }
274
324
  hears(trigger, handler) {
275
325
  return this.on("message", (context, next) => {
276
326
  if ((typeof trigger === "string" && context.text === trigger) ||
@@ -310,6 +360,10 @@ let Bot = (() => {
310
360
  return next();
311
361
  });
312
362
  }
363
+ /** Currently not isolated!!! */
364
+ group(grouped) {
365
+ return grouped(this);
366
+ }
313
367
  async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) {
314
368
  await Promise.all(this.lazyloadPlugins.map(async (plugin) => this.extend(await plugin)));
315
369
  this.info = await this.api.getMe();
package/dist/index.d.ts CHANGED
@@ -8,3 +8,4 @@ export * from "@gramio/files";
8
8
  export * from "@gramio/keyboards";
9
9
  export * from "@gramio/types";
10
10
  export * from "@gramio/format";
11
+ export * from "@gramio/callback-data";
package/dist/index.js CHANGED
@@ -26,3 +26,4 @@ __exportStar(require("@gramio/files"), exports);
26
26
  __exportStar(require("@gramio/keyboards"), exports);
27
27
  __exportStar(require("@gramio/types"), exports);
28
28
  __exportStar(require("@gramio/format"), exports);
29
+ __exportStar(require("@gramio/callback-data"), exports);
package/dist/plugin.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { BotLike, 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
+ import type { Bot } from "./bot";
4
5
  export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extends DeriveDefinitions = DeriveDefinitions> {
5
6
  Errors: Errors;
6
7
  Derives: Derives;
@@ -9,6 +10,7 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
9
10
  Hooks.PreRequest<any>,
10
11
  MaybeArray<keyof APIMethods> | undefined
11
12
  ][];
13
+ groups: ((bot: Bot<any, any>) => Bot<any, any>)[];
12
14
  name: string;
13
15
  errorsDefinitions: Record<string, {
14
16
  new (...args: any): any;
@@ -18,6 +20,8 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
18
20
  constructor(name: string, { dependencies }?: {
19
21
  dependencies?: string[];
20
22
  });
23
+ /** Currently not isolated!!! */
24
+ group(grouped: (bot: Bot<Errors, Derives>) => Bot<any, any>): this;
21
25
  /**
22
26
  * Register custom class-error in plugin
23
27
  **/
package/dist/plugin.js CHANGED
@@ -61,6 +61,7 @@ let Plugin = (() => {
61
61
  Derives;
62
62
  derives = [];
63
63
  preRequests = [];
64
+ groups = [];
64
65
  name;
65
66
  errorsDefinitions = {};
66
67
  dependencies = [];
@@ -69,6 +70,11 @@ let Plugin = (() => {
69
70
  if (dependencies)
70
71
  this.dependencies = dependencies;
71
72
  }
73
+ /** Currently not isolated!!! */
74
+ group(grouped) {
75
+ this.groups.push(grouped);
76
+ return this;
77
+ }
72
78
  /**
73
79
  * Register custom class-error in plugin
74
80
  **/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gramio",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "description": "Powerful Telegram Bot API framework",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -24,19 +24,20 @@
24
24
  "author": "kravets",
25
25
  "license": "ISC",
26
26
  "devDependencies": {
27
- "@biomejs/biome": "1.6.0",
27
+ "@biomejs/biome": "1.6.2",
28
28
  "@gramio/types": "^7.1.7",
29
- "@types/node": "^20.11.25",
30
- "typescript": "^5.4.2"
29
+ "@types/node": "^20.11.30",
30
+ "typescript": "^5.4.3"
31
31
  },
32
32
  "dependencies": {
33
+ "@gramio/callback-data": "^0.0.1",
33
34
  "@gramio/contexts": "^0.0.7",
34
35
  "@gramio/files": "^0.0.3",
35
36
  "@gramio/format": "^0.0.8",
36
- "@gramio/keyboards": "^0.2.0",
37
+ "@gramio/keyboards": "^0.2.2",
37
38
  "inspectable": "^3.0.0",
38
39
  "middleware-io": "^2.8.1",
39
- "undici": "^6.7.1"
40
+ "undici": "^6.10.1"
40
41
  },
41
42
  "files": [
42
43
  "dist"