-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
31 lines (23 loc) · 789 Bytes
/
app.js
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
const express = require('express');
const TelegramBot = require('node-telegram-bot-api');
const bodyParser = require('body-parser');
// dependencies
const token = process.env.TOKEN;
const url = 'http://<public-url>';
const port = process.env.PORT;
const bot = new TelegramBot(token);
// this informs the telegram servers of the new webhook
bot.setWebHook('${url}/bot${token}');
const app = express();
// middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post(`/bot${token}`, (req, res) => {
bot.processUpdate(req.body);
res.sendStatus(200);
});
app.listen(port, () => console.log('Express server is listening on port ${port}'));
// just to ping
bot.on('message', msg => {
bot.sendMessage(msg.chat.id, 'I am alive!');
});