Skip to content

Commit da5c1ae

Browse files
committed
update webhook with augment
1 parent d8b632a commit da5c1ae

File tree

1 file changed

+60
-15
lines changed

1 file changed

+60
-15
lines changed

packages/main/src/webhook.ts

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,64 @@
1+
/**
2+
* Webhook class for managing Telegram bot webhook configuration.
3+
* Handles setting up and configuring webhooks for Telegram bots.
4+
*/
15
export default class Webhook {
2-
api: URL;
3-
webhook: URL;
6+
/** Base URL for the Telegram Bot API */
7+
private readonly api: URL;
48

5-
constructor(token: string, request: Request) {
6-
this.api = new URL('https://api.telegram.org/bot' + token);
7-
this.webhook = new URL(new URL(request.url).origin + `/${token}`);
8-
}
9+
/** Webhook URL that Telegram will send updates to */
10+
private readonly webhook: URL;
911

10-
async set() {
11-
const url = new URL(`${this.api.origin}${this.api.pathname}/setWebhook`);
12-
const params = url.searchParams;
13-
params.append('url', this.webhook.toString());
14-
params.append('max_connections', '100');
15-
params.append('allowed_updates', JSON.stringify(['message', 'inline_query', 'business_message', 'business_connection']));
16-
params.append('drop_pending_updates', 'true');
17-
return await fetch(`${url.toString()}?${params.toString()}`);
18-
}
12+
/**
13+
* Creates a new Webhook instance.
14+
*
15+
* @param token - The Telegram bot token
16+
* @param request - The incoming request object used to determine the webhook URL
17+
*/
18+
constructor(token: string, request: Request) {
19+
this.api = new URL(`https://api.telegram.org/bot${token}`);
20+
this.webhook = new URL(`${new URL(request.url).origin}/${token}`);
21+
}
22+
23+
/**
24+
* Sets the webhook URL for the Telegram bot.
25+
*
26+
* @returns Promise that resolves to the fetch response from Telegram
27+
* @throws Will throw an error if the fetch request fails
28+
*/
29+
async set(): Promise<Response> {
30+
const url = new URL(`${this.api.toString()}setWebhook`);
31+
32+
// Configure webhook parameters
33+
const params = new URLSearchParams({
34+
url: this.webhook.toString(),
35+
max_connections: '100',
36+
allowed_updates: JSON.stringify(['message', 'inline_query', 'business_message', 'business_connection']),
37+
drop_pending_updates: 'true',
38+
});
39+
40+
try {
41+
return await fetch(`${url.toString()}?${params.toString()}`);
42+
} catch (error) {
43+
console.error('Failed to set webhook:', error);
44+
throw error;
45+
}
46+
}
47+
48+
/**
49+
* Removes the webhook configuration from Telegram.
50+
*
51+
* @returns Promise that resolves to the fetch response from Telegram
52+
* @throws Will throw an error if the fetch request fails
53+
*/
54+
async delete(): Promise<Response> {
55+
const url = new URL(`${this.api.toString()}deleteWebhook`);
56+
57+
try {
58+
return await fetch(url.toString());
59+
} catch (error) {
60+
console.error('Failed to delete webhook:', error);
61+
throw error;
62+
}
63+
}
1964
}

0 commit comments

Comments
 (0)