-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathBookACab_sync.js
57 lines (52 loc) · 1.8 KB
/
BookACab_sync.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
var botId = "st-007da037-67f9-55c3-bf93-6272ca639359";
var botName = "Book a Cab";
var sdk = require("./lib/sdk");
var Promise = sdk.Promise;
var config = require("./config");
var mockServiceUrl = config.examples.mockServicesHost + '/cabbot';
var { makeHttpCall } = require("./makeHttpCall");
/*
* This example is the sync version of 'BookACab.js'. Both the webhook calls in this are responding syncly
*/
function findCabs(/*userLoc*/) {
return new Promise(function(resolve, reject) {
makeHttpCall(
'get',
mockServiceUrl + '/findcabs'
)
.then(function(res) {
resolve(res);
})
.catch(function(err){
return reject(err);
})
});
}
module.exports = {
botId : botId,
botName : botName,
on_user_message : function(requestId, data, callback) {
sdk.sendBotMessage(data, callback);
},
on_bot_message : function(requestId, data, callback) {
sdk.sendUserMessage(data, callback);
},
on_webhook : function(requestId, data, componentName, callback) {
var context = data.context;
if (componentName === 'FindNearbyCabs') {
findCabs()
.then(function(cabList) {
context.cabList = cabList;
callback(null, data);
});
} else if (componentName === 'BookTheCab') {
sdk.saveData(requestId, data)
.then(function() {
//Assuming the cab booking was successful. A mock service to book the cab can be called here.
data.successful = 'true';
data.bookedCab = context.entities.selectedCab || {};
callback(null, data);
});
}
}
};