forked from TargetProcess/slack-anonymous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (85 loc) · 3.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
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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var request = require('request');
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: false }));
function createError(errorMessage) {
return {
error: errorMessage
};
}
function getUsageHelp(commandName) {
function createSample(target) {
return commandName + ' *' + target + '* I know what you did last summer';
}
var text = 'Expected usage: \n' +
commandName + ' help -- Displays help message.\n' +
createSample('@user') + ' -- Sends to the specified user.\n' +
createSample('#channel') + ' -- Sends to the specified public channel.\n' +
createSample('group') + ' -- Sends to the specified private group.\n' +
createSample(':here') + ' -- Sends to the current group/channel/DM where you type this command.';
return text;
}
function getFullHelp(commandName) {
var text =
'Allows to send anonymous messages to users, channels and groups.\n' +
'The most convenient and safe way is to open up a conversation with slackbot in Slack and type the commands there, so that nobody detects that you are typing and you don\'t accidentally reveal yourself by typing an invalid command.\n' +
'Messages and authors are not stored, and the sources are available at <https://github.com/TargetProcess/slack-anonymous>.\n' +
'\n' +
getUsageHelp(commandName);
return text;
}
function createResponsePayload(requestBody) {
if (!requestBody) {
return createError('Request is empty');
}
var text = requestBody.text;
var command = requestBody.command;
if (!text || text === 'help') {
return createError(getFullHelp(command));
}
var splitted = text.split(" ");
if (splitted.length <= 1) {
return createError(getUsageHelp(command));
}
var target = splitted[0];
var remainingText = splitted.slice(1).join(' ');
remainingText = 'Birisi diyor ki "' + remainingText + '"';
if (target === ':here') {
return {
channel: requestBody.channel_id,
text: remainingText
};
}
return {
channel: target,
text: remainingText
};
}
app.post('/', function(req, response) {
var payloadOption = createResponsePayload(req.body);
if (payloadOption.error) {
response.end(payloadOption.error);
return;
}
request({
url: process.env.POSTBACK_URL,
json: payloadOption,
method: 'POST'
}, function (error) {
if(error) {
response.end('Anonim mesajınızı iletemedik: ' + JSON.stringify(error));
} else {
response.end('Gönderildi! :cop:');
}
});
});
app.get('/', function(request, response) {
response.write('HELLO THERE');
response.end();
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});