-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
105 lines (87 loc) · 2.92 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
// Load Config File
var config = require('./secrets/config.json');
// Load libraries; instantiate express app and socket.io
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var tmi = require('tmi.js');
var ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');
// Setup Watson Tone Analyzer
var tone_analyzer = new ToneAnalyzerV3({
url: config.watson.new.url,
version: config.watson.version_date,
iam_apikey: config.watson.new.apikey
});
// Set up options for connection to twitch chat
// Add channels in the config.json file
var tmi_options = {
options: {
debug: true
},
connection: {
cluster: "aws",
reconnect: true
},
identity: config.twitch_identity,
channels: config.twitch_channels
};
// Connect to twitch
var client = new tmi.client(tmi_options);
client.connect();
// Emit hello world announcement on first chat connection
client.on('connected', function(address, port) {
client.action(config.twitch_channels[0], "Hello World");
});
// Serve any files in the 'vendor' directory as static resources
// This is where js libraries for the client are stored
app.use('/watson-twitch-tone-analysis', express.static('vendor'))
// Serve index.html on /
app.get('/watson-twitch-tone-analysis', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/anger', function(req, res){
res.sendFile(__dirname + '/anger.html');
});
// Start the web server on port 3000
http.listen(3000, function(){
console.log('listening on *:3000');
});
// Setup a websocket with any web client that connects
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
app.get('/watson-twitch-tone-analysis/channels', function (req, res, next) {
// tell tmi to join the channel
// set a timer so that tmi eventually leaves the channel
// 5 minutes right now
var twitch_channel = req.query.twitch_channel;
client.join(twitch_channel);
var foo = setTimeout(function(){
client.part(twitch_channel);
}, 300000);
next();
//res.send('OK - Joined channel! Note, will leave channel in 2 minutes!');
res.redirect('/watson-twitch-tone-analysis')
// template the frontend so it filters messages
// currently only sends ok, user needs to refresh main page to see results
});
// When a chat message comes in, process it with watson tone analysis
// and send that to any clients connected via websockets
// This is the meat of the proram
client.on('chat', function(channel, user, message, self) {
tone_analyzer.tone({ text: message },
function(err, tone) {
if (err)
console.log(err);
else
io.emit('chat message', tone['document_tone']);
//io.emit('twitch_channel_" + twitch_channel, tone['document_tone']);
});
});