@codebam/cf-workers-telegram-bot 11.2.0 → 11.3.1

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.
@@ -19,6 +19,8 @@ export default class TelegramBot {
19
19
  currentContext: TelegramExecutionContext;
20
20
  /** Default command to use when no matching command is found */
21
21
  defaultCommand: string;
22
+ /** Optional secret token for webhook verification */
23
+ secretToken?: string;
22
24
  /**
23
25
  * Create a bot
24
26
  * @param token - the telegram secret token
@@ -26,6 +28,7 @@ export default class TelegramBot {
26
28
  */
27
29
  constructor(token: string, options?: {
28
30
  defaultCommand?: string;
31
+ secretToken?: string;
29
32
  });
30
33
  /**
31
34
  * Register a function on the bot
@@ -19,17 +19,22 @@ export default class TelegramBot {
19
19
  currentContext;
20
20
  /** Default command to use when no matching command is found */
21
21
  defaultCommand = ':message';
22
+ /** Optional secret token for webhook verification */
23
+ secretToken;
22
24
  /**
23
25
  * Create a bot
24
26
  * @param token - the telegram secret token
25
27
  * @param options - optional configuration for the bot
26
28
  */
27
29
  constructor(token, options) {
28
- this.token = token;
29
- this.api = new URL('https://api.telegram.org/bot' + token);
30
+ this.token = token.trim();
31
+ this.api = new URL('https://api.telegram.org/bot' + this.token);
30
32
  if (options?.defaultCommand) {
31
33
  this.defaultCommand = options.defaultCommand;
32
34
  }
35
+ if (options?.secretToken) {
36
+ this.secretToken = options.secretToken;
37
+ }
33
38
  // Register default handler for the default command to avoid errors
34
39
  this.commands[this.defaultCommand] = () => Promise.resolve(new Response('Command not implemented'));
35
40
  }
@@ -125,7 +130,7 @@ export default class TelegramBot {
125
130
  * @param request - the request to handle
126
131
  */
127
132
  async handle(request) {
128
- this.webhook = new Webhook(this.token, request);
133
+ this.webhook = new Webhook(this.token, request, this.secretToken);
129
134
  const url = new URL(request.url);
130
135
  // Check if the request is for this bot
131
136
  if (!url.pathname.startsWith(`/${this.token}`)) {
@@ -135,6 +140,10 @@ export default class TelegramBot {
135
140
  switch (request.method) {
136
141
  case 'POST': {
137
142
  try {
143
+ // Verify secret token if configured
144
+ if (this.secretToken && request.headers.get('X-Telegram-Bot-Api-Secret-Token') !== this.secretToken) {
145
+ return new Response('Unauthorized', { status: 403 });
146
+ }
138
147
  this.update = await request.json();
139
148
  console.log(this.update);
140
149
  const ctx = new TelegramExecutionContext(this, this.update);
package/dist/webhook.d.ts CHANGED
@@ -7,13 +7,16 @@ export default class Webhook {
7
7
  private readonly api;
8
8
  /** Webhook URL that Telegram will send updates to */
9
9
  private readonly webhook;
10
+ /** Secret token to be sent in the X-Telegram-Bot-Api-Secret-Token header */
11
+ private readonly secretToken?;
10
12
  /**
11
13
  * Creates a new Webhook instance.
12
14
  *
13
15
  * @param token - The Telegram bot token
14
16
  * @param request - The incoming request object used to determine the webhook URL
17
+ * @param secretToken - Optional secret token for webhook verification
15
18
  */
16
- constructor(token: string, request: Request);
19
+ constructor(token: string, request: Request, secretToken?: string);
17
20
  /**
18
21
  * Sets the webhook URL for the Telegram bot.
19
22
  *
package/dist/webhook.js CHANGED
@@ -7,15 +7,19 @@ export default class Webhook {
7
7
  api;
8
8
  /** Webhook URL that Telegram will send updates to */
9
9
  webhook;
10
+ /** Secret token to be sent in the X-Telegram-Bot-Api-Secret-Token header */
11
+ secretToken;
10
12
  /**
11
13
  * Creates a new Webhook instance.
12
14
  *
13
15
  * @param token - The Telegram bot token
14
16
  * @param request - The incoming request object used to determine the webhook URL
17
+ * @param secretToken - Optional secret token for webhook verification
15
18
  */
16
- constructor(token, request) {
19
+ constructor(token, request, secretToken) {
17
20
  this.api = new URL(`https://api.telegram.org/bot${token}`);
18
21
  this.webhook = new URL(`${new URL(request.url).origin}/${token}`);
22
+ this.secretToken = secretToken;
19
23
  }
20
24
  /**
21
25
  * Sets the webhook URL for the Telegram bot.
@@ -27,14 +31,18 @@ export default class Webhook {
27
31
  const baseUrl = this.api.toString();
28
32
  const url = new URL(`${baseUrl}${baseUrl.endsWith('/') ? '' : '/'}setWebhook`);
29
33
  // Configure webhook parameters
30
- const params = new URLSearchParams({
34
+ const params = {
31
35
  url: this.webhook.toString(),
32
36
  max_connections: '40',
33
37
  allowed_updates: JSON.stringify(['message', 'inline_query', 'guest_message', 'business_message', 'business_connection']),
34
38
  drop_pending_updates: 'true',
35
- });
39
+ };
40
+ if (this.secretToken) {
41
+ params.secret_token = this.secretToken;
42
+ }
43
+ const searchParams = new URLSearchParams(params);
36
44
  try {
37
- const response = await fetch(`${url.toString()}?${params.toString()}`);
45
+ const response = await fetch(`${url.toString()}?${searchParams.toString()}`);
38
46
  if (response.status !== 200) {
39
47
  throw new Error(`Telegram API error (setWebhook): ${String(response.status)} ${response.statusText}`);
40
48
  }
@@ -43,7 +51,7 @@ export default class Webhook {
43
51
  const json = await cloned.json();
44
52
  console.log({
45
53
  method: 'setWebhook',
46
- params: Object.fromEntries(params),
54
+ params: params,
47
55
  response: json,
48
56
  });
49
57
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "11.2.0",
3
+ "version": "11.3.1",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",