-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
200 lines (170 loc) · 6.37 KB
/
app.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
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const bodyParser = require('body-parser');
const Botkit = require('botkit');
const _ = require('lodash');
const mongoose = require('mongoose');
const Song = require('./song');
const User = require('./user');
const {getYoutubeIdFromUrl, getTitleForYoutubeId, getYoutubeUrlFromId, YOUTUBE_MATCHER, getFirstSearchResultBySearchTerm, sanitizeYoutubeUrl} = require('./youtube-helper');
mongoose.Promise = require('bluebird');
mongoose.connect(process.env.MONGODB_URI, {useMongoClient: true});
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const slackConfig = {
interactive_replies: true,
token: process.env.SLACK_BOT_TOKEN,
clientId: process.env.SLACK_CLIENT_ID,
clientSecret: process.env.SLACK_CLIENT_SECRET,
scopes: ['bot']
};
const controller = Botkit.slackbot(slackConfig);
const bot = controller.spawn(slackConfig);
bot.startRTM((err, bot, payload) => {
});
let sockets = [];
let getSockets = () => {
return {
emit: (id, obj) => {
_.each(sockets, socket => socket.emit(id, obj));
}
}
};
const songQueue = require('./songqueue');
controller.hears(/^(hi|hello|hey|yo|help)$/i, ['direct_message'], (bot, message) => {
bot.reply(message, `Hello! Just send me a YouTube song URL and I will add it to the playlist!
Other commands:
- search/find <search term>: to search a song from YouTube.
- play
- pause
- volume 0..1
- next: skip to next song
- current: display currently playing song`
);
});
controller.hears(/^play$/i, ['direct_message'], (bot, message) => {
getSockets().emit('playerStatus', true);
addReaction(bot, message, 'ok_hand');
});
controller.hears(/^(stop|pause)$/i, ['direct_message'], (bot, message) => {
getSockets().emit('playerStatus', false);
addReaction(bot, message, 'ok_hand');
});
controller.hears(/^next$/i, ['direct_message'], (bot, message) => {
songQueue.nextSong((song) => getSockets().emit('next', song.url));
getSockets().emit('playerStatus', true);
addReaction(bot, message, 'ok_hand');
});
// controller.hears(/^shuffle$/i, ['direct_message'], (bot, message) => {
// songQueue.refreshPlaylist();
// addReaction(bot, message, 'ok_hand');
// });
controller.hears(/^current/i, ['direct_message'], (bot, message) => {
const song = songQueue.getCurrentSong();
const title = song && song.url ? song.url : "No song playing right now :disappointed: Send me some songs to play!";
bot.reply(message, title);
});
controller.hears(/^queue/i, ['direct_message'], (bot, message) => {
const queue = songQueue.getQueue();
bot.reply(message, "TODO....");
});
controller.hears(/^volume (\d+([.,]\d+)?$)/i, ['direct_message'], (bot, message) => {
getSockets().emit('volume', Number(message.match[1]));
addReaction(bot, message, 'ok_hand');
});
controller.hears(/^(search|find) (.+)/i, ['direct_message'], (bot, message) => {
const searchTerm = message.match[2];
console.log(searchTerm);
getFirstSearchResultBySearchTerm(searchTerm, (data) => {
console.log(data);
bot.startConversation(message, (err, convo) => {
if (err) return;
convo.say(`Found: ${data.title} (${getYoutubeUrlFromId(data.id)})`);
convo.ask('Add this song?', [
{
pattern: bot.utterances.yes,
callback: (response, convo) => {
convo.next();
}
},
{
pattern: bot.utterances.no,
callback: (response, convo) => {
convo.stop();
}
},
{
default: true,
callback: (response, convo) => {
convo.repeat();
convo.next();
}
}
]);
convo.on('end', (convo) => {
if (convo.status === 'completed') {
addSongToUser(bot, message, getYoutubeUrlFromId(data.id));
} else {
bot.reply(message, 'OK, nevermind!');
}
});
});
});
});
const addReaction = (bot, message, reaction) => {
bot.api.reactions.add({
token: process.env.SLACK_API_TOKEN,
timestamp: message.ts,
channel: message.channel,
name: reaction
});
};
controller.hears(YOUTUBE_MATCHER, ['direct_message'], (bot, message) => {
addSongToUser(bot, message, message.match[0]);
});
const addSongToUser = (bot, message, youtubeUrl) => {
bot.api.users.info({user: message.user}, (error, response) => {
let {name, real_name} = response.user;
createSong(message.user, name, youtubeUrl, (song) => {
songQueue.addSong(song);
if (!songQueue.getCurrentSong().url) {
songQueue.nextSong((song) => getSockets().emit('next', song.url));
getSockets().emit('playerStatus', true);
}
});
});
bot.reply(message, 'Song added to playlist! :musical_note:');
};
const createSong = (userId, name, youtubeUrl, cb) => {
const id = getYoutubeIdFromUrl(youtubeUrl);
const youtubeUrlSanitized = sanitizeYoutubeUrl(youtubeUrl);
cb({
name: name,
userId: userId,
id: id,
url: youtubeUrlSanitized,
});
};
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'));
});
server.listen((process.env.PORT || 3001), (err, res) => console.log(`Server started.`));
io.on('connection', (socket) => {
console.log('Socket.io connection established.' + socket.id);
socket.on('disconnect', (disconnectedSocket) => sockets = _.reject(sockets, (soc) => soc.id === socket.id));
socket.on('ping', () => socket.emit('pong'));
socket.emit('playerStatus', true);
socket.on('getNext', () => {
songQueue.nextSong((song) => socket.emit('next', song.url));
getSockets().emit('playerStatus', true);
});
songQueue.nextSong((song) => socket.emit('next', song.url));
sockets.push(socket);
});