forked from Sparragus/slack-bot-edward-snowden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (57 loc) · 1.89 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
var Slack = require('slack-client');
var http = require('http');
var token = process.env.SLACK_API_TOKEN;
var autoReconnect = true;
var autoMark = true;
var slack = new Slack(token, autoReconnect, autoMark);
slack.on('open', function() {
console.log('Welcome to Slack. You are @' + slack.self.name + ' of ' + slack.team.name);
});
function onMessageToEdwardSnowden (message) {
// do not listen to bots
if (message.subtype === 'bot_message') {
return;
}
function randomUsername () {
return Math.random().toString(36).substring(2);
}
function randomHexColor() {
return Math.random().toString(16).substring(2, 2+6);
}
var text = message.text;
var anonymousUser = randomUsername();
var someColor = randomHexColor();
var anonymousIcon = 'http://placehold.it/80/' + someColor + '/'+ someColor;
var response = {
text: text,
username: anonymousUser,
icon_url: anonymousIcon
};
// post message to #general
var anonymousChannel = slack.getChannelByName('general');
anonymousChannel.postMessage(response);
}
slack.on('message', function (message) {
var channelGroupOrDM = slack.getChannelGroupOrDMByID(message.channel);
if (channelGroupOrDM.is_im) {
onMessageToEdwardSnowden(message);
}
});
slack.on('error', function (error) {
console.error('Error:', error);
});
slack.login();
// I don't want this app to crash in case someone sends an HTTP request, so lets implement a simple server
//Lets define a port we want to listen to
const PORT = process.env.PORT || 3000;
//We need a function which handles requests and send response
function handleRequest(request, response){
response.end("");
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT, function(){
//Callback triggered when server is successfully listening. Hurray!
console.log('Server listening on: http://localhost:%s', PORT);
});