-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
81 lines (60 loc) · 1.97 KB
/
index.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
'use strict';
const
express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()),
common = require('./utils/common.js'),
cache = require('./session-cache.js'),
request = require('request'),
config = require('./configuration/config.json'),
parser = require('./parse.js');
app.listen(process.env.PORT || 1337, () => console.log('alexa-webhook is listening'));
app.post('/alexa-webhook', (req, res) => {
let body = req.body;
var requestObj = parser(body);
var requestType = requestObj.requestType;
var msg = requestObj.msg;
var sessionId = requestObj.sessionId;
console.log("-------================---------");
console.log("requestType : ", JSON.stringify(requestType, null, 4));
console.log("");
console.log("msg : ", JSON.stringify(msg, null, 4));
console.log("");
console.log("-------================---------");
cache.insertSession(sessionId, res);
requestBotWebhook(requestObj);
});
app.post('/alexa-webhook-response', (req, res) => {
let body = req.body;
var alexaResponse = body.alexaResponse;
var sessionId = body.sessionId;
console.log("-------================---------");
console.log("alexaResponse : ", JSON.stringify(alexaResponse, null, 4));
var responseCallback = cache.popSessionResponse(sessionId);
if (responseCallback) {
responseCallback.send(alexaResponse);
console.log("Message Sent to Alexa !");
res.sendStatus(200);
} else {
console.log("Error No response callback");
res.sendStatus(404);
}
});
function requestBotWebhook(requestObj) {
request({
uri: config.BOT_WEBHOOK_URL + '/bot-webhook',
method: 'POST',
json: requestObj,
headers: {
'Content-type': 'application/json',
},
}, function(error, response, body) {
if (error) {
console.log('Error:', error);
return;
} else {
console.log('Request sent to Bot-Webhook');
return;
}
});
}