-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (54 loc) · 1.95 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
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
/*jslint node: true */
/*jshint esversion: 6 */
'use strict';
const commands = require('./commands');
const helper = require('./helper');
const telegramMessageSender = require('./telegramMessageSender');
const DEBUG_MODE = process.env.DEBUG_MODE === 'ON';
/**
* Basic AWS Lambda handler. This is called when Telegram sends any messages to AWS API
* @param event Message from Telegram
* @param context Lambda context
* @returns HTML status response with statusCode 200
*/
exports.handler = (event, context) => {
console.log('starting to process message');
const chatId = helper.getEventChatId(event);
const standardResponse = {
statusCode: 200,
};
if (DEBUG_MODE) {
console.log(event.body);
}
commands.processCommand(event, chatId).then((result) => {
if (DEBUG_MODE) {
console.log('Done, result is: ');
console.log(result);
}
if(result.status === 1) {
var messageSendingPromises = [];
if (result.hasMultipleMessages) {
for (const cid of result.chatIds) {
messageSendingPromises.push(telegramMessageSender.sendMessageToTelegram(cid, result.message));
}
} else {
messageSendingPromises.push(telegramMessageSender.sendMessageToTelegram(chatId, result));
}
Promise.all(messageSendingPromises).then(() => {
context.succeed('Message sent');
}).catch((e) => {
console.error("Failed to send message");
console.log(e);
context.fail(e);
});
} else {
//Not error, but not a success
context.succeed('No need to send message');
}
}).catch((e) => {
console.log("Error handling command");
console.log(e);
context.fail(e);
});
return standardResponse;
};