-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
index.js
40 lines (34 loc) · 1.01 KB
/
index.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
import './settings.js';
import './models/Rooms.js';
import googleLanguage from '@google-cloud/language';
let languageClient;
RocketChat.settings.get('GoogleNaturalLanguage_ServiceAccount', (key, value) => {
if (value) {
try {
languageClient = googleLanguage({
credentials: JSON.parse(value),
});
} catch (e) {
languageClient = null;
console.error('Error parsing Google Natural Language credential.', e);
}
}
});
const setRoomSentiment = function(message) {
if (!languageClient) {
return;
}
languageClient.detectSentiment(message.msg, Meteor.bindEnvironment((error, result) => {
if (!error) {
RocketChat.models.Rooms.setSentiment(message.rid, result);
}
}));
return message;
};
RocketChat.settings.get('GoogleNaturalLanguage_Enabled', (key, value) => {
if (value) {
RocketChat.callbacks.add('afterSaveMessage', setRoomSentiment, RocketChat.callbacks.priority.MEDIUM, 'GoogleNaturalLanguage');
} else {
RocketChat.callbacks.remove('afterSaveMessage', 'GoogleNaturalLanguage');
}
});