-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathwit.actions.js
72 lines (63 loc) · 1.99 KB
/
wit.actions.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
'use strict';
const async = require('async');
const FB = require("./facebook.action");
module.exports = {
say(recipientId, context, message, cb) {
if (recipientId) {
// Yay, we found our recipient!
// Let's forward our bot response to her.
FB.sendText(recipientId, message, (err, data) => {
if (err) {
console.log(
'Oops! An error occurred while forwarding the response to',
recipientId,
':',
err
);
}
// Let's give the wheel back to our bot
cb();
});
} else {
console.log('Oops! Couldn\'t find user for session:', sessionId);
// Giving the wheel back to our bot
cb();
}
},
merge(recipientId, context, entities, message, cb) {
async.forEachOf(entities, (entity, key, cb) => {
const value = firstEntityValue(entity);
//console.error("Result", key, value);
if (value != null && (context[key] == null || context[key] != value)) {
switch (key) {
default:
cb();
}
}
else
cb();
}, (error) => {
if (error) {
console.error(error);
} else {
console.log("Context after merge:\n", context);
cb(context);
}
});
},
error(recipientId, context, error) {
console.log(error.message);
},
/**** Add your own functions HERE ******/
};
// Helper function to get the first message
const firstEntityValue = (entity) => {
const val = entity && Array.isArray(entity) &&
entity.length > 0 &&
entity[0].value
;
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};