-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·206 lines (171 loc) · 5.88 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
// Webserver to show we can start ok
var express = require('express');
var cfenv = require('cfenv');
var appEnv = cfenv.getAppEnv();
var app = express();
app.use(express.static(__dirname + '/public'));
app.listen(appEnv.port, '0.0.0.0', function() {
console.log("server starting on " + appEnv.url);
});
// botkit for helpful utils
var Botkit = require('botkit')
var os = require('os');
var controller = Botkit.slackbot({
debug: false,
});
var bot = controller.spawn({
token: process.env.slack_token
}).startRTM();
// Watson services initialize
var watson = require('watson-developer-cloud');
var watson_pi_credentials = appEnv.getServiceCreds('Personality Insights-fd');
var personality_insights = watson.personality_insights({
username: watson_pi_credentials.username,
password: watson_pi_credentials.password,
version: 'v2'
});
var tone_analyzer = watson.tone_analyzer({
username: watson_pi_credentials.username,
password: watson_pi_credentials.password,
version: 'v2-experimental'
});
var my_watson = require('./mywatson.js');
// Callbacks
controller.hears(['watson (.*)'],'ambient,mention', function(bot, message) {
console.log(message.text);
my_watson.watson_analyze(message.text, function(response) {
bot.reply(message, response);
});
});
controller.hears(['analyze', 'analyse'], 'direct_message,direct_mention', function(bot, message) {
bot.api.channels.history({
channel: message.channel,
}, function(err, history) {
if (err) {
console.log('ERROR',err);
}
var messages = [];
for (var i = 0; i < history.messages.length; i++) {
messages.push(history.messages[i].text);
}
// call the watson api with your text
var corpus = messages.join("\n");
personality_insights.profile(
{
text: corpus,
language: 'en'
},
function (err, response) {
if (err) {
console.log('error:', err);
bot.reply(message, err.error);
} else {
// response.tree.children.children is a list of the top 5 traits
var top5 = response.tree.children[0].children[0].children;
bot.reply(message, ':robot_face: Let me analyze that for you...');
bot.reply(message, 'In this community I\'m seeing:');
for (var c = 0; c < top5.length; c++) {
bot.reply(message, ' ' + Math.round(top5[c].percentage*100) + '% ' + top5[c].name);
}
bot.reply(message, 'Agreeableness is a person\'s tendency to be compassionate and cooperative toward others.');
bot.reply(message, 'Conscientiousness is a person\'s tendency to act in an organized or thoughtful way.');
bot.reply(message, 'Extraversion is a person\'s tendency to seek stimulation in the company of others.');
bot.reply(message, 'Emotional Range, also referred to as Neuroticism or Natural Reactions, is the extent to which a person\'s emotions are sensitive to his or her environment.');
bot.reply(message, 'Openness is the extent to which a person is open to experiencing a variety of activities');
}
} // end function
); // end personality_insights.profile
}); // end bot.api.channels.history
})
controller.hears(['hello','hi'],'direct_message,direct_mention,mention',function(bot,message) {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'robot_face',
},function(err,res) {
if (err) {
bot.botkit.log("Failed to add emoji reaction :(",err);
}
});
controller.storage.users.get(message.user,function(err,user) {
if (user && user.name) {
bot.reply(message,"Hello " + user.name+"!!");
} else {
bot.reply(message,"Hello.");
}
});
})
controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',function(bot,message) {
var matches = message.text.match(/call me (.*)/i);
var name = matches[1];
controller.storage.users.get(message.user,function(err,user) {
if (!user) {
user = {
id: message.user,
}
}
user.name = name;
controller.storage.users.save(user, function(err,id) {
bot.reply(message,"Got it. I will call you " + user.name + " from now on.");
})
})
});
controller.hears(['what is my name','who am i'],'direct_message,direct_mention,mention',function(bot,message) {
controller.storage.users.get(message.user, function(err,user) {
if (user && user.name) {
bot.reply(message,"Your name is " + user.name);
} else {
bot.reply(message,"I don't know yet!");
}
})
});
controller.hears(['shutdown'],'direct_message,direct_mention,mention',function(bot,message) {
bot.startConversation(message,function(err,convo) {
convo.ask("Are you sure you want me to shutdown?",[
{
pattern: bot.utterances.yes,
callback: function(response,convo) {
convo.say("Bye!");
convo.next();
setTimeout(function() {
process.exit();
},3000);
}
},
{
pattern: bot.utterances.no,
default:true,
callback: function(response,convo) {
convo.say("*Phew!*");
convo.next();
}
}
])
})
})
controller.hears(
['uptime','identify yourself','who are you','what is your name'],
'direct_message,direct_mention,mention',
function(bot,message) {
var hostname = os.hostname();
var uptime = formatUptime(process.uptime());
bot.reply(
message,
':robot_face: I am <@' + bot.identity.name +'> and I have been running for ' + uptime + ' on ' + hostname + ".");
})
function formatUptime(uptime) {
var unit = 'second';
if (uptime > 60) {
uptime = uptime / 60;
unit = 'minute';
}
if (uptime > 60) {
uptime = uptime / 60;
unit = 'hour';
}
if (uptime != 1) {
unit = unit +'s';
}
uptime = Math.round(uptime * 10) / 10 + ' ' + unit;
return uptime;
}