-
Notifications
You must be signed in to change notification settings - Fork 83
/
bot-transaction.js
103 lines (74 loc) · 2.41 KB
/
bot-transaction.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
const botmgr = require('node-red-viseo-bot-manager');
const helper = require('node-red-viseo-helper');
module.exports = function(RED) {
const register = function(config) {
RED.nodes.createNode(this, config);
let node = this;
node.prompt = true;
start(RED, node, config);
this.on('input', (data) => {
input(node, data, config);
});
this.on('close', (done) => {
stop(done);
});
}
RED.nodes.registerType("node-bot-transaction", register, {});
}
const start = (RED, node, config) => {
};
const input = (node, data, config) => {
let convId = botmgr.getConvId(data)
// Prepare the prompt
if (node.prompt){
botmgr.delayCallback(convId, (prompt) => {
data.prompt = prompt
node.send(data)
})
}
// Retrieve replies
let reply = buildReply(node, data, config);
if (!reply){
node.send(data)
return;
}
// Emit reply message
data.reply = [ reply ];
helper.emitAsyncEvent('before-reply', node, data, config, (beforeData) => {
helper.emitAsyncEvent('reply', node, beforeData, config, (newData) => {
helper.emitAsyncEvent('replied', node, newData, config, () => {})
if (node.prompt) return;
node.send(data);
});
});
};
const buildReply = (node, data, config) => {
let reply = {
type: "transaction",
intent: config.intent,
prompt: node.prompt
}
if(config.intent == "confirm") {
reply.orderItems = helper.getByString(data, config.orderItems, [])
reply.orderId = helper.getByString(data, config.orderId, config.orderId)
reply.merchant = config.merchant
}
if(config.intent == "receipt") {
let receipt = {
'carrierId': helper.getByString(data, config.carrierId, config.carrierId),
'orderState': config.receiptStatus,
'orderStateName': config.receiptStatusName,
'orderId': helper.getByString(data, config.orderId, config.orderId),
'orderActions': helper.getByString(data, config.orderActions, [])
}
if(receipt.carrierId !== undefined) {
data._receipt = receipt //save to be sent along the next message
}
return;
}
return reply;
}
const stop = (done) => {
//nothing to do.
done();
};