-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatson-slack.js
175 lines (153 loc) · 5.18 KB
/
watson-slack.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
var Botkit = require('botkit');
require('dotenv').load();
var request = require('request');
var weather = require('./weather.js');
var course = require('./course.js');
var library = require('./libraryTime.js')
var locate = require('./map.js')
var proff = require('./proffessors.js')
var middleware = require('botkit-middleware-watson')({
username: process.env.CONVERSATION_USERNAME,
password: process.env.CONVERSATION_PASSWORD,
workspace_id: process.env.WORKSPACE_ID,
version_date: '2016-09-20'
});
var controller = Botkit.slackbot({
json_file_store: './db_slackbutton_bot/',
}).configureSlackApp(
{
clientId: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
scopes: ['bot']
}
);
controller.setupWebserver(3000, function (err, webserver) {
controller.createWebhookEndpoints(controller.webserver);
controller.createOauthEndpoints(controller.webserver, function (err, req, res) {
if (err) {
res.status(500).send('ERROR: ' + err);
} else {
//res.send('Success!');
res.redirect('https://distinct-cross.glitch.me/sucess.html');
}
});
});
var _bots = {};
function trackBot(bot) {
_bots[bot.config.token] = bot;
}
function invokeAction(watsonDataOutput, bot, message) {
let actionName = watsonDataOutput.context.action.name;
switch (actionName) {
case 'lookupWeather':
//let answer = JSON.stringify(watsonDataOutput.context.action)
//bot.reply(message, answer);
weather.lookupWeather(watsonDataOutput, bot, message);
break;
case 'lookupCourse':
course.lookupCourse(watsonDataOutput, bot, message);
break;
case 'libraryTiming':
library.libraryTiming(watsonDataOutput, bot, message);
break;
case 'lookupLocation':
locate.lookupLocation(watsonDataOutput, bot, message);
break;
case 'lookupProff':
proff.lookupProff(watsonDataOutput, bot, message);
break;
default:
bot.reply(message, "Sorry, I cannot execute what you've asked me to do");
}
}
function handleWatsonResponse(bot, message) {
let customSlackMessage = false;
let actionToBeInvoked = false;
if (message.watsonData) {
if (message.watsonData.output) {
if (message.watsonData.output.context) {
if (message.watsonData.output.context.slack) {
customSlackMessage = true;
}
if (message.watsonData.output.context.action) {
actionToBeInvoked = true;
}
}
}
}
if (actionToBeInvoked == true) {
bot.reply(message, message.watsonData.output.text.join('\n'));
invokeAction(message.watsonData.output, bot, message);
}
else {
if (customSlackMessage == true) {
bot.reply(message, message.watsonData.output.context.slack);
}
else {
bot.reply(message, message.watsonData.output.text[0]);
}
}
}
controller.on('direct_message,direct_mention,mention', function (bot, message) {
middleware.interpret(bot, message, function (err) {
if (!err) {
handleWatsonResponse(bot, message);
}
else {
bot.reply(message, "I'm sorry, but for technical reasons I can't respond to your message");
}
});
});
controller.on('interactive_message_callback', function (bot, message) {
middleware.interpret(bot, message, function (err) {
if (!err) {
handleWatsonResponse(bot, message);
}
else {
bot.reply(message, "I'm sorry, but for technical reasons I can't respond to your message");
}
});
});
controller.on('create_bot', function (bot, config) {
if (_bots[bot.config.token]) {
// already online! do nothing.
} else {
bot.startRTM(function (err) {
if (!err) {
trackBot(bot);
}
bot.startPrivateConversation({ user: config.createdBy }, function (err, convo) {
if (err) {
console.log(err);
} else {
convo.say("Hello! I'm ChatME bot for SJSU related queries.");
}
});
});
}
});
controller.on('rtm_open', function (bot) {
console.log('** The RTM api just connected!');
});
controller.on('rtm_close', function (bot) {
console.log('** The RTM api just closed');
// you may want to attempt to re-open
});
controller.storage.teams.all(function (err, teams) {
if (err) {
console.log(err);
throw new Error(err);
}
// connect all teams with bots up to slack!
for (var t in teams) {
if (teams[t].bot) {
controller.spawn(teams[t]).startRTM(function (err, bot) {
if (err) {
console.log('Error connecting bot to Slack:', err);
} else {
trackBot(bot);
}
});
}
}
});