-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
134 lines (112 loc) · 3.6 KB
/
server.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
var app = require('express')();
var bodyParser = require('body-parser');
var request = require('request');
const { Wit, log } = require('node-wit');
const templates = require('./actions/templates');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
const client = new Wit({
accessToken: '', // wit token
logger: new log.Logger(log.DEBUG) // optional
});
var port = process.env.PORT || 8080;
var token = ""; // messenger token
function replyToSender({ sender, actionType, text }) {
messageData = {
text: text
};
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: token },
method: 'POST',
json: {
recipient: { id: sender },
message: templates(actionType).listTemplate || messageData,
}
}, function (error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
};
let qty = 0;
const addCartQty = (count) => {
qty += Number(count);
return qty;
}
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === 'cool') {
res.send(req.query['hub.challenge']);
}
res.send('Error, wrong validation token');
});
app.post('/webhook/', function (req, res) {
messaging_events = req.body.entry[0].messaging;
for (i = 0; i < messaging_events.length; i++) {
event = req.body.entry[0].messaging[i];
sender = event.sender.id;
if (event.message && event.message.text) {
text = event.message.text;
client.message(text, {})
.then((data) => {
const entity = data.entities;
if (entity['order'] && entity['order'][0].value === 'order food') {
replyToSender({ sender, text: "Can you tell me your location?" });
} else if (entity['order'] && entity['order'][0].value === 'checkout') {
replyToSender({ sender, text: `Ok, So I am placing order for ${qty} added in your cart. Check your email for order status.` })
const orderStatus = placeOrder();
}
else if (entity['location']) {
replyToSender({ sender, text: `We have an outlet serving in ${entity['location'][0].value}` });
setTimeout(() => {
replyToSender({ sender, text: "Cool! let's start creating your order. What would like to order?" });
}, 200);
setTimeout(() => {
replyToSender({ sender, actionType: 'list' });
}, 400);
}
})
.catch(err => {
replyToSender({ sender, text: `There is something wrong with me. Don't worry you can order online too.` });
});
} else if (event && event.postback) {
const payload = JSON.parse(event.postback.payload);
const itemsInCart = addCartQty(payload.quantity);
replyToSender({ sender, text: `You have ${itemsInCart} in cart. Let me know whenever you want to checkout.` })
}
};
res.sendStatus(200);
});
app.get('/', function (req, res) {
res.sendStatus(200);
});
app.listen(port, function () {
console.log('The webhook is running on port ' + port);
});
const options = {
method: 'POST',
uri: '', // create order url
headers: {
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
},
multipart:
[{
'content-type': 'application/json',
body: {}
}],
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
const info = JSON.parse(body);
return info;
}
console.log(error);
}
const placeOrder = () => {
return request(options, callback);
}