-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
59 lines (52 loc) · 1.58 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
import getUsers from './lib/getUsers';
import logResponse from './lib/logger';
import sendMessage from './lib/sendMessage';
import AWS from 'aws-sdk';
const BOT_TOKEN = process.env.BOT_TOKEN;
const botSendMessage = sendMessage(BOT_TOKEN);
const dynamoDB = new AWS.DynamoDB.DocumentClient();
export const commands = async (event) => {
const { message } = JSON.parse(event.body);
const {
chat: { id: chat_id },
entities: [{
type = ''
}] = [{}],
from: { first_name: userName }
} = message;
const isCommand = type === 'bot_command';
const isJoinUpdate = Object.keys(message).includes('new_chat_members');
if (isJoinUpdate) {
const users = getUsers(message['new_chat_members']);
// Welcome Each New Member
users.forEach(async function ({ first_name }) {
const respones = await botSendMessage(chat_id, `Welcome to the group ${first_name}👋`);
logResponse(respones);
});
// Add Users to the database
users.forEach(async function (user) {
try {
await dynamoDB.put({
TableName: process.env.TableName,
Item: { ...user }
});
} catch (error) {
console.log(error);
}
});
}
if (isCommand) {
const [command] = message.text.match(/^\/\w+/) || [];
let response;
switch (command) {
case '/greetings':
response = await botSendMessage(chat_id, `Hello ${userName}👋`);
logResponse(response);
break;
default:
response = await sendMessage(chat_id, 'Command Not Found ✖');
logResponse(response);
break;
}
}
};