gramio 0.0.22 → 0.0.24

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
+ queryData: 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;
@@ -84,9 +89,12 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
84
89
  }) => unknown, options?: Omit<SetMyCommandsParams, "commands"> & Omit<TelegramBotCommand, "command">): this;
85
90
  /** Currently not isolated!!! */
86
91
  group(grouped: (bot: typeof this) => Bot<any, any>): Bot<any, any>;
92
+ init(): Promise<void>;
87
93
  start({ webhook, dropPendingUpdates, allowedUpdates, }?: {
88
94
  webhook?: Omit<APIMethodParams<"setWebhook">, "drop_pending_updates" | "allowed_updates">;
89
95
  dropPendingUpdates?: boolean;
90
96
  allowedUpdates?: NonNullable<APIMethodParams<"getUpdates">>["allowed_updates"];
91
- }): Promise<TelegramUser>;
97
+ }): Promise<TelegramUser | undefined>;
98
+ /** Currently does not implement graceful shutdown */
99
+ stop(): Promise<void>;
92
100
  }
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");
@@ -83,6 +84,7 @@ let Bot = (() => {
83
84
  preRequest: [],
84
85
  onError: [],
85
86
  onStart: [],
87
+ onStop: [],
86
88
  };
87
89
  constructor(token, options) {
88
90
  if (!token || typeof token !== "string")
@@ -281,6 +283,45 @@ let Bot = (() => {
281
283
  this.dependencies.push(plugin.name);
282
284
  return this;
283
285
  }
286
+ reaction(trigger, handler) {
287
+ const reactions = Array.isArray(trigger) ? trigger : [trigger];
288
+ return this.on("message_reaction", (context, next) => {
289
+ const newReactions = [];
290
+ for (const reaction of context.newReactions) {
291
+ if (reaction.type !== "emoji")
292
+ continue;
293
+ const foundIndex = context.oldReactions.findIndex((oldReaction) => oldReaction.type === "emoji" &&
294
+ oldReaction.emoji === reaction.emoji);
295
+ if (foundIndex === -1) {
296
+ newReactions.push(reaction);
297
+ }
298
+ else {
299
+ // TODO: REFACTOR
300
+ context.oldReactions.splice(foundIndex, 1);
301
+ }
302
+ }
303
+ if (!newReactions.some((x) => x.type === "emoji" && reactions.includes(x.emoji)))
304
+ return next();
305
+ return handler(context);
306
+ });
307
+ }
308
+ callbackQuery(trigger, handler) {
309
+ return this.on("callback_query", (context, next) => {
310
+ if (!context.data)
311
+ return next();
312
+ if (typeof trigger === "string" && context.data !== trigger)
313
+ return next();
314
+ if (trigger instanceof callback_data_1.CallbackData &&
315
+ !trigger.regexp().test(context.data))
316
+ return next();
317
+ if (trigger instanceof RegExp && !trigger.test(context.data))
318
+ return next();
319
+ // @ts-expect-error
320
+ context.queryData = trigger.unpack(context.data);
321
+ //@ts-expect-error
322
+ return handler(context);
323
+ });
324
+ }
284
325
  hears(trigger, handler) {
285
326
  return this.on("message", (context, next) => {
286
327
  if ((typeof trigger === "string" && context.text === trigger) ||
@@ -324,9 +365,12 @@ let Bot = (() => {
324
365
  group(grouped) {
325
366
  return grouped(this);
326
367
  }
327
- async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) {
368
+ async init() {
328
369
  await Promise.all(this.lazyloadPlugins.map(async (plugin) => this.extend(await plugin)));
329
370
  this.info = await this.api.getMe();
371
+ }
372
+ async start({ webhook, dropPendingUpdates, allowedUpdates, } = {}) {
373
+ await this.init();
330
374
  if (!webhook) {
331
375
  await this.api.deleteWebhook({
332
376
  drop_pending_updates: dropPendingUpdates,
@@ -336,6 +380,7 @@ let Bot = (() => {
336
380
  });
337
381
  this.runImmutableHooks("onStart", {
338
382
  plugins: this.dependencies,
383
+ // biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
339
384
  info: this.info,
340
385
  updatesFrom: "long-polling",
341
386
  });
@@ -350,11 +395,24 @@ let Bot = (() => {
350
395
  });
351
396
  this.runImmutableHooks("onStart", {
352
397
  plugins: this.dependencies,
398
+ // biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
353
399
  info: this.info,
354
400
  updatesFrom: "webhook",
355
401
  });
356
402
  return this.info;
357
403
  }
404
+ /** Currently does not implement graceful shutdown */
405
+ async stop() {
406
+ if (this.updates.isStarted)
407
+ this.updates.stopPolling();
408
+ else
409
+ await this.api.deleteWebhook();
410
+ await this.runImmutableHooks("onStop", {
411
+ plugins: this.dependencies,
412
+ // biome-ignore lint/style/noNonNullAssertion: bot.init() guarantees this.info
413
+ info: this.info,
414
+ });
415
+ }
358
416
  };
359
417
  return Bot = _classThis;
360
418
  })();
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/types.d.ts CHANGED
@@ -37,10 +37,15 @@ export declare namespace Hooks {
37
37
  info: TelegramUser;
38
38
  updatesFrom: "webhook" | "long-polling";
39
39
  }) => unknown;
40
+ type OnStop = (context: {
41
+ plugins: string[];
42
+ info: TelegramUser;
43
+ }) => unknown;
40
44
  interface Store<T extends ErrorDefinitions> {
41
45
  preRequest: PreRequest[];
42
46
  onError: OnError<T>[];
43
47
  onStart: OnStart[];
48
+ onStop: OnStop[];
44
49
  }
45
50
  }
46
51
  export type ErrorDefinitions = Record<string, Error>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gramio",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
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.2",
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"