@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.
- package/dist/telegram_bot.d.ts +3 -0
- package/dist/telegram_bot.js +11 -2
- package/dist/telegram_execution_context.js +3 -0
- package/dist/types/PartialTelegramUpdate.d.ts +2 -0
- package/dist/types/TelegramBusinessConnection.d.ts +10 -0
- package/dist/types/TelegramBusinessConnection.js +1 -0
- package/dist/types/TelegramUpdate.d.ts +2 -0
- package/dist/types/TelegramUpdate.js +2 -0
- package/dist/webhook.d.ts +4 -1
- package/dist/webhook.js +13 -5
- package/package.json +1 -1
package/dist/telegram_bot.d.ts
CHANGED
|
@@ -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
|
package/dist/telegram_bot.js
CHANGED
|
@@ -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]
|
|
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);
|
|
@@ -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 @@
|
|
|
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 =
|
|
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()}?${
|
|
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:
|
|
54
|
+
params: params,
|
|
47
55
|
response: json,
|
|
48
56
|
});
|
|
49
57
|
}
|