-
Notifications
You must be signed in to change notification settings - Fork 0
/
processMessage.js
141 lines (120 loc) · 4.37 KB
/
processMessage.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
console.log('Loading function');
const OFFERS_QTD = 5;
const VERIFY_TOKEN = process.env.VERIFY_TOKEN;
const START_PAYLOAD = 'start';
const MessageSender = require('./lib/MessageSender');
const lomadee = require('./lib/LomadeeHandler');
const dynamoDB = require('./lib/DynamoDBHandler');
const Offer = require('./model/Offer');
const Message = require('./model/Message');
const User = require('./model/User');
function verifyToken(parameters, callback) {
const serverToken = parameters['hub.verify_token'];
const response = {
body: null,
statusCode: null,
};
if (serverToken === VERIFY_TOKEN) {
const challenge = parseInt(parameters['hub.challenge'], 10);
[response.body, response.statusCode] = [challenge, 200];
} else {
[response.body, response.statusCode] = [
'Error, wrong validation token',
400,
];
}
callback(null, response);
}
function errorEvent(error) {
console.warn(error);
}
function processMessages(evt, callback) {
const data = JSON.parse(evt.body);
if (data.object === 'page') {
// Iterate over each entry - there may be multiple if batched
data.entry.forEach((entry) => {
// Iterate over each messaging event
entry.messaging.forEach((msg) => {
if (msg.message) {
const senderId = msg.sender.id;
const messageText = msg.message.text;
if (msg.message.quick_reply &&
msg.message.quick_reply.payload ===
MessageSender.SUBSCRIBE_YES_PAYLOAD) {
const user = new User(senderId, true);
dynamoDB.upsertUser(user);
MessageSender.sendTextMessage(senderId, Message.SUBSCRIBE_YES);
console.log(`User ${senderId} has subscribed`);
return;
} else if (msg.message.quick_reply &&
msg.message.quick_reply.payload ===
MessageSender.SUBSCRIBE_NO_PAYLOAD) {
const user = new User(senderId, false);
dynamoDB.upsertUser(user);
MessageSender.sendTextMessage(senderId, Message.SUBSCRIBE_NO);
console.log(`User ${senderId} has subscribed`);
return;
}
lomadee.searchByKeyword(messageText)
.then((response) => {
const searchData = response.data;
console.log(`Lomadee search by ${messageText}`, searchData);
if (!Array.isArray(searchData.offers)) {
console.log('Searched data is not an array');
return;
}
const offers = searchData.offers
.filter(Offer.filterByEbit)
.sort(Offer.sortByBestPrice)
.slice(0, OFFERS_QTD)
.map(Offer.formatOffer);
const preMessage = Message.SEARCH_RESULTS + Message.SEARCH_POS;
MessageSender.sendTextMessage(senderId, preMessage)
.catch(errorEvent);
MessageSender.sendOfferTemplateMessage(senderId, offers)
.catch(errorEvent);
})
.catch((error) => {
const preMessage = Message.SEARCH_NO_RESULTS + Message.SEARCH_POS;
MessageSender.sendTextMessage(senderId, preMessage)
.catch(errorEvent);
console.warn(`Error at Lomadee search by ${messageText}`, error);
});
} else if (msg.postback && msg.postback.payload) {
const payload = msg.postback.payload;
if (payload === START_PAYLOAD) {
const id = msg.sender.id;
console.log('Start payload: ', id);
MessageSender.sendTextMessage(id, Message.WELCOME)
.then(() => {
MessageSender.sendSubscribeMessage(
id,
Message.SUBSCRIBE_QUESTION
).catch(errorEvent);
})
.catch(errorEvent);
} else {
console.warn('Webhook received unhandled payload', payload);
}
} else {
console.error('Webhook received unknown event: ', evt);
}
});
});
}
// Assume all went well
const response = {
body: 'ok',
statusCode: 200,
};
callback(null, response);
}
exports.handler = (event, context, callback) => {
const queryParameters = event.queryStringParameters;
// GET/POST requests
if (queryParameters) {
verifyToken(queryParameters, callback);
} else {
processMessages(event, callback);
}
};