-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·114 lines (93 loc) · 3.08 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
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
/**
* Created by pawan.venugopal on 3/20/17.
*/
"use strict";
require('dotenv').config();
var Botkit= require('botkit'),
os = require('os'),
http = require('http'),
request = require('request'),
watson = require('watson-developer-cloud'),
fs = require('fs');
var accessToken = "";
var platform = null;
if (!process.env.RC_CLIENT_ID || !process.env.RC_CLIENT_SECRET || !process.env.PORT) {
console.log('Error: Specify clientId clientSecret and port in environment');
process.exit(1);
}
var controller = Botkit.glipbot({
debug: true
}).configureGlipApp({
clientId: process.env.RC_CLIENT_ID,
clientSecret: process.env.RC_CLIENT_SECRET,
redirectUri: process.env.OAUTH_REDIRECT_URI,
apiRoot: process.env.RC_SERVER
});
readAccessToken()
function readAccessToken(){
try {
fs.accessSync('token.dat');
accessToken = fs.readFileSync('token.dat', 'utf8');
}catch (e) {
accessToken = ""
}
}
function storeAccessToken(accessToken){
fs.writeFile('token.dat', accessToken, function(err) {
if(err)
console.log(err)
})
}
var bot = controller.spawn({});
controller.setupWebserver(process.env.PORT || 3000, function(err, webserver){
controller.createWebhookEndpoints(webserver, bot, function () {
console.log("Online");
});
controller.createOauthEndpoints(webserver, bot, accessToken, function(err, req, res, token) {
if(err) {
res.status(500).send('ERROR: ' + err);
} else {
platform = controller.getRCPlatform();
storeAccessToken(token);
//res.send('Success!');
}
})
});
var personality_insight = watson.personality_insights({
username: process.env.WATSON_USERNAME,
password: process.env.WATSON_PASSWORD,
url: process.env.WATSON_URL,
version: process.env.WATSON_VERSION,
version_date: process.env.WATSON_VERSION_DATE
});
controller.hears(['watson: analyze'], 'message_received', function (bot,message) {
console.log("In Hears:" + JSON.stringify(message));
controller.getRCPlatform().get('/team-messaging/v1/chats/'+message.channel+'/posts').then(function(history) {
console.log("History: " + history.json());
var messages = [];
var data = history.json();
for(var i=0; i < data.records.length; i++){
messages.push(data.records[i].text);
}
var corpus = messages.join("\n");
console.log(corpus);
personality_insight.profile(
{
text: corpus,
language: 'en'
},
function (err, response) {
if(err){
console.log('error:', err)
}else{
controller.startTask(bot, message, function (task, convo) {
var top5 = response.personality;
for (var c = 0; c < top5.length; c++) {
convo.say('This channel has ' + Math.round(top5[c].percentage*100) + '% ' + top5[c].name);
}
})
}
}
)
});
});