-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
37 lines (27 loc) · 960 Bytes
/
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
const { getSuggestions, sendReply } = require('./utils');
async function hello(event, context, callback) {
try {
const data = JSON.parse(event.body);
const { text } = data.message;
const { id: chatId, first_name: firstName } = data.message.chat;
console.log(`incoming - ${firstName} wrote "${text}" in #${chatId}`);
let response;
// First contact, say hello
if (text.toLowerCase().includes('start')) {
response = `Hallo, ${firstName}. Was suchst du?`;
}
// Query for `what` in `where`
if (text.toLowerCase().includes(' in ')) {
const [what, where] = text.split(' in ');
const suggestions = await getSuggestions(what, where);
response = suggestions.join('\n');
}
// send reply
await sendReply(response, chatId);
// terminate lambda
callback(null, { statusCode: 200 });
} catch (error) {
callback(error, { statusCode: 500 });
}
}
module.exports = { hello };