gramio 0.0.22 → 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,7 +76,11 @@ 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;
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");
@@ -281,6 +282,45 @@ let Bot = (() => {
281
282
  this.dependencies.push(plugin.name);
282
283
  return this;
283
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
+ }
284
324
  hears(trigger, handler) {
285
325
  return this.on("message", (context, next) => {
286
326
  if ((typeof trigger === "string" && context.text === trigger) ||
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gramio",
3
- "version": "0.0.22",
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"