forked from vcchang-zz/careBOTyou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
279 lines (258 loc) · 9.33 KB
/
app.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"use strict";
var restify = require('restify');
var builder = require('botbuilder');
var https = require('https');
var rp = require('request-promise');
var motivation = require("motivation");
var MICROSOFT_APP_ID = '2b72e45a-84b0-4f50-8615-e80cdcb7068c';
var MICROSOFT_APP_PASSWORD = 'oqwrIM3244!@pckKXBJM8?]';
var header = {'Content-Type':'application/json', 'Ocp-Apim-Subscription-Key':'6fb7959510a84e389985fd3343705e6b'}
var requestUrl = 'https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment';
var task;
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: MICROSOFT_APP_ID,
appPassword: MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
// Listen for messages from users
server.post('/api/messages', connector.listen());
// Bot introduces itself and says hello upon conversation start
bot.on('conversationUpdate', function (message) {
if (message.membersAdded[0].id === message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.text("Hello, I'm careBOTyou! How's your day going?");
bot.send(reply);
}
});
bot.dialog('/', function(session) {
sendGetSentimentRequest(session.message.text).then(function (parsedBody) {
console.log(parsedBody);
var score = parsedBody.documents[0].score.toString();
if(score > 0.80) { // happy
session.beginDialog("/happy");
} else if(score > 0.1) { // stressed
session.beginDialog("/stressed");
} else { // crisis
session.beginDialog("/crisis");
}
})
.catch(function (err) {
console.log("POST FAILED: " + err);
});
});
bot.dialog("/stressed", [
//We want to either cheer up the person->tell a joke or something
function(session) {
builder.Prompts.text(session, "You sound a bit stressed out, how can I help?");
},
function(session, results) {
switch (stressedCases(session.message.text)) {
case 1:
session.send("What do you need to get done?");
session.replaceDialog("/todo");
break;
case 2:
builder.Prompts.text(session, findAJoke());
session.endDialog("Hope that cheers you up.")
break;
default:
builder.Prompts.text(session, "Sorry I don't know how to help with that, but I hope this quote will inspire you.");
session.send(findMotivation());
// session.send("Your mind will answer most questions if you learn to relax and wait for the answers. --William S. Burroughs")
}
},
function(session) {
session.endDialog("I hope that helped:)");
}
]);
bot.dialog('/todo', [
function (session) {
builder.Prompts.text(session, 'Let me know & I will help you schedule a reminder.');
},
function (session, results) {
//setTimeout(startReminders(session.message), 2*60*1000);
task = `${results.response}`;
setTimeout(function() { startReminders(session.message); }, 5*1000);
session.endDialog(`Ok, I will remind you to do *${results.response}* in 30 minutes.`);
}
]);
bot.dialog('/reminder', [
function (session) {
session.send(`Hello, just want to remind you *${task}*`);
builder.Prompts.choice(session, "Would you like me to remind you again?", "yes|no");
},
function (session, results) {
console.log("LOGGING: " + results.response);
if (session.message.text == "yes") {
setTimeout(function() { startReminders(session.message); }, 5*1000);
session.endDialog(`Ok, I will remind you again in 30 minutes.`);
} else {
session.endDialog("OK, hope that helped:)");
}
}
]);
bot.dialog("/crisis", [
function(session) {
var question = "";
if(session.message.text.includes("suicidal")) {
question = "Please call this helpline for support: 1-833-456-4566?";
} else if(session.message.text.includes("sad")) {
question = "I think it would be a good idea to text a friend.";
} else if(session.message.text.includes("depressed")) {
question = "Check out this resource: http://www.bcmhsus.ca/our-services. Consider making an appointment with a professional.";
} else {
question = "Consider reaching out to your family or friends or making an appointment with a professional.";
}
builder.Prompts.text(session, question);
},
function(session, results) {
session.send("Thank you. Remember that you don't have to go through this alone.");
session.endDialog();
}
]);
bot.dialog('/happy', [
function(session) {
builder.Prompts.text(session, "That's awesome! What would make you even happier?");
},
function(session, results) {
getGiphy(results.response).then(function(gif) {
// session.send(gif.toString());
console.log(JSON.parse(gif).data);
session.send({
text: "Here you go!",
attachments: [
{
contentType: 'image/gif',
contentUrl: JSON.parse(gif).data.images.original.url
}
]
});
}).catch(function(err)
{
console.log("Error getting giphy: " + err);
session.send({
text: "We couldn't find that unfortunately :(",
attachments: [
{
contentType: 'image/gif',
contentUrl: 'https://media.giphy.com/media/ToMjGpt4q1nF76cJP9K/giphy.gif',
name: 'Chicken nugz are life'
}
]
});
}).then(function(idk) {
builder.Prompts.text(session, "Would you like to see more?");
});
},
function (session, results) {
if (results.response === "Yes" || results.response === "yes") {
session.beginDialog('/giphy');
} else {
session.endDialog("Have a great rest of your day!!!");
}
}
]);
bot.dialog('/giphy', [
function(session) {
builder.Prompts.text(session, "What would you like to see?");
}, function(session, results) {
getGiphy(results.response).then(function(gif) {
// session.send(gif.toString());
console.log(JSON.parse(gif).data);
session.send({
text: "Here you go!",
attachments: [
{
contentType: 'image/gif',
contentUrl: JSON.parse(gif).data.images.original.url
}
]
});
}).catch(function(err)
{
console.log("Error getting giphy: " + err);
session.send({
text: "We couldn't find that unfortunately :(",
attachments: [
{
contentType: 'image/gif',
contentUrl: 'https://media.giphy.com/media/ToMjGpt4q1nF76cJP9K/giphy.gif',
name: 'Chicken nugz are life'
}
]
});
}).then(function(idk) {
builder.Prompts.text(session, "Would you like to see more?");
});
},
function (session, results) {
if (results.response === "Yes" || results.response === "yes") {
session.beginDialog('/giphy');
} else {
session.endDialog("Have a great rest of your day!!!");
}
}
]);
function startReminders(msg){
console.log("SETTING REMINDER");
bot.beginDialog(msg.address, "/reminder")
}
function stressedCases(message){
if (message.includes("todo") || message.includes("homework") || message.includes("work")){
return 1;
}else if (message.includes("joke") || message.includes("funny") || message.includes("sad")){
return 2;
}else {
return 0;
}
}
function findMotivation(){
var m = motivation.get();
return m;
}
function findAJoke(){
switch (Math.floor(Math.random() * 4)) {
case 0:
return "Why aren’t koalas actual bears? They don’t meet the koalafications."
break;
case 1:
return "You know why you never see elephants hiding up in trees? Because they’re really good at it."
break;
case 2:
return "Two gold fish are in a tank. One looks at the other and says, “You know how to drive this thing?!”"
break;
default:
return "How does NASA organize a party? They planet."
break;
}
}
function getGiphy(searchString) {
var options = {
method: 'GET',
uri: 'https://api.giphy.com/v1/gifs/translate',
qs: {
s: searchString,
api_key: '9n8AIaWULVu37sA1k8eE38IwnCCjmXP9'
}
}
return rp(options);
}
function sendGetSentimentRequest(message) {
var options = {
method: 'POST',
uri: requestUrl,
body: {
documents:[{id:'1', language: 'en', text:message}]
},
json: true, // Automatically stringifies the body to JSON,
headers: header
};
return rp(options);
}