-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
109 lines (98 loc) · 2.78 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
/**
* Requires (Custom Modules)
*/
const rp = require('request-promise');
const helper = require('./helper');
const commands = require('./commands');
const telegramToken = require('./token');
/**
* Send message to Telegram
*
* @param {int} chatId Chat ID
* @param {string} message Message to send
*
* @return {object} Request Promise
*/
function sendMessageToTelegram(chatId, message) {
return rp({
method: 'POST',
uri: `https://api.telegram.org/bot${telegramToken}/sendMessage`,
form: {
chat_id: chatId,
text: message,
parse_mode: 'HTML',
},
});
}
/**
* Process Commands
*
* @param {object} message AWS Lambda Event
*
* @return {object} Request Promise
*/
function processCommands(message) {
if (message) {
const commandArguments = helper.parseCommand(message.trim());
if (commandArguments === null) {
return commands.error('Invalid Command');
}
const commandKeys = Object.keys(commandArguments);
if (commandKeys.length === 0 && !commands[commandKeys[0]]) {
return commands.error('Invalid Command');
}
const command = commandKeys[0];
return commands[command](commandArguments[command]);
}
return commands.error('Event not specified');
}
/**
* Main Lambda function
*
* @param {object} event AWS Lambda uses this parameter to pass in event data to the handler.
* @param {object} context AWS Lambda uses this parameter to provide your handler the runtime information of the Lambda function that is executing.
*
* @return {object} Request Promise
*/
exports.handler = (event, context) => {
// Message
let message;
if (event.body.channel_post && event.body.channel_post.text) {
message = event.body.channel_post.text;
} else if (event.body.message && event.body.message.text) {
message = event.body.message.text;
}
const processCommand = processCommands(message);
// Chat ID
let chatId;
if (event.body.message && event.body.message.chat && event.body.message.chat.id) {
chatId = event.body.message.chat.id;
} else if (event.body.channel_post && event.body.channel_post.chat && event.body.channel_post.chat.id) {
chatId = event.body.channel_post.chat.id;
}
if (chatId) {
processCommand.then((response) => {
const processTelegram = sendMessageToTelegram(chatId, response);
processTelegram.then(() => {
context.succeed();
}).catch(() => {
context.fail();
});
}).catch((error) => {
const processTelegram = sendMessageToTelegram(chatId, error.message);
processTelegram.then(() => {
context.succeed();
}).catch(() => {
context.fail();
});
});
} else {
processCommand.then(() => {
context.succeed();
}).catch(() => {
context.fail();
});
}
return processCommand;
};