-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchatbot.js
215 lines (190 loc) · 7.37 KB
/
chatbot.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
'use strict';
const request = require('request');
const async = require('async');
const wicked = require('wicked-sdk');
const mustache = require('mustache');
const { debug, warn, error } = require('portal-env').Logger('portal-chatbot:chatbot');
const Messages = require('./messages.json');
const utils = require('./utils');
const chatbot = function () { };
chatbot.eventTargetMapping = {};
chatbot.interestingTemplates = {};
chatbot.chatbotTemplates = null;
chatbot.init = function (app, done) {
debug('init()');
const myUrl = app.get('my_url');
async.parallel({
registerWebhook: function (callback) {
debug('Registering as listener.');
wicked.upsertWebhookListener('chatbot', {
id: 'chatbot',
url: myUrl
}, callback);
},
getGlobals: function (callback) {
const chatbotGlobals = wicked.getGlobals();
return callback(null, chatbotGlobals);
},
getTemplates: function (callback) {
debug('Getting templates...');
wicked.getChatbotTemplates(function (err, chatbotTemplates) {
if (err)
return callback(err);
debug('Retrieved templates successfully.');
return callback(null, chatbotTemplates);
});
}
}, function (err, results) {
if (err)
return done(err);
app.chatbotGlobals = results.getGlobals;
chatbot.chatbotTemplates = results.getTemplates;
chatbot.initInterestingEvents(app.chatbotGlobals);
return done(null);
});
};
chatbot.deinit = function (app, done) {
debug('deinit()');
wicked.deleteWebhookListener('chatbot', done);
};
chatbot.initInterestingEvents = function (chatbotGlobals) {
debug('initInterestingEvents()');
if (!chatbotGlobals.chatbot ||
!chatbotGlobals.chatbot.targets ||
!chatbotGlobals.chatbot.useChatbot) {
return;
}
for (let message in Messages) {
const thisMessage = Messages[message];
const eventId = thisMessage.entity + '.' + thisMessage.action;
// Store all targets that are interesting for each event
for (let i in chatbotGlobals.chatbot.targets) {
const target = chatbotGlobals.chatbot.targets[i];
if (target.events[message]) {
if (!chatbot.eventTargetMapping[eventId]) {
chatbot.eventTargetMapping[eventId] = [];
}
const messageTemplate = chatbot.chatbotTemplates[message];
if (!messageTemplate)
throw new Error('The chatbot message template is missing for event "' + message + '".');
chatbot.interestingTemplates[eventId] = messageTemplate;
chatbot.eventTargetMapping[eventId].push({
hookUrl: target.hookUrl,
type: target.type,
});
}
}
}
};
chatbot.isEventInteresting = function (event) {
debug('isEventInteresting()');
debug(event);
const eventId = event.entity + '.' + event.action;
return !!chatbot.interestingTemplates[eventId];
};
chatbot.handleEvent = function (app, event, done) {
debug('handleEvent()');
debug(event);
const eventId = event.entity + '.' + event.action;
const messageTemplate = chatbot.interestingTemplates[eventId];
const targets = chatbot.eventTargetMapping[eventId];
if (!event.data)
return done(null);
if (!event.data.userId)
return done(null);
buildViewModel(app, event, function (err, viewModel) {
if (err)
return done(err);
const text = mustache.render(messageTemplate, viewModel);
async.each(targets, function (target, callback) {
// Payload depends on type of messenger
// Post to the hook URL
let payload = {};
switch (target.type) {
case "slack":
payload = {
username: app.chatbotGlobals.chatbot.username,
icon_url: app.chatbotGlobals.chatbot.icon_url,
text: text,
};
break;
case "msteams":
payload = {
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0072C6",
"title": "Notification from " + app.chatbotGlobals.chatbot.username,
"text": text,
};
break;
default:
error("Unknown chatbot target " + target.type);
return callback(null);
}
request.post({
url: target.hookUrl,
json: true,
body: payload
}, function (chatbotErr, apiResponse, apiBody) {
if (chatbotErr)
return callback(chatbotErr);
if (apiResponse.statusCode > 299) {
debug('Posting to Chatbot failed: Status ' + apiResponse.statusCode);
debug(apiResponse);
debug(apiBody);
error(utils.getText(apiBody));
}
return callback(null);
});
}, function (err, results) {
if (err) {
error(err);
}
done(null);
});
});
};
function getPortalUrl(app) {
return app.chatbotGlobals.network.schema + '://' +
app.chatbotGlobals.network.portalHost;
}
function buildViewModel(app, event, callback) {
debug('buildViewModel()');
wicked.getUser(event.data.userId, function (err, userInfo) {
if (err)
return callback(err);
// Try to get the name of the user; but if there is none, just use the email address
// of the user as the user's name (getRegistration must not provoke an error in this case).
wicked.getUserRegistrations('wicked', event.data.userId, function (err, registrations) {
let reg = null;
if (err) {
warn(`Could not retrieve registration for user ${event.data.userId} (${userInfo.email})`);
} else {
// There should be exactly one registration
if (registrations.items.length !== 1) {
warn(`User with user ID ${event.data.userId} does not have a registration for pool 'wicked'`);
reg = {
name: userInfo.email
};
} else {
reg = registrations.items[0];
}
}
const portalUrl = getPortalUrl(app);
let applicationLink = null;
if (event.data.applicationId)
applicationLink = portalUrl + '/applications/' + event.data.applicationId;
callback(null, {
userId: event.data.userId,
email: userInfo.email,
name: reg.name,
apiId: event.data.apiId,
applicationId: event.data.applicationId,
approvalsLink: portalUrl + '/admin/approvals',
userLink: portalUrl + '/users/' + event.data.userId,
applicationLink: applicationLink
});
});
});
}
module.exports = chatbot;