-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
121 lines (103 loc) · 5.96 KB
/
bot.js
File metadata and controls
121 lines (103 loc) · 5.96 KB
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
exports.handler = (event, context, callback) => {
// VIEW DOCS HERE: https://github.com/MotionAI/nodejs-samples
/* "event" object contains payload from Motion AI
{
"from":"string", // the end-user's identifier (may be FB ID, email address, Slack username etc, depends on bot type)
"session":"string", // a unique session identifier
"botId":"string", // the Motion AI ID of the bot
"botType":"string", // the type of bot this is (FB, Slack etc)
"customPayload":"string", // a developer-defined payload for carrying information
"moduleId":"string", // the current Motion AI Module ID
"moduleNickname":"string", // the current Motion AI Module's nickname
"inResponseTo":"string", // the Motion AI module that directed the conversation flow to this module
"reply":"string", // the end-user's reply that led to this module
"result":"string" // any extracted data from the prior module, if applicable,
"replyHistory":"object" // an object containing the current session's conversation messages
"nlpData":"object" // stringified NLP data object parsed from a user's message to your bot if NLP engine is enabled
"customVars":"string" // stringified object containing any existing customVars for current session
"fbUserData":"string" // for Messenger bots only - stringified object containing user's meta data - first name, last name, and id
"attachedMedia":"string" // for Messenger bots only - stringified object containing attachment data from the user
}
*/
// Check whether the user wants to have an email sent out to NMLA
var emailNMLA = JSON.parse(event.replyHistory).filter(function(replies){
return replies.id === "<Module_ID"; // Add the ID for the previous module in the branch that ask the user if they wish to send an email to NMLA for assistance
})[0].raw;
// this is the object we will return to Motion AI in the callback
var responseJSON = {
"response": "", // what the bot will respond with
"continue": true, // "true" will result in Motion AI continuing the flow based on connections, whie "false" will make Motion AI hit this module again when the user replies
"customPayload": "", // OPTIONAL: working data to examine in future calls to this function to keep track of state
"quickReplies": null, // OPTIONAL: a JSON string array containing suggested/quick replies to display to the user .. i.e., ["Hello","World"]
"cards": null, // OPTIONAL: a cards JSON object to display a carousel to the user (see docs)
"customVars": null, // OPTIONAL: an object or stringified object with key-value pairs to set custom variables eg: {"key":"value"} or '{"key":"value"}'
"nextModule": null // OPTIONAL: the ID of a module to follow this Node JS module
}
var request = require("request");
var optionsHeroku = {
uri: "https://api.heroku.com/apps/protechmepdfconversion/config-vars",
method: "GET",
headers: {
accept: "application/vnd.heroku+json; version=3"
},
auth: {user: "<EMAIL_for_Heroku_account>",
pass: "<API_KEY_for_Heroku_account>"}
};
request(optionsHeroku, function (error, response, body) {
if (error) {
console.log('Heroku API Error: ' + error.toString());
throw new Error(error);
}
var data = JSON.parse(body);
var secret = data["CRYPTO_SECRET"];
var crypto = require('crypto');
var algorithm = 'aes-256-ctr';
var encrypt = function(text) {
var cipher = crypto.createCipher(algorithm, secret);
var crypted = cipher.update(text,'utf8','hex');
crypted += cipher.final('hex');
return crypted;
}
var emailAddress = encrypt(event.reply);
request("https://api.motion.ai/getConversations?key=<MOTION.AI_API_KEY>&session=" + event.session,
function (error, response, body) {
if (error) {
console.log('MotionAI Error: ' + error.toString());
throw new Error(error);
}
var data = JSON.parse(body);
// send JSON data to the 'protechmepdfconversion' app
var options = {
uri: 'http://protechmepdfconversion.herokuapp.com/create-pdf',
method: 'POST',
json: {
"emailAssist": emailNMLA,
"emailUser": emailAddress,
"data": data.messages
}
};
request(options, function (error, res, body) {
if (error) {
console.log('MotionAI Error: ' + error.toString());
throw new Error(error);
}
// Response is a JSON object containing a 'link' key for the pdf link
// and potentially a 'email' key if an email was sent
var pdfLink = res.body.link;
if (response3.body.emailUser && !response3.body.emailAssist){
responseJSON.response = "An email was sent out to " + event.reply + ".\nYou can also download a PDF of the conversation <a target='_blank' href='" + pdfLink + "'>here</a>.";
callback(null, responseJSON);
} else if (response3.body.emailAssist && !response3.body.emailUser) {
responseJSON.response = "An email was sent out to New Mexico Legal Aid.\nYou can also download a PDF of the conversation <a target='_blank' href='" + pdfLink + "'>here</a>.";
callback(null, responseJSON);
} else if (response3.body.emailAssist && response3.body.emailUser) {
responseJSON.response = "An email was sent out to New Mexico Legal Aid and to " + event.reply + ".\nYou can also download a PDF of the conversation <a target='_blank' href='" + pdfLink + "'>here</a>.";
callback(null, responseJSON);
} else {
responseJSON.response = "Download a PDF of the conversation <a target='_blank' href='" + pdfLink + "'>here</a>";
callback(null, responseJSON);
}
});
});
});
};