gramio 0.0.24 → 0.0.26

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # GramIO
2
2
 
3
- Work in progress
3
+ Work in progress.
4
4
 
5
- Currently support Bot API 7.1
5
+ Currently support Bot API 7.2
6
6
 
7
7
  See [Documentation](https://gramio.netlify.app/)
package/dist/bot.d.ts CHANGED
@@ -72,8 +72,13 @@ export declare class Bot<Errors extends ErrorDefinitions = {}, Derives extends D
72
72
  [K in Update]: Awaited<ReturnType<Handler>>;
73
73
  }>;
74
74
  onStart(handler: Hooks.OnStart): this;
75
+ onStop(handler: Hooks.OnStop): this;
75
76
  preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
76
77
  preRequest(handler: Hooks.PreRequest): this;
78
+ onResponse<Methods extends keyof APIMethods, Handler extends Hooks.OnResponse<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
79
+ onResponse(handler: Hooks.OnResponse): this;
80
+ onResponseError<Methods extends keyof APIMethods, Handler extends Hooks.OnResponseError<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
81
+ onResponseError(handler: Hooks.OnResponseError): this;
77
82
  on<T extends UpdateName>(updateName: MaybeArray<T>, handler: Handler<ContextType<typeof this, T> & Derives["global"] & Derives[T]>): this;
78
83
  use(handler: Handler<Context<typeof this> & Derives["global"]>): this;
79
84
  extend<NewPlugin extends Plugin<any, any>>(plugin: MaybePromise<NewPlugin>): Bot<Errors & NewPlugin["Errors"], Derives & NewPlugin["Derives"]>;
package/dist/bot.js CHANGED
@@ -82,6 +82,8 @@ let Bot = (() => {
82
82
  updates = new updates_1.Updates(this, this.errorHandler.bind(this));
83
83
  hooks = {
84
84
  preRequest: [],
85
+ onResponse: [],
86
+ onResponseError: [],
85
87
  onError: [],
86
88
  onStart: [],
87
89
  onStop: [],
@@ -110,11 +112,11 @@ let Bot = (() => {
110
112
  }
111
113
  return data;
112
114
  }
113
- async runImmutableHooks(type, context) {
115
+ async runImmutableHooks(type, ...context) {
114
116
  for await (const hook of this.hooks[type]) {
115
117
  //TODO: solve that later
116
118
  //@ts-expect-error
117
- await hook(context);
119
+ await hook(...context);
118
120
  }
119
121
  }
120
122
  async _callApi(method, params = {}) {
@@ -143,8 +145,20 @@ let Bot = (() => {
143
145
  }
144
146
  const response = await (0, undici_1.request)(url, reqOptions);
145
147
  const data = (await response.body.json());
146
- if (!data.ok)
147
- throw new errors_1.TelegramError(data, method, params);
148
+ if (!data.ok) {
149
+ const err = new errors_1.TelegramError(data, method, params);
150
+ // @ts-expect-error
151
+ this.runImmutableHooks("onResponseError", err, this.api);
152
+ throw err;
153
+ }
154
+ this.runImmutableHooks("onResponse",
155
+ // TODO: fix type error
156
+ // @ts-expect-error
157
+ {
158
+ method,
159
+ params,
160
+ response: data.result,
161
+ });
148
162
  return data.result;
149
163
  }
150
164
  /**
@@ -219,6 +233,10 @@ let Bot = (() => {
219
233
  this.hooks.onStart.push(handler);
220
234
  return this;
221
235
  }
236
+ onStop(handler) {
237
+ this.hooks.onStop.push(handler);
238
+ return this;
239
+ }
222
240
  preRequest(methodsOrHandler, handler) {
223
241
  if (typeof methodsOrHandler === "string" ||
224
242
  Array.isArray(methodsOrHandler)) {
@@ -242,6 +260,48 @@ let Bot = (() => {
242
260
  this.hooks.preRequest.push(methodsOrHandler);
243
261
  return this;
244
262
  }
263
+ onResponse(methodsOrHandler, handler) {
264
+ if (typeof methodsOrHandler === "string" ||
265
+ Array.isArray(methodsOrHandler)) {
266
+ // TODO: error
267
+ if (!handler)
268
+ throw new Error("TODO:");
269
+ const methods = typeof methodsOrHandler === "string"
270
+ ? [methodsOrHandler]
271
+ : methodsOrHandler;
272
+ this.hooks.onResponse.push(async (context) => {
273
+ // TODO: remove ts-ignore
274
+ // @ts-expect-error
275
+ if (methods.includes(context.method))
276
+ return handler(context);
277
+ return context;
278
+ });
279
+ }
280
+ else
281
+ this.hooks.onResponse.push(methodsOrHandler);
282
+ return this;
283
+ }
284
+ onResponseError(methodsOrHandler, handler) {
285
+ if (typeof methodsOrHandler === "string" ||
286
+ Array.isArray(methodsOrHandler)) {
287
+ // TODO: error
288
+ if (!handler)
289
+ throw new Error("TODO:");
290
+ const methods = typeof methodsOrHandler === "string"
291
+ ? [methodsOrHandler]
292
+ : methodsOrHandler;
293
+ this.hooks.onResponseError.push(async (context) => {
294
+ // TODO: remove ts-ignore
295
+ // @ts-expect-error
296
+ if (methods.includes(context.method))
297
+ return handler(context);
298
+ return context;
299
+ });
300
+ }
301
+ else
302
+ this.hooks.onResponseError.push(methodsOrHandler);
303
+ return this;
304
+ }
245
305
  on(updateName, handler) {
246
306
  this.updates.on(updateName, handler);
247
307
  return this;
@@ -277,6 +337,20 @@ let Bot = (() => {
277
337
  else
278
338
  this.preRequest(updateName, preRequest);
279
339
  }
340
+ for (const value of plugin.onResponses) {
341
+ const [onResponse, updateName] = value;
342
+ if (!updateName)
343
+ this.onResponse(onResponse);
344
+ else
345
+ this.onResponse(updateName, onResponse);
346
+ }
347
+ for (const value of plugin.onResponseErrors) {
348
+ const [onResponseError, updateName] = value;
349
+ if (!updateName)
350
+ this.onResponseError(onResponseError);
351
+ else
352
+ this.onResponseError(updateName, onResponseError);
353
+ }
280
354
  for (const handler of plugin.groups) {
281
355
  this.group(handler);
282
356
  }
@@ -323,7 +397,7 @@ let Bot = (() => {
323
397
  });
324
398
  }
325
399
  hears(trigger, handler) {
326
- return this.on("message", (context, next) => {
400
+ return this.on(["message", "business_message"], (context, next) => {
327
401
  if ((typeof trigger === "string" && context.text === trigger) ||
328
402
  // @ts-expect-error
329
403
  (typeof trigger === "function" && trigger(context)) ||
@@ -343,7 +417,7 @@ let Bot = (() => {
343
417
  command(command, handler, options) {
344
418
  if (command.startsWith("/"))
345
419
  throw new Error("Do not use / in command name");
346
- return this.on("message", (context, next) => {
420
+ return this.on(["message", "business_message"], (context, next) => {
347
421
  // TODO: change to find
348
422
  if (context.entities?.some((entity) => {
349
423
  if (entity.type !== "bot_command" || entity.offset > 0)
package/dist/plugin.d.ts CHANGED
@@ -10,6 +10,14 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
10
10
  Hooks.PreRequest<any>,
11
11
  MaybeArray<keyof APIMethods> | undefined
12
12
  ][];
13
+ onResponses: [
14
+ Hooks.OnResponse<any>,
15
+ MaybeArray<keyof APIMethods> | undefined
16
+ ][];
17
+ onResponseErrors: [
18
+ Hooks.OnResponseError<any>,
19
+ MaybeArray<keyof APIMethods> | undefined
20
+ ][];
13
21
  groups: ((bot: Bot<any, any>) => Bot<any, any>)[];
14
22
  name: string;
15
23
  errorsDefinitions: Record<string, {
@@ -37,4 +45,8 @@ export declare class Plugin<Errors extends ErrorDefinitions = {}, Derives extend
37
45
  }>;
38
46
  preRequest<Methods extends keyof APIMethods, Handler extends Hooks.PreRequest<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
39
47
  preRequest(handler: Hooks.PreRequest): this;
48
+ onResponse<Methods extends keyof APIMethods, Handler extends Hooks.OnResponse<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
49
+ onResponse(handler: Hooks.OnResponse): this;
50
+ onResponseError<Methods extends keyof APIMethods, Handler extends Hooks.OnResponseError<Methods>>(methods: MaybeArray<Methods>, handler: Handler): this;
51
+ onResponseError(handler: Hooks.OnResponseError): this;
40
52
  }
package/dist/plugin.js CHANGED
@@ -61,6 +61,8 @@ let Plugin = (() => {
61
61
  Derives;
62
62
  derives = [];
63
63
  preRequests = [];
64
+ onResponses = [];
65
+ onResponseErrors = [];
64
66
  groups = [];
65
67
  name;
66
68
  errorsDefinitions = {};
@@ -102,6 +104,24 @@ let Plugin = (() => {
102
104
  this.preRequests.push([methodsOrHandler, undefined]);
103
105
  return this;
104
106
  }
107
+ onResponse(methodsOrHandler, handler) {
108
+ if ((typeof methodsOrHandler === "string" ||
109
+ Array.isArray(methodsOrHandler)) &&
110
+ handler)
111
+ this.onResponses.push([handler, methodsOrHandler]);
112
+ else if (typeof methodsOrHandler === "function")
113
+ this.onResponses.push([methodsOrHandler, undefined]);
114
+ return this;
115
+ }
116
+ onResponseError(methodsOrHandler, handler) {
117
+ if ((typeof methodsOrHandler === "string" ||
118
+ Array.isArray(methodsOrHandler)) &&
119
+ handler)
120
+ this.onResponseErrors.push([handler, methodsOrHandler]);
121
+ else if (typeof methodsOrHandler === "function")
122
+ this.onResponseErrors.push([methodsOrHandler, undefined]);
123
+ return this;
124
+ }
105
125
  };
106
126
  return Plugin = _classThis;
107
127
  })();
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { BotLike, Context, UpdateName } from "@gramio/contexts";
2
- import type { APIMethodParams, APIMethods, TelegramUser } from "@gramio/types";
2
+ import type { APIMethodParams, APIMethodReturn, APIMethods, TelegramUser } from "@gramio/types";
3
3
  import type { NextMiddleware } from "middleware-io";
4
4
  import type { TelegramError } from "./errors";
5
5
  export interface BotOptions {
@@ -14,15 +14,22 @@ interface ErrorHandlerParams<Ctx extends Context<BotLike>, Kind extends string,
14
14
  kind: Kind;
15
15
  error: Err;
16
16
  }
17
- type AnyTelegramError = {
18
- [APIMethod in keyof APIMethods]: TelegramError<APIMethod>;
19
- }[keyof APIMethods];
17
+ type AnyTelegramError<Methods extends keyof APIMethods = keyof APIMethods> = {
18
+ [APIMethod in Methods]: TelegramError<APIMethod>;
19
+ }[Methods];
20
20
  type AnyTelegramMethod<Methods extends keyof APIMethods> = {
21
21
  [APIMethod in Methods]: {
22
22
  method: APIMethod;
23
23
  params: APIMethodParams<APIMethod>;
24
24
  };
25
25
  }[Methods];
26
+ type AnyTelegramMethodWithReturn<Methods extends keyof APIMethods> = {
27
+ [APIMethod in Methods]: {
28
+ method: APIMethod;
29
+ params: APIMethodParams<APIMethod>;
30
+ response: APIMethodReturn<APIMethod>;
31
+ };
32
+ }[Methods];
26
33
  export type MaybePromise<T> = Promise<T> | T;
27
34
  export declare namespace Hooks {
28
35
  type Derive<Ctx> = (context: Ctx) => MaybePromise<Record<string, unknown>>;
@@ -41,8 +48,12 @@ export declare namespace Hooks {
41
48
  plugins: string[];
42
49
  info: TelegramUser;
43
50
  }) => unknown;
51
+ type OnResponseError<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramError<Methods>, api: BotLike["api"]) => unknown;
52
+ type OnResponse<Methods extends keyof APIMethods = keyof APIMethods> = (context: AnyTelegramMethodWithReturn<Methods>) => unknown;
44
53
  interface Store<T extends ErrorDefinitions> {
45
54
  preRequest: PreRequest[];
55
+ onResponse: OnResponse[];
56
+ onResponseError: OnResponseError[];
46
57
  onError: OnError<T>[];
47
58
  onStart: OnStart[];
48
59
  onStop: OnStop[];
package/dist/updates.js CHANGED
@@ -37,20 +37,20 @@ class Updates {
37
37
  let context = new UpdateContext({
38
38
  bot: this.bot,
39
39
  update: data,
40
- //@ts-expect-error
41
40
  payload: data[updateType],
42
41
  type: updateType,
43
42
  updateId: data.update_id,
44
43
  });
45
44
  if ("isEvent" in context && context.isEvent() && context.eventType) {
45
+ // @ts-expect-error contextsMappings is any
46
46
  context = new contexts_1.contextsMappings[context.eventType]({
47
47
  bot: this.bot,
48
48
  update: data,
49
- //@ts-expect-error
50
49
  payload: data.message ??
51
50
  data.edited_message ??
52
51
  data.channel_post ??
53
- data.edited_channel_post,
52
+ data.edited_channel_post ??
53
+ data.business_message,
54
54
  type: context.eventType,
55
55
  updateId: data.update_id,
56
56
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gramio",
3
- "version": "0.0.24",
3
+ "version": "0.0.26",
4
4
  "description": "Powerful Telegram Bot API framework",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -24,20 +24,20 @@
24
24
  "author": "kravets",
25
25
  "license": "ISC",
26
26
  "devDependencies": {
27
- "@biomejs/biome": "1.6.2",
28
- "@gramio/types": "^7.1.7",
29
- "@types/node": "^20.11.30",
27
+ "@biomejs/biome": "1.6.4",
28
+ "@types/node": "^20.12.4",
30
29
  "typescript": "^5.4.3"
31
30
  },
32
31
  "dependencies": {
33
32
  "@gramio/callback-data": "^0.0.2",
34
- "@gramio/contexts": "^0.0.7",
35
- "@gramio/files": "^0.0.3",
33
+ "@gramio/contexts": "^0.0.8",
34
+ "@gramio/files": "^0.0.4",
36
35
  "@gramio/format": "^0.0.8",
37
- "@gramio/keyboards": "^0.2.2",
36
+ "@gramio/keyboards": "^0.2.3",
37
+ "@gramio/types": "^7.2.1",
38
38
  "inspectable": "^3.0.0",
39
39
  "middleware-io": "^2.8.1",
40
- "undici": "^6.10.1"
40
+ "undici": "^6.11.1"
41
41
  },
42
42
  "files": [
43
43
  "dist"