-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
35 lines (33 loc) · 1.07 KB
/
index.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
32
33
34
35
const mode = process.argv.slice(2)[0] || 'prod' // 'dev' || 'prod'
const TelegramBot = require("node-telegram-bot-api");
const { configuration } = require('./config.js');
const config = configuration[mode]; // dynamically load config
const bot = new TelegramBot(config.token, { polling: mode === 'dev' });
/**
* MESSAGE HANDLER
*/
handleMessage = message => {
// Your code here!
if (message.text) {
// Echos the received message
bot.sendMessage(message.chat.id, `Message received! You said:\n"${message.text}"`);
}
}
/**
* EVENT HANDLERS
*/
if (bot.options.polling) {
// Polling event handlers
bot.on('message', message => { handleMessage(message) });
}
exports.handler = (event, context, callback) => {
// Responding "200 - OK" to telegram servers
callback(null, {
statusCode: 200,
headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true },
body: JSON.stringify({ ok: true }),
});
// Extracting message from JSON
const { body } = event;
handleMessage(JSON.parse(body).message);
}