grammy 1.19.1 → 1.19.3

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
@@ -257,7 +257,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
257
257
  <td align="center" valign="top" width="11.11%"><a href="https://github.com/binamralamsal"><img src="https://avatars.githubusercontent.com/u/61900781?v=4?s=100" width="100px;" alt="Binamra Lamsal"/><br /><sub><b>Binamra Lamsal</b></sub></a><br /><a href="#ideas-binamralamsal" title="Ideas, Planning, & Feedback">🤔</a></td>
258
258
  <td align="center" valign="top" width="11.11%"><a href="https://github.com/gertminov"><img src="https://avatars.githubusercontent.com/u/78727928?v=4?s=100" width="100px;" alt="gertminov"/><br /><sub><b>gertminov</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/commits?author=gertminov" title="Documentation">📖</a> <a href="#tutorial-gertminov" title="Tutorials">✅</a></td>
259
259
  <td align="center" valign="top" width="11.11%"><a href="https://github.com/amanape"><img src="https://avatars.githubusercontent.com/u/83104063?v=4?s=100" width="100px;" alt="Stephan Psaras"/><br /><sub><b>Stephan Psaras</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/issues?q=author%3Aamanape" title="Bug reports">🐛</a></td>
260
- <td align="center" valign="top" width="11.11%"><a href="https://github.com/shevernitskiy"><img src="https://avatars.githubusercontent.com/u/28886342?v=4?s=100" width="100px;" alt="shevernitskiy"/><br /><sub><b>shevernitskiy</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/issues?q=author%3Ashevernitskiy" title="Bug reports">🐛</a> <a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Ashevernitskiy" title="Reviewed Pull Requests">👀</a></td>
260
+ <td align="center" valign="top" width="11.11%"><a href="https://github.com/shevernitskiy"><img src="https://avatars.githubusercontent.com/u/28886342?v=4?s=100" width="100px;" alt="shevernitskiy"/><br /><sub><b>shevernitskiy</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/issues?q=author%3Ashevernitskiy" title="Bug reports">🐛</a> <a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Ashevernitskiy" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/grammyjs/grammY/commits?author=shevernitskiy" title="Code">💻</a></td>
261
261
  <td align="center" valign="top" width="11.11%"><a href="https://github.com/mrmaster009"><img src="https://avatars.githubusercontent.com/u/81420490?v=4?s=100" width="100px;" alt="mrmaster009"/><br /><sub><b>mrmaster009</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/commits?author=mrmaster009" title="Documentation">📖</a></td>
262
262
  <td align="center" valign="top" width="11.11%"><a href="https://lwjerri.dev/"><img src="https://avatars.githubusercontent.com/u/50290430?v=4?s=100" width="100px;" alt="Andrey Zontov"/><br /><sub><b>Andrey Zontov</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/issues?q=author%3ALWJerri" title="Bug reports">🐛</a> <a href="https://github.com/grammyjs/grammY/commits?author=LWJerri" title="Code">💻</a> <a href="#question-LWJerri" title="Answering Questions">💬</a> <a href="#ideas-LWJerri" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/grammyjs/grammY/commits?author=LWJerri" title="Documentation">📖</a> <a href="#translation-LWJerri" title="Translation">🌍</a></td>
263
263
  <td align="center" valign="top" width="11.11%"><a href="https://github.com/AbbassAlmusawi"><img src="https://avatars.githubusercontent.com/u/73327881?v=4?s=100" width="100px;" alt="Abbass Al-Musawi"/><br /><sub><b>Abbass Al-Musawi</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/commits?author=AbbassAlmusawi" title="Documentation">📖</a></td>
@@ -280,6 +280,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
280
280
  <td align="center" valign="top" width="11.11%"><a href="https://github.com/shaunnope"><img src="https://avatars.githubusercontent.com/u/19631195?v=4?s=100" width="100px;" alt="Sean Yap"/><br /><sub><b>Sean Yap</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/issues?q=author%3Ashaunnope" title="Bug reports">🐛</a> <a href="https://github.com/grammyjs/grammY/commits?author=shaunnope" title="Code">💻</a></td>
281
281
  <td align="center" valign="top" width="11.11%"><a href="https://sergeysolovev.com"><img src="https://avatars.githubusercontent.com/u/5831301?v=4?s=100" width="100px;" alt="Sergey Solovev"/><br /><sub><b>Sergey Solovev</b></sub></a><br /><a href="#ideas-sergeysolovev" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/grammyjs/grammY/pulls?q=is%3Apr+reviewed-by%3Asergeysolovev" title="Reviewed Pull Requests">👀</a></td>
282
282
  <td align="center" valign="top" width="11.11%"><a href="https://github.com/HeySreelal"><img src="https://avatars.githubusercontent.com/u/94184909?v=4?s=100" width="100px;" alt="Sree (Taylor's Version)"/><br /><sub><b>Sree (Taylor's Version)</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/issues?q=author%3AHeySreelal" title="Bug reports">🐛</a> <a href="https://github.com/grammyjs/grammY/commits?author=HeySreelal" title="Code">💻</a></td>
283
+ <td align="center" valign="top" width="11.11%"><a href="http://linkedin.com/in/thecoorum"><img src="https://avatars.githubusercontent.com/u/32096016?v=4?s=100" width="100px;" alt="Yaroslav Vovchenko"/><br /><sub><b>Yaroslav Vovchenko</b></sub></a><br /><a href="https://github.com/grammyjs/grammY/issues?q=author%3Athecoorum" title="Bug reports">🐛</a></td>
283
284
  </tr>
284
285
  </tbody>
285
286
  </table>
@@ -46,21 +46,22 @@ export interface ReqResHandler {
46
46
  */
47
47
  export type FrameworkAdapter = (...args: any[]) => ReqResHandler;
48
48
  export declare const adapters: {
49
- express: FrameworkAdapter;
50
- koa: FrameworkAdapter;
51
- fastify: FrameworkAdapter;
52
- serveHttp: FrameworkAdapter;
53
- "std/http": FrameworkAdapter;
54
- oak: FrameworkAdapter;
55
- http: FrameworkAdapter;
56
- https: FrameworkAdapter;
57
49
  "aws-lambda": FrameworkAdapter;
58
50
  "aws-lambda-async": FrameworkAdapter;
59
51
  azure: FrameworkAdapter;
60
- "next-js": FrameworkAdapter;
61
- sveltekit: FrameworkAdapter;
62
52
  cloudflare: FrameworkAdapter;
63
53
  "cloudflare-mod": FrameworkAdapter;
54
+ express: FrameworkAdapter;
55
+ fastify: FrameworkAdapter;
64
56
  hono: FrameworkAdapter;
57
+ http: FrameworkAdapter;
58
+ https: FrameworkAdapter;
59
+ koa: FrameworkAdapter;
60
+ "next-js": FrameworkAdapter;
61
+ nhttp: FrameworkAdapter;
62
+ oak: FrameworkAdapter;
63
+ serveHttp: FrameworkAdapter;
64
+ "std/http": FrameworkAdapter;
65
+ sveltekit: FrameworkAdapter;
65
66
  worktop: FrameworkAdapter;
66
67
  };
@@ -13,111 +13,6 @@ const unauthorized = () => new Response('"unauthorized"', {
13
13
  status: 401,
14
14
  statusText: WRONG_TOKEN_ERROR,
15
15
  });
16
- /** express web framework */
17
- const express = (req, res) => ({
18
- update: Promise.resolve(req.body),
19
- header: req.header(SECRET_HEADER),
20
- end: () => res.end(),
21
- respond: (json) => {
22
- res.set("Content-Type", "application/json");
23
- res.send(json);
24
- },
25
- unauthorized: () => {
26
- res.status(401).send(WRONG_TOKEN_ERROR);
27
- },
28
- });
29
- /** koa web framework */
30
- const koa = (ctx) => ({
31
- update: Promise.resolve(ctx.request.body),
32
- header: ctx.get(SECRET_HEADER),
33
- end: () => {
34
- ctx.body = "";
35
- },
36
- respond: (json) => {
37
- ctx.set("Content-Type", "application/json");
38
- ctx.response.body = json;
39
- },
40
- unauthorized: () => {
41
- ctx.status = 401;
42
- },
43
- });
44
- /** fastify web framework */
45
- const fastify = (req, reply) => ({
46
- update: Promise.resolve(req.body),
47
- header: req.headers[SECRET_HEADER_LOWERCASE],
48
- end: () => reply.status(200).send(),
49
- respond: (json) => reply.send(json),
50
- unauthorized: () => reply.code(401).send(WRONG_TOKEN_ERROR),
51
- });
52
- const serveHttp = (requestEvent) => ({
53
- update: requestEvent.request.json(),
54
- header: requestEvent.request.headers.get(SECRET_HEADER) || undefined,
55
- end: () => requestEvent.respondWith(ok()),
56
- respond: (json) => requestEvent.respondWith(okJson(json)),
57
- unauthorized: () => requestEvent.respondWith(unauthorized()),
58
- });
59
- /** std/http web server */
60
- const stdHttp = (req) => {
61
- let resolveResponse;
62
- return {
63
- update: req.json(),
64
- header: req.headers.get(SECRET_HEADER) || undefined,
65
- end: () => {
66
- if (resolveResponse)
67
- resolveResponse(ok());
68
- },
69
- respond: (json) => {
70
- if (resolveResponse)
71
- resolveResponse(okJson(json));
72
- },
73
- unauthorized: () => {
74
- if (resolveResponse)
75
- resolveResponse(unauthorized());
76
- },
77
- handlerReturn: new Promise((resolve) => {
78
- resolveResponse = resolve;
79
- }),
80
- };
81
- };
82
- /** oak web framework */
83
- const oak = (ctx) => ({
84
- update: ctx.request.body({ type: "json" }).value,
85
- header: ctx.request.headers.get(SECRET_HEADER) || undefined,
86
- end: () => {
87
- ctx.response.status = 200;
88
- },
89
- respond: (json) => {
90
- ctx.response.type = "json";
91
- ctx.response.body = json;
92
- },
93
- unauthorized: () => {
94
- ctx.response.status = 401;
95
- },
96
- });
97
- /** Node.js native 'http' and 'https' modules */
98
- const http = (req, res) => {
99
- const secretHeaderFromRequest = req.headers[SECRET_HEADER_LOWERCASE];
100
- return {
101
- update: new Promise((resolve, reject) => {
102
- const chunks = [];
103
- req.on("data", (chunk) => chunks.push(chunk))
104
- .once("end", () => {
105
- // @ts-ignore `Buffer` is Node-only
106
- const raw = Buffer.concat(chunks).toString("utf-8");
107
- resolve(JSON.parse(raw));
108
- })
109
- .once("error", reject);
110
- }),
111
- header: Array.isArray(secretHeaderFromRequest)
112
- ? secretHeaderFromRequest[0]
113
- : secretHeaderFromRequest,
114
- end: () => res.end(),
115
- respond: (json) => res
116
- .writeHead(200, { "Content-Type": "application/json" })
117
- .end(json),
118
- unauthorized: () => res.writeHead(401).end(WRONG_TOKEN_ERROR),
119
- };
120
- };
121
16
  /** AWS lambda serverless functions */
122
17
  const awsLambda = (event, _context, callback) => ({
123
18
  update: JSON.parse(event.body),
@@ -164,37 +59,6 @@ const azure = (context, req) => ({
164
59
  context.res.send(401, WRONG_TOKEN_ERROR);
165
60
  },
166
61
  });
167
- /** Next.js Serverless Functions */
168
- const nextJs = (req, res) => ({
169
- update: Promise.resolve(req.body),
170
- header: req.headers[SECRET_HEADER_LOWERCASE],
171
- end: () => res.end(),
172
- respond: (json) => res.status(200).json(json),
173
- unauthorized: () => res.status(401).send(WRONG_TOKEN_ERROR),
174
- });
175
- /** Sveltekit Serverless Functions */
176
- const sveltekit = ({ request }) => {
177
- let resolveResponse;
178
- return {
179
- update: Promise.resolve(request.json()),
180
- header: request.headers.get(SECRET_HEADER) || undefined,
181
- end: () => {
182
- if (resolveResponse)
183
- resolveResponse(ok());
184
- },
185
- respond: (json) => {
186
- if (resolveResponse)
187
- resolveResponse(okJson(json));
188
- },
189
- unauthorized: () => {
190
- if (resolveResponse)
191
- resolveResponse(unauthorized());
192
- },
193
- handlerReturn: new Promise((resolve) => {
194
- resolveResponse = resolve;
195
- }),
196
- };
197
- };
198
62
  /** Native CloudFlare workers (service worker) */
199
63
  const cloudflare = (event) => {
200
64
  let resolveResponse;
@@ -235,6 +99,27 @@ const cloudflareModule = (request) => {
235
99
  }),
236
100
  };
237
101
  };
102
+ /** express web framework */
103
+ const express = (req, res) => ({
104
+ update: Promise.resolve(req.body),
105
+ header: req.header(SECRET_HEADER),
106
+ end: () => res.end(),
107
+ respond: (json) => {
108
+ res.set("Content-Type", "application/json");
109
+ res.send(json);
110
+ },
111
+ unauthorized: () => {
112
+ res.status(401).send(WRONG_TOKEN_ERROR);
113
+ },
114
+ });
115
+ /** fastify web framework */
116
+ const fastify = (req, reply) => ({
117
+ update: Promise.resolve(req.body),
118
+ header: req.headers[SECRET_HEADER_LOWERCASE],
119
+ end: () => reply.status(200).send(),
120
+ respond: (json) => reply.send(json),
121
+ unauthorized: () => reply.code(401).send(WRONG_TOKEN_ERROR),
122
+ });
238
123
  /** hono web framework */
239
124
  const hono = (ctx) => {
240
125
  let resolveResponse;
@@ -258,6 +143,130 @@ const hono = (ctx) => {
258
143
  }),
259
144
  };
260
145
  };
146
+ /** Node.js native 'http' and 'https' modules */
147
+ const http = (req, res) => {
148
+ const secretHeaderFromRequest = req.headers[SECRET_HEADER_LOWERCASE];
149
+ return {
150
+ update: new Promise((resolve, reject) => {
151
+ const chunks = [];
152
+ req.on("data", (chunk) => chunks.push(chunk))
153
+ .once("end", () => {
154
+ // @ts-ignore `Buffer` is Node-only
155
+ const raw = Buffer.concat(chunks).toString("utf-8");
156
+ resolve(JSON.parse(raw));
157
+ })
158
+ .once("error", reject);
159
+ }),
160
+ header: Array.isArray(secretHeaderFromRequest)
161
+ ? secretHeaderFromRequest[0]
162
+ : secretHeaderFromRequest,
163
+ end: () => res.end(),
164
+ respond: (json) => res
165
+ .writeHead(200, { "Content-Type": "application/json" })
166
+ .end(json),
167
+ unauthorized: () => res.writeHead(401).end(WRONG_TOKEN_ERROR),
168
+ };
169
+ };
170
+ /** koa web framework */
171
+ const koa = (ctx) => ({
172
+ update: Promise.resolve(ctx.request.body),
173
+ header: ctx.get(SECRET_HEADER),
174
+ end: () => {
175
+ ctx.body = "";
176
+ },
177
+ respond: (json) => {
178
+ ctx.set("Content-Type", "application/json");
179
+ ctx.response.body = json;
180
+ },
181
+ unauthorized: () => {
182
+ ctx.status = 401;
183
+ },
184
+ });
185
+ /** Next.js Serverless Functions */
186
+ const nextJs = (req, res) => ({
187
+ update: Promise.resolve(req.body),
188
+ header: req.headers[SECRET_HEADER_LOWERCASE],
189
+ end: () => res.end(),
190
+ respond: (json) => res.status(200).json(json),
191
+ unauthorized: () => res.status(401).send(WRONG_TOKEN_ERROR),
192
+ });
193
+ /** nhttp web framework */
194
+ const nhttp = (rev) => ({
195
+ update: rev.body,
196
+ header: rev.headers.get(SECRET_HEADER) || undefined,
197
+ end: () => rev.response.sendStatus(200),
198
+ respond: (json) => rev.response.status(200).send(json),
199
+ unauthorized: () => rev.response.status(401).send(WRONG_TOKEN_ERROR),
200
+ });
201
+ /** oak web framework */
202
+ const oak = (ctx) => ({
203
+ update: ctx.request.body({ type: "json" }).value,
204
+ header: ctx.request.headers.get(SECRET_HEADER) || undefined,
205
+ end: () => {
206
+ ctx.response.status = 200;
207
+ },
208
+ respond: (json) => {
209
+ ctx.response.type = "json";
210
+ ctx.response.body = json;
211
+ },
212
+ unauthorized: () => {
213
+ ctx.response.status = 401;
214
+ },
215
+ });
216
+ /** Deno.serve */
217
+ const serveHttp = (requestEvent) => ({
218
+ update: requestEvent.request.json(),
219
+ header: requestEvent.request.headers.get(SECRET_HEADER) || undefined,
220
+ end: () => requestEvent.respondWith(ok()),
221
+ respond: (json) => requestEvent.respondWith(okJson(json)),
222
+ unauthorized: () => requestEvent.respondWith(unauthorized()),
223
+ });
224
+ /** std/http web server */
225
+ const stdHttp = (req) => {
226
+ let resolveResponse;
227
+ return {
228
+ update: req.json(),
229
+ header: req.headers.get(SECRET_HEADER) || undefined,
230
+ end: () => {
231
+ if (resolveResponse)
232
+ resolveResponse(ok());
233
+ },
234
+ respond: (json) => {
235
+ if (resolveResponse)
236
+ resolveResponse(okJson(json));
237
+ },
238
+ unauthorized: () => {
239
+ if (resolveResponse)
240
+ resolveResponse(unauthorized());
241
+ },
242
+ handlerReturn: new Promise((resolve) => {
243
+ resolveResponse = resolve;
244
+ }),
245
+ };
246
+ };
247
+ /** Sveltekit Serverless Functions */
248
+ const sveltekit = ({ request }) => {
249
+ let resolveResponse;
250
+ return {
251
+ update: Promise.resolve(request.json()),
252
+ header: request.headers.get(SECRET_HEADER) || undefined,
253
+ end: () => {
254
+ if (resolveResponse)
255
+ resolveResponse(ok());
256
+ },
257
+ respond: (json) => {
258
+ if (resolveResponse)
259
+ resolveResponse(okJson(json));
260
+ },
261
+ unauthorized: () => {
262
+ if (resolveResponse)
263
+ resolveResponse(unauthorized());
264
+ },
265
+ handlerReturn: new Promise((resolve) => {
266
+ resolveResponse = resolve;
267
+ }),
268
+ };
269
+ };
261
270
  /** worktop CloudFlare workers framework */
262
271
  const worktop = (req, res) => ({
263
272
  update: Promise.resolve(req.body.json()),
@@ -266,23 +275,24 @@ const worktop = (req, res) => ({
266
275
  respond: (json) => res.send(200, json),
267
276
  unauthorized: () => res.send(401, WRONG_TOKEN_ERROR),
268
277
  });
269
- // Please open a PR if you want to add another adapter
278
+ // Please open a pull request if you want to add another adapter
270
279
  exports.adapters = {
271
- express,
272
- koa,
273
- fastify,
274
- serveHttp,
275
- "std/http": stdHttp,
276
- oak,
277
- http,
278
- https: http,
279
280
  "aws-lambda": awsLambda,
280
281
  "aws-lambda-async": awsLambdaAsync,
281
282
  azure,
282
- "next-js": nextJs,
283
- sveltekit,
284
283
  cloudflare,
285
284
  "cloudflare-mod": cloudflareModule,
285
+ express,
286
+ fastify,
286
287
  hono,
288
+ http,
289
+ https: http,
290
+ koa,
291
+ "next-js": nextJs,
292
+ nhttp,
293
+ oak,
294
+ serveHttp,
295
+ "std/http": stdHttp,
296
+ sveltekit,
287
297
  worktop,
288
298
  };
@@ -64,6 +64,19 @@ export interface ApiClientOptions {
64
64
  * https://api.telegram.org
65
65
  */
66
66
  apiRoot?: string;
67
+ /**
68
+ * Specifies whether to use the [test
69
+ * environment](https://core.telegram.org/bots/webapps#using-bots-in-the-test-environment).
70
+ * Can be either `"prod"` (default) or `"test"`.
71
+ *
72
+ * The testing infrastructure is separate from the regular production
73
+ * infrastructure. No chats, accounts, or other data is shared between them.
74
+ * If you set this option to `"test"`, you will need to make your Telegram
75
+ * client connect to the testing data centers of Telegram, register your
76
+ * phone number again, open a new chat with @BotFather, and create a
77
+ * separate bot.
78
+ */
79
+ environment?: "prod" | "test";
67
80
  /**
68
81
  * URL builder function for API calls. Can be used to modify which API
69
82
  * server should be called.
@@ -71,9 +84,10 @@ export interface ApiClientOptions {
71
84
  * @param root The URL that was passed in `apiRoot`, or its default value
72
85
  * @param token The bot's token that was passed when creating the bot
73
86
  * @param method The API method to be called, e.g. `getMe`
87
+ * @param env The value that was passed in `environment`, or its default value
74
88
  * @returns The URL that will be fetched during the API call
75
89
  */
76
- buildUrl?: (root: string, token: string, method: string) => string | URL;
90
+ buildUrl?: (root: string, token: string, method: string, env: "prod" | "test") => string | URL;
77
91
  /**
78
92
  * Maximum number of seconds that a request to the Bot API server may take.
79
93
  * If a request has not completed before this time has elapsed, grammY
@@ -11,7 +11,7 @@ function concatTransformer(prev, trans) {
11
11
  }
12
12
  class ApiClient {
13
13
  constructor(token, options = {}, webhookReplyEnvelope = {}) {
14
- var _a, _b, _c, _d, _e;
14
+ var _a, _b, _c, _d, _e, _f;
15
15
  this.token = token;
16
16
  this.webhookReplyEnvelope = webhookReplyEnvelope;
17
17
  this.hasUsedWebhookReply = false;
@@ -39,7 +39,7 @@ class ApiClient {
39
39
  const timeout = createTimeout(controller, opts.timeoutSeconds, method);
40
40
  const streamErr = createStreamError(controller);
41
41
  // Build request URL and config
42
- const url = opts.buildUrl(opts.apiRoot, this.token, method);
42
+ const url = opts.buildUrl(opts.apiRoot, this.token, method, opts.environment);
43
43
  const config = formDataRequired
44
44
  ? (0, payload_js_1.createFormDataPayload)(payload, (err) => streamErr.catch(err))
45
45
  : (0, payload_js_1.createJsonPayload)(payload);
@@ -60,16 +60,18 @@ class ApiClient {
60
60
  }
61
61
  };
62
62
  const apiRoot = (_a = options.apiRoot) !== null && _a !== void 0 ? _a : "https://api.telegram.org";
63
+ const environment = (_b = options.environment) !== null && _b !== void 0 ? _b : "prod";
63
64
  this.options = {
64
65
  apiRoot,
65
- buildUrl: (_b = options.buildUrl) !== null && _b !== void 0 ? _b : ((root, token, method) => `${root}/bot${token}/${method}`),
66
- timeoutSeconds: (_c = options.timeoutSeconds) !== null && _c !== void 0 ? _c : 500,
66
+ environment,
67
+ buildUrl: (_c = options.buildUrl) !== null && _c !== void 0 ? _c : defaultBuildUrl,
68
+ timeoutSeconds: (_d = options.timeoutSeconds) !== null && _d !== void 0 ? _d : 500,
67
69
  baseFetchConfig: {
68
70
  ...(0, platform_node_js_1.baseFetchConfig)(apiRoot),
69
71
  ...options.baseFetchConfig,
70
72
  },
71
- canUseWebhookReply: (_d = options.canUseWebhookReply) !== null && _d !== void 0 ? _d : (() => false),
72
- sensitiveLogs: (_e = options.sensitiveLogs) !== null && _e !== void 0 ? _e : false,
73
+ canUseWebhookReply: (_e = options.canUseWebhookReply) !== null && _e !== void 0 ? _e : (() => false),
74
+ sensitiveLogs: (_f = options.sensitiveLogs) !== null && _f !== void 0 ? _f : false,
73
75
  };
74
76
  if (this.options.apiRoot.endsWith("/")) {
75
77
  throw new Error(`Remove the trailing '/' from the 'apiRoot' option (use '${this.options.apiRoot.substring(0, this.options.apiRoot.length - 1)}' instead of '${this.options.apiRoot}')`);
@@ -124,6 +126,10 @@ function createRawApi(token, options, webhookReplyEnvelope) {
124
126
  return api;
125
127
  }
126
128
  exports.createRawApi = createRawApi;
129
+ const defaultBuildUrl = (root, token, method, env) => {
130
+ const prefix = env === "test" ? "test/" : "";
131
+ return `${root}/bot${token}/${prefix}${method}`;
132
+ };
127
133
  const proxyMethods = {
128
134
  set() {
129
135
  return false;
@@ -4,8 +4,7 @@
4
4
  import { Agent as HttpAgent } from "http";
5
5
  import { Agent as HttpsAgent } from "https";
6
6
  import { Readable } from "stream";
7
- import { debug as d } from "debug";
8
- export { d as debug };
7
+ export { debug } from "debug";
9
8
  export declare const itrToStream: (itr: AsyncIterable<Uint8Array>) => Readable;
10
9
  export declare function baseFetchConfig(apiRoot: string): {
11
10
  compress: boolean;
@@ -6,7 +6,7 @@ const http_1 = require("http");
6
6
  const https_1 = require("https");
7
7
  const stream_1 = require("stream");
8
8
  // === Export debug
9
- const debug_1 = require("debug");
9
+ var debug_1 = require("debug");
10
10
  Object.defineProperty(exports, "debug", { enumerable: true, get: function () { return debug_1.debug; } });
11
11
  // === Export system-specific operations
12
12
  // Turn an AsyncIterable<Uint8Array> into a stream
package/out/web.mjs CHANGED
@@ -1919,7 +1919,7 @@ class ApiClient {
1919
1919
  const controller = createAbortControllerFromSignal(signal);
1920
1920
  const timeout = createTimeout(controller, opts.timeoutSeconds, method);
1921
1921
  const streamErr = createStreamError(controller);
1922
- const url = opts.buildUrl(opts.apiRoot, this.token, method);
1922
+ const url = opts.buildUrl(opts.apiRoot, this.token, method, opts.environment);
1923
1923
  const config = formDataRequired ? createFormDataPayload(payload, (err)=>streamErr.catch(err)) : createJsonPayload(payload);
1924
1924
  const sig = controller.signal;
1925
1925
  const options = {
@@ -1941,9 +1941,11 @@ class ApiClient {
1941
1941
  }
1942
1942
  };
1943
1943
  const apiRoot = options.apiRoot ?? "https://api.telegram.org";
1944
+ const environment = options.environment ?? "prod";
1944
1945
  this.options = {
1945
1946
  apiRoot,
1946
- buildUrl: options.buildUrl ?? ((root, token, method)=>`${root}/bot${token}/${method}`),
1947
+ environment,
1948
+ buildUrl: options.buildUrl ?? defaultBuildUrl,
1947
1949
  timeoutSeconds: options.timeoutSeconds ?? 500,
1948
1950
  baseFetchConfig: {
1949
1951
  ...baseFetchConfig(apiRoot),
@@ -1988,6 +1990,10 @@ function createRawApi(token, options, webhookReplyEnvelope) {
1988
1990
  };
1989
1991
  return api;
1990
1992
  }
1993
+ const defaultBuildUrl = (root, token, method, env)=>{
1994
+ const prefix = env === "test" ? "test/" : "";
1995
+ return `${root}/bot${token}/${prefix}${method}`;
1996
+ };
1991
1997
  const proxyMethods = {
1992
1998
  set () {
1993
1999
  return false;
@@ -4151,99 +4157,6 @@ const unauthorized = ()=>new Response('"unauthorized"', {
4151
4157
  status: 401,
4152
4158
  statusText: WRONG_TOKEN_ERROR
4153
4159
  });
4154
- const express = (req, res)=>({
4155
- update: Promise.resolve(req.body),
4156
- header: req.header(SECRET_HEADER),
4157
- end: ()=>res.end(),
4158
- respond: (json)=>{
4159
- res.set("Content-Type", "application/json");
4160
- res.send(json);
4161
- },
4162
- unauthorized: ()=>{
4163
- res.status(401).send(WRONG_TOKEN_ERROR);
4164
- }
4165
- });
4166
- const koa = (ctx)=>({
4167
- update: Promise.resolve(ctx.request.body),
4168
- header: ctx.get(SECRET_HEADER),
4169
- end: ()=>{
4170
- ctx.body = "";
4171
- },
4172
- respond: (json)=>{
4173
- ctx.set("Content-Type", "application/json");
4174
- ctx.response.body = json;
4175
- },
4176
- unauthorized: ()=>{
4177
- ctx.status = 401;
4178
- }
4179
- });
4180
- const fastify = (req, reply)=>({
4181
- update: Promise.resolve(req.body),
4182
- header: req.headers[SECRET_HEADER_LOWERCASE],
4183
- end: ()=>reply.status(200).send(),
4184
- respond: (json)=>reply.send(json),
4185
- unauthorized: ()=>reply.code(401).send(WRONG_TOKEN_ERROR)
4186
- });
4187
- const serveHttp = (requestEvent)=>({
4188
- update: requestEvent.request.json(),
4189
- header: requestEvent.request.headers.get(SECRET_HEADER) || undefined,
4190
- end: ()=>requestEvent.respondWith(ok()),
4191
- respond: (json)=>requestEvent.respondWith(okJson(json)),
4192
- unauthorized: ()=>requestEvent.respondWith(unauthorized())
4193
- });
4194
- const stdHttp = (req)=>{
4195
- let resolveResponse;
4196
- return {
4197
- update: req.json(),
4198
- header: req.headers.get(SECRET_HEADER) || undefined,
4199
- end: ()=>{
4200
- if (resolveResponse) resolveResponse(ok());
4201
- },
4202
- respond: (json)=>{
4203
- if (resolveResponse) resolveResponse(okJson(json));
4204
- },
4205
- unauthorized: ()=>{
4206
- if (resolveResponse) resolveResponse(unauthorized());
4207
- },
4208
- handlerReturn: new Promise((resolve)=>{
4209
- resolveResponse = resolve;
4210
- })
4211
- };
4212
- };
4213
- const oak = (ctx)=>({
4214
- update: ctx.request.body({
4215
- type: "json"
4216
- }).value,
4217
- header: ctx.request.headers.get(SECRET_HEADER) || undefined,
4218
- end: ()=>{
4219
- ctx.response.status = 200;
4220
- },
4221
- respond: (json)=>{
4222
- ctx.response.type = "json";
4223
- ctx.response.body = json;
4224
- },
4225
- unauthorized: ()=>{
4226
- ctx.response.status = 401;
4227
- }
4228
- });
4229
- const http = (req, res)=>{
4230
- const secretHeaderFromRequest = req.headers[SECRET_HEADER_LOWERCASE];
4231
- return {
4232
- update: new Promise((resolve, reject)=>{
4233
- const chunks = [];
4234
- req.on("data", (chunk)=>chunks.push(chunk)).once("end", ()=>{
4235
- const raw = Buffer.concat(chunks).toString("utf-8");
4236
- resolve(JSON.parse(raw));
4237
- }).once("error", reject);
4238
- }),
4239
- header: Array.isArray(secretHeaderFromRequest) ? secretHeaderFromRequest[0] : secretHeaderFromRequest,
4240
- end: ()=>res.end(),
4241
- respond: (json)=>res.writeHead(200, {
4242
- "Content-Type": "application/json"
4243
- }).end(json),
4244
- unauthorized: ()=>res.writeHead(401).end(WRONG_TOKEN_ERROR)
4245
- };
4246
- };
4247
4160
  const awsLambda = (event, _context, callback)=>({
4248
4161
  update: JSON.parse(event.body),
4249
4162
  header: event.headers[SECRET_HEADER],
@@ -4299,32 +4212,6 @@ const azure = (context, req)=>({
4299
4212
  context.res.send(401, WRONG_TOKEN_ERROR);
4300
4213
  }
4301
4214
  });
4302
- const nextJs = (req, res)=>({
4303
- update: Promise.resolve(req.body),
4304
- header: req.headers[SECRET_HEADER_LOWERCASE],
4305
- end: ()=>res.end(),
4306
- respond: (json)=>res.status(200).json(json),
4307
- unauthorized: ()=>res.status(401).send(WRONG_TOKEN_ERROR)
4308
- });
4309
- const sveltekit = ({ request })=>{
4310
- let resolveResponse;
4311
- return {
4312
- update: Promise.resolve(request.json()),
4313
- header: request.headers.get(SECRET_HEADER) || undefined,
4314
- end: ()=>{
4315
- if (resolveResponse) resolveResponse(ok());
4316
- },
4317
- respond: (json)=>{
4318
- if (resolveResponse) resolveResponse(okJson(json));
4319
- },
4320
- unauthorized: ()=>{
4321
- if (resolveResponse) resolveResponse(unauthorized());
4322
- },
4323
- handlerReturn: new Promise((resolve)=>{
4324
- resolveResponse = resolve;
4325
- })
4326
- };
4327
- };
4328
4215
  const cloudflare = (event)=>{
4329
4216
  let resolveResponse;
4330
4217
  event.respondWith(new Promise((resolve)=>{
@@ -4363,6 +4250,25 @@ const cloudflareModule = (request)=>{
4363
4250
  })
4364
4251
  };
4365
4252
  };
4253
+ const express = (req, res)=>({
4254
+ update: Promise.resolve(req.body),
4255
+ header: req.header(SECRET_HEADER),
4256
+ end: ()=>res.end(),
4257
+ respond: (json)=>{
4258
+ res.set("Content-Type", "application/json");
4259
+ res.send(json);
4260
+ },
4261
+ unauthorized: ()=>{
4262
+ res.status(401).send(WRONG_TOKEN_ERROR);
4263
+ }
4264
+ });
4265
+ const fastify = (req, reply)=>({
4266
+ update: Promise.resolve(req.body),
4267
+ header: req.headers[SECRET_HEADER_LOWERCASE],
4268
+ end: ()=>reply.status(200).send(),
4269
+ respond: (json)=>reply.send(json),
4270
+ unauthorized: ()=>reply.code(401).send(WRONG_TOKEN_ERROR)
4271
+ });
4366
4272
  const hono = (ctx)=>{
4367
4273
  let resolveResponse;
4368
4274
  return {
@@ -4385,6 +4291,113 @@ const hono = (ctx)=>{
4385
4291
  })
4386
4292
  };
4387
4293
  };
4294
+ const http = (req, res)=>{
4295
+ const secretHeaderFromRequest = req.headers[SECRET_HEADER_LOWERCASE];
4296
+ return {
4297
+ update: new Promise((resolve, reject)=>{
4298
+ const chunks = [];
4299
+ req.on("data", (chunk)=>chunks.push(chunk)).once("end", ()=>{
4300
+ const raw = Buffer.concat(chunks).toString("utf-8");
4301
+ resolve(JSON.parse(raw));
4302
+ }).once("error", reject);
4303
+ }),
4304
+ header: Array.isArray(secretHeaderFromRequest) ? secretHeaderFromRequest[0] : secretHeaderFromRequest,
4305
+ end: ()=>res.end(),
4306
+ respond: (json)=>res.writeHead(200, {
4307
+ "Content-Type": "application/json"
4308
+ }).end(json),
4309
+ unauthorized: ()=>res.writeHead(401).end(WRONG_TOKEN_ERROR)
4310
+ };
4311
+ };
4312
+ const koa = (ctx)=>({
4313
+ update: Promise.resolve(ctx.request.body),
4314
+ header: ctx.get(SECRET_HEADER),
4315
+ end: ()=>{
4316
+ ctx.body = "";
4317
+ },
4318
+ respond: (json)=>{
4319
+ ctx.set("Content-Type", "application/json");
4320
+ ctx.response.body = json;
4321
+ },
4322
+ unauthorized: ()=>{
4323
+ ctx.status = 401;
4324
+ }
4325
+ });
4326
+ const nextJs = (req, res)=>({
4327
+ update: Promise.resolve(req.body),
4328
+ header: req.headers[SECRET_HEADER_LOWERCASE],
4329
+ end: ()=>res.end(),
4330
+ respond: (json)=>res.status(200).json(json),
4331
+ unauthorized: ()=>res.status(401).send(WRONG_TOKEN_ERROR)
4332
+ });
4333
+ const nhttp = (rev)=>({
4334
+ update: rev.body,
4335
+ header: rev.headers.get(SECRET_HEADER) || undefined,
4336
+ end: ()=>rev.response.sendStatus(200),
4337
+ respond: (json)=>rev.response.status(200).send(json),
4338
+ unauthorized: ()=>rev.response.status(401).send(WRONG_TOKEN_ERROR)
4339
+ });
4340
+ const oak = (ctx)=>({
4341
+ update: ctx.request.body({
4342
+ type: "json"
4343
+ }).value,
4344
+ header: ctx.request.headers.get(SECRET_HEADER) || undefined,
4345
+ end: ()=>{
4346
+ ctx.response.status = 200;
4347
+ },
4348
+ respond: (json)=>{
4349
+ ctx.response.type = "json";
4350
+ ctx.response.body = json;
4351
+ },
4352
+ unauthorized: ()=>{
4353
+ ctx.response.status = 401;
4354
+ }
4355
+ });
4356
+ const serveHttp = (requestEvent)=>({
4357
+ update: requestEvent.request.json(),
4358
+ header: requestEvent.request.headers.get(SECRET_HEADER) || undefined,
4359
+ end: ()=>requestEvent.respondWith(ok()),
4360
+ respond: (json)=>requestEvent.respondWith(okJson(json)),
4361
+ unauthorized: ()=>requestEvent.respondWith(unauthorized())
4362
+ });
4363
+ const stdHttp = (req)=>{
4364
+ let resolveResponse;
4365
+ return {
4366
+ update: req.json(),
4367
+ header: req.headers.get(SECRET_HEADER) || undefined,
4368
+ end: ()=>{
4369
+ if (resolveResponse) resolveResponse(ok());
4370
+ },
4371
+ respond: (json)=>{
4372
+ if (resolveResponse) resolveResponse(okJson(json));
4373
+ },
4374
+ unauthorized: ()=>{
4375
+ if (resolveResponse) resolveResponse(unauthorized());
4376
+ },
4377
+ handlerReturn: new Promise((resolve)=>{
4378
+ resolveResponse = resolve;
4379
+ })
4380
+ };
4381
+ };
4382
+ const sveltekit = ({ request })=>{
4383
+ let resolveResponse;
4384
+ return {
4385
+ update: Promise.resolve(request.json()),
4386
+ header: request.headers.get(SECRET_HEADER) || undefined,
4387
+ end: ()=>{
4388
+ if (resolveResponse) resolveResponse(ok());
4389
+ },
4390
+ respond: (json)=>{
4391
+ if (resolveResponse) resolveResponse(okJson(json));
4392
+ },
4393
+ unauthorized: ()=>{
4394
+ if (resolveResponse) resolveResponse(unauthorized());
4395
+ },
4396
+ handlerReturn: new Promise((resolve)=>{
4397
+ resolveResponse = resolve;
4398
+ })
4399
+ };
4400
+ };
4388
4401
  const worktop = (req, res)=>({
4389
4402
  update: Promise.resolve(req.body.json()),
4390
4403
  header: req.headers.get(SECRET_HEADER),
@@ -4393,22 +4406,23 @@ const worktop = (req, res)=>({
4393
4406
  unauthorized: ()=>res.send(401, WRONG_TOKEN_ERROR)
4394
4407
  });
4395
4408
  const adapters = {
4396
- express,
4397
- koa,
4398
- fastify,
4399
- serveHttp,
4400
- "std/http": stdHttp,
4401
- oak,
4402
- http,
4403
- https: http,
4404
4409
  "aws-lambda": awsLambda,
4405
4410
  "aws-lambda-async": awsLambdaAsync,
4406
4411
  azure,
4407
- "next-js": nextJs,
4408
- sveltekit,
4409
4412
  cloudflare,
4410
4413
  "cloudflare-mod": cloudflareModule,
4414
+ express,
4415
+ fastify,
4411
4416
  hono,
4417
+ http,
4418
+ https: http,
4419
+ koa,
4420
+ "next-js": nextJs,
4421
+ nhttp,
4422
+ oak,
4423
+ serveHttp,
4424
+ "std/http": stdHttp,
4425
+ sveltekit,
4412
4426
  worktop
4413
4427
  };
4414
4428
  const debugErr1 = browser$1("grammy:error");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "grammy",
3
3
  "description": "The Telegram Bot Framework.",
4
- "version": "1.19.1",
4
+ "version": "1.19.3",
5
5
  "author": "KnorpelSenf",
6
6
  "license": "MIT",
7
7
  "engines": {