-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
113 lines (95 loc) · 3.37 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
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
const URL = require("url"); // Built in library
const API_AI = require("apiai-promise"); // DialogFlow v1 library (Promise version)
const polly = require("polly-js"); // A request retry library
const { API_AI_KEY } = process.env; // Load key from secure environment variables
const DEFAULT_APP = API_AI_KEY ? API_AI(API_AI_KEY) : null;
// Some constants
const RETRY_ATTEMPTS = 3;
const MAX_QUERY_STRING_LENGTH = 255;
// Define the endpoint. This is run every HTTP request
exports.endpoint = function(req, res) {
let query = URL.parse(req.url, true).query; // Get query string if any from ChatFuel etc
let app = query.API_AI_KEY ? API_AI(query.API_AI_KEY) : DEFAULT_APP; // Create app with API key
// Generate new random session ID if not passed one in the query
// We can use this to keep track of sessions
let newSessionId =
!query.DF_SESSION_ID || query.DF_SESSION_ID === "0"
? Math.random()
.toString()
.slice(2)
: 0;
let sessionId =
query.DF_SESSION_ID && query.DF_SESSION_ID != "0"
? query.DF_SESSION_ID
: newSessionId;
let contexts = [
{
name: query.DF_CONTEXT || "DEFAULT",
parameters: query
}
];
let queryStringToSend = "";
// DialogFlow can only string less than 255 characters
if (query.queryString.length > MAX_QUERY_STRING_LENGTH) {
queryStringToSend = query.queryString.substring(0, MAX_QUERY_STRING_LENGTH);
} else {
queryStringToSend = query.queryString;
}
// Use polly to retry DialogFlow requests until one works or else send error
polly()
.retry(RETRY_ATTEMPTS)
.executeForPromise(function() {
return app.textRequest(queryStringToSend, {
sessionId,
contexts
});
})
.then(handleResponse(res), handleError(res));
};
function sendResponse({ response, message }) {
response.writeHead(200, { "Content-Type": "application/json" });
response.end(JSON.stringify(message));
}
function createTextMsg(text) {
return { messages: [{ text }] };
}
function handleResponse(response) {
return function({ result, sessionId }) {
console.log(result);
// Deal with custom payloads if there is one
let messages = result.fulfillment.messages;
let customPayload = messages.find(message => message.type === 4);
// If there is a custom payload send that back to ChatFuel
if (customPayload) {
let message = customPayload.payload;
// Check if there are already custom messages
if (message.messages) {
// If so then push any text responses from DialogFlow
message.messages.push({
text: result.fulfillment.speech
});
} else {
// Otherwise it's the only one
message.messages = [{ text: result.fulfillment.speech }];
}
message.set_attributes = Object.assign(message.set_attributes || {}, {
DF_SESSION_ID: sessionId
});
sendResponse({ response, message });
} else {
let message = createTextMsg(result.fulfillment.speech);
message.set_attributes = Object.assign(message.set_attributes || {}, {
DF_SESSION_ID: sessionId
});
sendResponse({ response, message });
}
};
}
function handleError(response) {
return function(error) {
console.log("Failed " + RETRY_ATTEMPTS + " retries...");
console.log(error);
let message = createTextMsg("There was an error...");
sendResponse({ response, message });
};
}