-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
77 lines (69 loc) · 2.06 KB
/
handler.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"use strict";
const axios = require("axios");
const TelegramBot = require('node-telegram-bot-api');
const axiosTelegram = axios.create({
baseURL: `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}`
})
const telegramBot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN);
module.exports.telegramWebhook = async (event) => {
if (event['pathParameters'].token !== process.env.TELEGRAM_BOT_TOKEN)
return { statusCode: 200 }
const data = JSON.parse(event.body);
try {
let response = '';
if ('message' in data) {
const botCommands = [];
if ('entities' in data.message) {
data.message.entities.forEach((entity) => {
if (entity.type === 'bot_command') {
botCommands.push(data.message.text.slice(entity.offset, entity.length));
}
})
}
let botCommand = botCommands[0] || ''
switch (botCommand) {
default:
response = await telegramBot.sendMessage(
data.message['chat'].id,
data.message.text
);
break;
}
}
return {
statusCode: 200,
body: JSON.stringify(response),
};
} catch (e) {
await telegramBot.sendMessage(data.message['chat'].id, e.toString());
return {
statusCode: 200,
body: JSON.stringify({
status: 500,
error: e.toString()
}),
};
}
}
module.exports.telegram = async (event) => {
const response = await axiosTelegram.post(event['method'], {
...event
});
return {
statusCode: 200,
body: JSON.stringify(response.data),
};
}
module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: "Go Serverless v2.0! Your function executed successfully!",
input: event,
},
null,
2
),
};
};