@codebam/cf-workers-telegram-bot 11.2.1 → 11.3.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.
@@ -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,6 +19,8 @@ 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
@@ -30,6 +32,9 @@ export default class TelegramBot {
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
  }
@@ -114,7 +119,7 @@ export default class TelegramBot {
114
119
  return ':successful_payment' in this.commands ? ':successful_payment' : this.defaultCommand;
115
120
  }
116
121
  // Then check if it's a command starting with /
117
- if (args[0].startsWith('/')) {
122
+ if (args[0]?.startsWith('/')) {
118
123
  const command = args[0].substring(1, args[0].lastIndexOf('@') > -1 ? args[0].lastIndexOf('@') : args[0].length);
119
124
  return command in this.commands ? command : this.defaultCommand;
120
125
  }
@@ -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);
@@ -94,6 +94,9 @@ export default class TelegramExecutionContext {
94
94
  else if (this.update.message?.successful_payment) {
95
95
  return 'successful_payment';
96
96
  }
97
+ else if (this.update.business_connection) {
98
+ return 'business_connection';
99
+ }
97
100
  return '';
98
101
  }
99
102
  /**
@@ -4,6 +4,7 @@ import TelegramMessage from './TelegramMessage.js';
4
4
  import TelegramGuestMessage from './TelegramGuestMessage.js';
5
5
  import TelegramPreCheckoutQuery from './TelegramPreCheckoutQuery.js';
6
6
  import TelegramCallbackQuery from './TelegramCallbackQuery.js';
7
+ import TelegramBusinessConnection from './TelegramBusinessConnection.js';
7
8
  interface PartialTelegramUpdate {
8
9
  update_id?: number;
9
10
  message?: TelegramMessage;
@@ -15,5 +16,6 @@ interface PartialTelegramUpdate {
15
16
  guest_message?: TelegramGuestMessage;
16
17
  pre_checkout_query?: TelegramPreCheckoutQuery;
17
18
  callback_query?: TelegramCallbackQuery;
19
+ business_connection?: TelegramBusinessConnection;
18
20
  }
19
21
  export default PartialTelegramUpdate;
@@ -0,0 +1,10 @@
1
+ import TelegramUser from './TelegramUser.js';
2
+ interface TelegramBusinessConnection {
3
+ id: string;
4
+ user: TelegramUser;
5
+ user_chat_id: number;
6
+ date: number;
7
+ can_reply: boolean;
8
+ is_enabled: boolean;
9
+ }
10
+ export default TelegramBusinessConnection;
@@ -0,0 +1 @@
1
+ export {};
@@ -5,6 +5,7 @@ import TelegramCallbackQuery from './TelegramCallbackQuery.js';
5
5
  import TelegramBusinessMessage from './TelegramBusinessMessage.js';
6
6
  import TelegramGuestMessage from './TelegramGuestMessage.js';
7
7
  import TelegramPreCheckoutQuery from './TelegramPreCheckoutQuery.js';
8
+ import TelegramBusinessConnection from './TelegramBusinessConnection.js';
8
9
  export default class TelegramUpdate {
9
10
  update_id: number;
10
11
  message?: TelegramMessage;
@@ -16,5 +17,6 @@ export default class TelegramUpdate {
16
17
  business_message?: TelegramBusinessMessage;
17
18
  guest_message?: TelegramGuestMessage;
18
19
  pre_checkout_query?: TelegramPreCheckoutQuery;
20
+ business_connection?: TelegramBusinessConnection;
19
21
  constructor(update: PartialTelegramUpdate);
20
22
  }
@@ -16,6 +16,7 @@ export default class TelegramUpdate {
16
16
  // my_chat_member?: TelegramChatMemberUpdated;
17
17
  // chat_member?: TelegramChatMemberUpdated;
18
18
  // chat_join_request: TelegramChatJoinRequest;
19
+ business_connection;
19
20
  constructor(update) {
20
21
  this.update_id = update.update_id ?? 0;
21
22
  this.message = update.message;
@@ -29,6 +30,7 @@ export default class TelegramUpdate {
29
30
  this.callback_query = update.callback_query;
30
31
  // shipping_query = update.shipping_query;
31
32
  this.pre_checkout_query = update.pre_checkout_query;
33
+ this.business_connection = update.business_connection;
32
34
  // poll = update.poll;
33
35
  // poll_answer = update.poll_answer;
34
36
  // my_chat_member = update.my_chat_member;
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.1",
3
+ "version": "11.3.3",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",