grammy 1.7.3 → 1.8.2

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.
@@ -31,8 +31,8 @@ exports.InlineKeyboard = exports.Keyboard = void 0;
31
31
  class Keyboard {
32
32
  constructor() {
33
33
  /**
34
- * The nested array that holds the custom keyboard. It will be extended every time
35
- * you call one of the provided methods.
34
+ * The nested array that holds the custom keyboard. It will be extended
35
+ * every time you call one of the provided methods.
36
36
  */
37
37
  this.keyboard = [[]];
38
38
  }
@@ -100,6 +100,17 @@ class Keyboard {
100
100
  requestPoll(text, type) {
101
101
  return this.add({ text, request_poll: { type } });
102
102
  }
103
+ /**
104
+ * Adds a new web app button. The Web App that will be launched when the
105
+ * user presses the button. The Web App will be able to send a
106
+ * “web_app_data” service message. Available in private chats only.
107
+ *
108
+ * @param text Text text to display
109
+ * @param url An HTTPS URL of a Web App to be opened with additional data
110
+ */
111
+ webApp(text, url) {
112
+ return this.add({ text, web_app: { url } });
113
+ }
103
114
  /**
104
115
  * Return the resulting custom keyboard that was built. May be called in the
105
116
  * end if necessary so you can specify more options in `reply_markup`.
@@ -174,22 +185,6 @@ class InlineKeyboard {
174
185
  url(text, url) {
175
186
  return this.add({ text, url });
176
187
  }
177
- /**
178
- * Adds a new login button. This can be used as a replacement for the
179
- * Telegram Login Widget. You must specify an HTTP URL used to automatically
180
- * authorize the user.
181
- *
182
- * @param text The text to display
183
- * @param loginUrl The login URL as string or `LoginUrl` object
184
- */
185
- login(text, loginUrl) {
186
- return this.add({
187
- text,
188
- login_url: typeof loginUrl === "string"
189
- ? { url: loginUrl }
190
- : loginUrl,
191
- });
192
- }
193
188
  /**
194
189
  * Adds a new callback query button. The button contains a text and a custom
195
190
  * payload. This payload will be sent back to your bot when the button is
@@ -211,6 +206,31 @@ class InlineKeyboard {
211
206
  text(text, data = text) {
212
207
  return this.add({ text, callback_data: data });
213
208
  }
209
+ /**
210
+ * Adds a new web app button, confer https://core.telegram.org/bots/webapps
211
+ *
212
+ * @param text The text to display
213
+ * @param url An HTTPS URL of a Web App to be opened with additional data
214
+ */
215
+ webApp(text, url) {
216
+ return this.add({ text, web_app: { url } });
217
+ }
218
+ /**
219
+ * Adds a new login button. This can be used as a replacement for the
220
+ * Telegram Login Widget. You must specify an HTTP URL used to automatically
221
+ * authorize the user.
222
+ *
223
+ * @param text The text to display
224
+ * @param loginUrl The login URL as string or `LoginUrl` object
225
+ */
226
+ login(text, loginUrl) {
227
+ return this.add({
228
+ text,
229
+ login_url: typeof loginUrl === "string"
230
+ ? { url: loginUrl }
231
+ : loginUrl,
232
+ });
233
+ }
214
234
  /**
215
235
  * Adds a new inline query button. Telegram clients will let the user pick a
216
236
  * chat when this button is pressed. This will start an inline query. The
@@ -247,7 +267,8 @@ class InlineKeyboard {
247
267
  return this.add({ text, switch_inline_query_current_chat: query });
248
268
  }
249
269
  /**
250
- * Adds a new game query button, confer https://core.telegram.org/bots/api#games
270
+ * Adds a new game query button, confer
271
+ * https://core.telegram.org/bots/api#games
251
272
  *
252
273
  * This type of button must always be the first button in the first row.
253
274
  *
@@ -257,9 +278,11 @@ class InlineKeyboard {
257
278
  return this.add({ text, callback_game: {} });
258
279
  }
259
280
  /**
260
- * Adds a new payment button, confer https://core.telegram.org/bots/api#payments
281
+ * Adds a new payment button, confer
282
+ * https://core.telegram.org/bots/api#payments
261
283
  *
262
- * This type of button must always be the first button in the first row and can only be used in invoice messages.
284
+ * This type of button must always be the first button in the first row and
285
+ * can only be used in invoice messages.
263
286
  *
264
287
  * @param text The text to display
265
288
  */
@@ -64,13 +64,15 @@ function session(options = {}) {
64
64
  Object.defineProperty(ctx, "session", {
65
65
  get() {
66
66
  if (key === undefined) {
67
- throw new Error("Cannot access session data because the session key was undefined!");
67
+ const msg = undef("access", getSessionKey);
68
+ throw new Error(msg);
68
69
  }
69
70
  return value;
70
71
  },
71
72
  set(v) {
72
73
  if (key === undefined) {
73
- throw new Error("Cannot assign session data because the session key was undefined!");
74
+ const msg = undef("assign", getSessionKey);
75
+ throw new Error(msg);
74
76
  }
75
77
  value = v;
76
78
  },
@@ -132,7 +134,8 @@ function lazySession(options = {}) {
132
134
  async function load() {
133
135
  var _a;
134
136
  if (key === undefined) {
135
- throw new Error("Cannot access lazy session data because the session key was undefined!");
137
+ const msg = undef("access", getSessionKey, { lazy: true });
138
+ throw new Error(msg);
136
139
  }
137
140
  let v = await storage.read(key);
138
141
  if (!fetching)
@@ -162,7 +165,8 @@ function lazySession(options = {}) {
162
165
  },
163
166
  set(v) {
164
167
  if (key === undefined) {
165
- throw new Error("Cannot assign lazy session data because the session key was undefined!");
168
+ const msg = undef("assign", getSessionKey, { lazy: true });
169
+ throw new Error(msg);
166
170
  }
167
171
  wrote = true;
168
172
  fetching = false;
@@ -184,10 +188,20 @@ function lazySession(options = {}) {
184
188
  };
185
189
  }
186
190
  exports.lazySession = lazySession;
191
+ /** Stores session data per chat by default */
187
192
  function defaultGetSessionKey(ctx) {
188
193
  var _a;
189
194
  return (_a = ctx.chat) === null || _a === void 0 ? void 0 : _a.id.toString();
190
195
  }
196
+ /** Returns a useful error message for when the session key is undefined */
197
+ function undef(op, getSessionKey, opts = {}) {
198
+ var _a;
199
+ const lazy = (_a = opts.lazy) !== null && _a !== void 0 ? _a : false;
200
+ const reason = getSessionKey === defaultGetSessionKey
201
+ ? "this update does not belong to a chat, so the session key is undefined"
202
+ : "the custom `getSessionKey` function returned undefined for this update";
203
+ return `Cannot ${op} ${lazy ? "lazy " : ""}session data because ${reason}!`;
204
+ }
191
205
  /**
192
206
  * The memory session storage is a built-in storage adapter that saves your
193
207
  * session data in RAM using a regular JavaScript `Map` object. If you use this
@@ -70,8 +70,8 @@ interface ReqResHandler {
70
70
  */
71
71
  respond: (json: string) => unknown;
72
72
  /**
73
- * Some frameworks (e.g. Deno's std/http `listenAndServe`) assume
74
- * that handler returns something
73
+ * Some frameworks (e.g. Deno's std/http `listenAndServe`) assume that
74
+ * handler returns something
75
75
  */
76
76
  handlerReturn?: any;
77
77
  }
@@ -95,7 +95,7 @@ export declare type FrameworkAdapter = (...args: any[]) => ReqResHandler;
95
95
  * about how to run your bot with webhooks.
96
96
  *
97
97
  * @param bot The bot for which to create a callback
98
- * @param framework An optional string identifying the framework (default: 'express')
98
+ * @param adapter An optional string identifying the framework (default: 'express')
99
99
  * @param onTimeout An optional strategy to handle timeouts (default: 'throw')
100
100
  * @param timeoutMilliseconds An optional number of timeout milliseconds (default: 10_000)
101
101
  */
@@ -24,24 +24,19 @@ const adapters = { ...frameworks_node_js_1.adapters, callback: callbackAdapter }
24
24
  * about how to run your bot with webhooks.
25
25
  *
26
26
  * @param bot The bot for which to create a callback
27
- * @param framework An optional string identifying the framework (default: 'express')
27
+ * @param adapter An optional string identifying the framework (default: 'express')
28
28
  * @param onTimeout An optional strategy to handle timeouts (default: 'throw')
29
29
  * @param timeoutMilliseconds An optional number of timeout milliseconds (default: 10_000)
30
30
  */
31
31
  function webhookCallback(bot, adapter = frameworks_node_js_1.defaultAdapter, onTimeout = "throw", timeoutMilliseconds = 10000) {
32
- let firstUpdate = true;
33
32
  let initialized = false;
34
- let initCall;
35
33
  const server = typeof adapter === "string"
36
34
  ? adapters[adapter]
37
35
  : adapter;
38
36
  return async (...args) => {
39
37
  if (!initialized) {
40
- if (firstUpdate) {
41
- initCall = bot.init();
42
- firstUpdate = false;
43
- }
44
- await initCall;
38
+ // Will dedupe concurrently incoming calls from several updates
39
+ await bot.init();
45
40
  initialized = true;
46
41
  }
47
42
  const { update, respond, end, handlerReturn } = server(...args);