This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessenger.js
65 lines (51 loc) · 1.87 KB
/
messenger.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
'use strict';
let witClient = require('./lib/wit.js');
const sessions = {};
let sendTextMessage = require('./fbSendMessage');
function receivedMessage(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfMessage = event.timestamp;
var message = event.message;
console.log("Received message for user %d and page %d at %d with message:",
senderID, recipientID, timeOfMessage, message.text);
// console.log(JSON.stringify(message));
var messageId = message.mid;
// You may get a text or attachment but not both
var messageText = message.text;
var messageAttachments = message.attachments;
if (messageText) {
// If we receive a text message, check to see if it matches any special
// keywords and send back the corresponding example. Otherwise, just echo
// the text we received.
switch (messageText) {
case 'image':
// sendImageMessage(senderID);
break;
case 'button':
// sendButtonMessage(senderID);
break;
case 'generic':
// sendGenericMessage(senderID);
break;
case 'receipt':
// sendReceiptMessage(senderID);
break;
default:
console.log('CONTEXT PRE ACTIONS', sessions[senderID]);
witClient.runActions(senderID, messageText, sessions[senderID] = sessions[senderID] || {})
.then(function(context){
sessions[senderID] = context;
console.log('CONTEXT POST ACTIONS', context);
if (context.ticket) {
sessions[senderID] = {};
}
// console.log('context ....', context);
// console.log('context', JSON.stringify(context));
});
}
} else if (messageAttachments) {
sendTextMessage(senderID, "Este tipo de mensaje no está soportado.");
}
}
module.exports = {sendTextMessage, receivedMessage};