forked from hunkim/Wit-Facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
168 lines (149 loc) · 4.53 KB
/
bot.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
// Weather Example
// See https://wit.ai/sungkim/weather/stories and https://wit.ai/docs/quickstart
const Wit = require('node-wit').Wit;
const FB = require('./facebook.js');
const Config = require('./const.js');
const API = require('./api.js');
const firstEntityValue = (entities, entity) => {
const val = entities && entities[entity] &&
Array.isArray(entities[entity]) &&
entities[entity].length > 0 &&
entities[entity][0].value;
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};
const firstValue = (entity) => {
return entity[0].value;
};
// Bot actions
const actions = {
say(sessionId, context, message, cb) {
console.log(message);
// Bot testing mode, run cb() and return
if (require.main === module) {
cb();
return;
}
// Our bot has something to say!
// Let's retrieve the Facebook user whose session belongs to from context
// TODO: need to get Facebook user name
const recipientId = context._fbid_;
if (recipientId) {
// Yay, we found our recipient!
// Let's forward our bot response to her.
FB.fbMessage(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 in context:', context);
// Giving the wheel back to our bot
cb();
}
},
merge(sessionId, context, entities, message, cb) {
// Retrieve the location entity and store it into a context field
var keys = Object.keys(entities);
var i;
for(i = 0; i < keys.length; i++){
context[keys[i]] = firstValue(entities[keys[i]]);
}
cb(context);
},
error(sessionId, context, error) {
console.log(error.message);
},
['fetch-weather-bobbejaanland'](sessionId, context, cb) {
API.getForecast('Herentals', forecastCallback, context, cb);
},
['fetch-weather'](sessionId, context, cb) {
// Here should go the api call, e.g.:
API.getForecast(context.location, forecastCallback, context, cb);
},
['fetch-drukte'](sessionId, context, cb) {
API.call('/drukte/' + context.datetime, drukteCallback, context, cb);
},
['fetch-drukte-vandaag'](sessionId, context, cb) {
API.call('/drukte/vandaag', drukteCallback, context, cb);
},
['fetch-openingsuren'](sessionId, context, cb) {
console.log(context);
API.call('/openingsuren/' + context.openingsuren + '/' + context.datetime, openingsurenCallback, context, cb);
},
['fetch-openingsuren-vandaag'](sessionId, context, cb) {
console.log(context);
API.call('/openingsuren/' + context.openingsuren + '/vandaag', openingsurenCallback, context, cb);
},
['fetch-grap'](sessionId, context, cb) {
console.log(context);
API.call('/grap', grapCallback, context, cb);
},
['fetch-attractiegrootte'](sessionId, context, cb) {
console.log(context);
API.call('/attractie/' + context.bob_attractie + '/size', attractiegrootteCallback, context, cb);
},
['fetch-wachtrij'](sessionId, context, cb) {
API.call('/wachtrij/' + context.bob_attractie, wachtrijCallback, context, cb);
},
['fetch-where'](sessionId, context, cb) {
API.call('/where/type/' + context.bob_faciliteiten + '/3', whereCallback, context, cb);
},
['clear-context'](sessionId, context, cb) {
fbid = context._fbid_;
context = {};
context._fbid_ = fbid;
cb();
}
};
const getWit = () => {
return new Wit(Config.WIT_TOKEN, actions);
};
exports.getWit = getWit;
function forecastCallback(data, context, cb) {
var data = JSON.parse(data);
var weather = data.weather[0];
context.forecast = weather.main;
cb(context);
}
function drukteCallback(data, context, cb) {
context.drukte = data;
cb(context);
}
function openingsurenCallback(data, context, cb) {
context.openingsuren = data;
cb(context);
}
function wachtrijCallback(data, context, cb) {
context.wachtrij = data;
cb(context);
}
function whereCallback(data, context, cb) {
context.location = data;
cb(context);
}
function grapCallback(data, context, cb) {
context.grap = data;
cb(context);
}
function attractiegrootteCallback(data, context, cb) {
context.attractiegrootte = data;
cb(context);
}
// bot testing mode
// http://stackoverflow.com/questions/6398196
if (require.main === module) {
console.log("Bot testing mode.");
const client = getWit();
client.interactive();
}