-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathhttps.ts
More file actions
32 lines (26 loc) · 957 Bytes
/
Copy pathhttps.ts
File metadata and controls
32 lines (26 loc) · 957 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* This example demonstrates setting up a webhook using a
* self-signed certificate.
*
* Run with: npx tsx examples/webhook/https.ts
*/
import { fileURLToPath } from 'node:url';
import TelegramBot, { type Message } from 'node-telegram-bot-api';
const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN';
const key = fileURLToPath(new URL('../ssl/key.pem', import.meta.url));
const cert = fileURLToPath(new URL('../ssl/crt.pem', import.meta.url));
const bot = new TelegramBot(TOKEN, {
webHook: {
port: 443,
key, // Path to file with PEM private key
cert, // Path to file with PEM certificate
},
});
// This URL must route to the port set above (i.e. 443).
const url = 'https://<PUBLIC-URL>';
// This informs the Telegram servers of the new webhook.
bot.setWebHook(`${url}/bot${TOKEN}`, { certificate: cert });
// Just to ping!
bot.on('message', (msg: Message) => {
bot.sendMessage(msg.chat.id, 'I am alive!');
});