-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
259 lines (220 loc) · 9.64 KB
/
Copy pathapp.js
File metadata and controls
259 lines (220 loc) · 9.64 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
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
// This loads the environment variables from the .env file
require('dotenv-extended').load();
const builder = require('botbuilder'),
emotionService = require('./emotion-service'),
needle = require("needle"),
restify = require('restify'),
url = require('url');
validUrl = require('valid-url'),
request = require('request').defaults({ encoding: null }),
oxfordEemotion = require("node-oxford-emotion")(process.env.MICROSOFT_EMOTION_API_KEY);
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
const 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 bot
const connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
const bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
// LUIS
const model = process.env.LUIS_MODEL;
const recognizer = new builder.LuisRecognizer(model);
var intents = new builder.IntentDialog({ recognizers: [recognizer] });
// Setup intents
bot.endConversationAction('goodbye', 'Goodbye :)', { matches: /^goodbye|bye|see you|end|stop/i });
bot.beginDialogAction('help', '/help', { matches: /^help|home|stuck/i });
bot.dialog('/', intents);
intents.matches('None', '/help')
.matches('greeting', '/greeting')
.matches('thank', builder.DialogAction.send(":)"))
.matches(/^awesome|cool|great/i, builder.DialogAction.send("I know right!"))
.matches('about', '/about')
.matches('play', '/play')
.onDefault(builder.DialogAction.send("I'm sorry. I didn't understand."))
//=========================================================
// Bots Events
//=========================================================
//Sends greeting message when the bot is first added to a conversation
bot.on('conversationUpdate', message => {
if (message.membersAdded) {
message.membersAdded.forEach(identity => {
if (identity.id === message.address.bot.id) {
const reply = new builder.Message()
.address(message.address)
.text("Hi! I am Sweet Bot, welcome to the Microsoft stand here at Bot World. I can understand your emotions and if you play a game with me, you'll get a sweet! Ask me about the game :)");
bot.send(reply);
}
});
}
});
//=========================================================
// Bot Dialogs
//=========================================================
bot.dialog('/greeting', session => {
new Promise(
(resolve, reject) => {
const requestData = {
url: "https://sweetbot.azure-api.net/api/values/1",
headers: {
'content-type': 'application/json',
'Ocp-Apim-Subscription-Key': process.env.SWEETBOT_API_KEY
},
json: { "user": session.message.user.name}
};
request.put(requestData, (error, response, body) => {
if (error) {
reject(error);
}
else if (response.statusCode != 200) {
reject(body);
}
else {
resolve(body);
}
});
}
);
session.endDialog("Hey " + session.message.user.name + " :)");
});
bot.dialog('/help', session => {
session.endDialog("* start - starts the game \n* give me more - to keep playing \n* about - gives you information about this game \n* help - Displays these commands.");
});
bot.dialog('/about', session => {
session.send("The game is about guessing your emotions (don't worry! we do not store the photos). We use the Microsoft Emotions API to do this. If you want to play, let me know and I'll give you the emotion you need to act out, all you'll have to do is take a picture and send it to me.");
session.endDialog("If you act out the correct emotion I will fire a request to the lovely sweet machine at the Microsoft Stand and you will get a sweet!");
});
bot.dialog('/play', [
function (session) {
var randNum = Math.floor(Math.random() * emotionList.length);
session.userData.emotionSelected = emotionList[randNum];
builder.Prompts.text(session, "Send me a picture showing the emotion '" + emotionList[randNum] + "'. Try sending me an image or an image URL");
},
function (session){
if (hasImageAttachment(session)) {
if (isSkypeAttachment(session.message.attachments[0])) {
////////////////////////////////////// SKYPE IMAGE STREAM PROBLEM ////////////////////////////////////////////////
var stream = getImageStreamFromUrl(session.message.attachments[0]);
// var bitmap = fs.readFileSync(stream);
// var imageData = new Buffer(bitmap.toString('binary'),'binary');
// var emotion = oxfordEmotion.recognize("image", stream, function(cb) {
// console.log(cb);
// });
emotionService
.getEmotionFromStream(stream)
.then(caption => handleSuccessResponse(session, caption))
.catch(error => handleErrorResponse(session, error));
////////////////////////////////////// SKYPE IMAGE STREAM PROBLEM ////////////////////////////////////////////////
}else{
var imgUrl = session.message.attachments[0].contentUrl;
emotionService
.getEmotionFromUrl(imgUrl)
.then(emotion => handleSuccessResponse(session, emotion))
.catch(error => handleErrorResponse(session, error));
}
}
else if(imageUrl = (parseAnchorTag(session.message.text) || (validUrl.isUri(session.message.text)? session.message.text : null))) {
emotionService
.getEmotionFromUrl(imageUrl)
.then(emotion => handleSuccessResponse(session, emotion))
.catch(error => handleErrorResponse(session, error));
}
else {
session.send("Did you upload an image? I'm more of a visual person. Try sending me an image or an image URL");
session.beginDialog('/play');
}
}
]);
//=========================================================
// Utilities
//=========================================================
const emotionList = [
"anger",
"contempt",
"disgust",
"fear",
"happiness",
"neutral",
"sadness",
"surprise"
];
const hasImageAttachment = session => {
return ((session.message.attachments.length > 0) && (session.message.attachments[0].contentType.indexOf("image") !== -1));
}
const getImageStreamFromUrl = attachment => {
var headers = {};
if (isSkypeAttachment(attachment)) {
connector.getAccessToken((error, token) => {
var tok = token;
headers['Authorization'] = 'Bearer ' + token;
headers['Content-Type'] = 'application/octet-stream';
return needle.get(attachment.contentUrl, { headers: headers });
});
}
headers['Content-Type'] = attachment.contentType;
return needle.get(attachment.contentUrl, { headers: headers });
}
const isSkypeAttachment = attachment => {
if (url.parse(attachment.contentUrl).hostname.substr(-"skype.com".length) == "skype.com") {
return true;
}
return false;
}
/**
* Gets the href value in an anchor element.
* Skype transforms raw urls to html. Here we extract the href value from the url
*/
const parseAnchorTag = input => {
var match = input.match("^<a href=\"([^\"]*)\">[^<]*</a>$");
if(match && match[1]) {
return match[1];
}
return null;
}
//=========================================================
// Response Handling
//=========================================================
const handleSuccessResponse = (session, emotion) => {
if (emotion) {
if(emotion == session.userData.emotionSelected){
session.endDialog("Nice! That's definitely " + emotion + ". I'll speak to that machine and make sure you get something sweet :)");
// API call to Paul's sweet dispenser
new Promise(
(resolve, reject) => {
const requestData = {
url: "https://sweetbot.azure-api.net/api/values/add",
headers: { 'Ocp-Apim-Subscription-Key': process.env.SWEETBOT_API_KEY }
};
request.get(requestData, (error, response, body) => {
if (error) {
reject(error);
}
else if (response.statusCode != 200) {
reject(body);
}
else {
resolve(body);
}
});
}
);
}
else {
session.endDialog("I think that emotion is " + emotion + ". I was looking for some thing more along the lines of " + session.userData.emotionSelected + ". Let's try something else!");
session.beginDialog('/play');
}
}
else {
session.send("Woops! I couldn't pick out an emotion from that picture :(");
}
}
const handleErrorResponse = (session, error) => {
session.send("Oops! Something went wrong. Try again later.");
console.error(error);
}