-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
129 lines (103 loc) · 3.79 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const slackEventsApi = require('@slack/events-api');
const SlackClient = require('@slack/client').WebClient;
const express = require('express');
const fs = require('fs'); //file system library
const papa = require('papaparse');
const file = fs.createReadStream('./fruits.csv');
//const file = fs.createReadStream('./harassment-lexicon.csv');
const fruitnames = [];
papa.parse(file, {
worker: true,
step: function(result){ //callback step executed when parsing completes
var temp = result.data;
var temp1 = temp[0];
length = temp1.length;
for(var i = 0; i < length ; i++){
var name = temp1[i];
fruitnames.push(name.toLowerCase());
}
},
complete: function(results, file){
//do nothing
}
});
//General class imports
var appMentionHelper = require('./appMentionHelper');
var passiveListener = require('./passiveListener');
//method imports
var sendHi = require('./appMentionHelper').sendHi;
var negativity = require('./passiveListener').negativity;
// regex for matching subreddits
// var subredditRegex = /(.*\s)*/?(r\/).*/i;
// *** Initialize an Express application
const app = express();
// *** Initialize a client with your access token
const slack = new SlackClient(process.env.SLACK_ACCESS_TOKEN);
// *** Initialize event adapter using signing secret from environment variables ***
const slackEvents = slackEventsApi.createEventAdapter(process.env.SLACK_SIGNING_SECRET);
// Homepage
app.get('/', (req, res) => {
const url = `https://${req.hostname}/slack/events`;
res.setHeader('Content-Type', 'text/html');
return res.send(`<pre>Copy this link to paste into the event URL field: <a href="${url}">${url}</a></pre>`);
});
// *** Plug the event adapter into the express app as middleware ***
app.use('/slack/events', slackEvents.expressMiddleware());
// *** Attach listeners to the event adapter ***
// *** Greeting any user that says "hi" ***
slackEvents.on('app_mention', (message) => {
if(message.text.includes('hi')){
sendHi(slack, message);
}
// if(message.text.includes('fruit')){
// slack.chat.postMessage({
// channel: message.channel,
// text: `FRUIT <@${message.user}> did you just ask for a fruit?`
// })
// .catch(console.error);
// }
});
// *** Responding to reactions with the same emoji ***
slackEvents.on('reaction_added', (event) => {
// Respond to the reaction back with the same emoji
slack.chat.postMessage({
channetl: event.item.channel,
text: `:${event.reaction}:`
})
.catch(console.error);
});
//checks message for negativity
slackEvents.on('message', (message) => {
if (message.subtype == "bot_message") return; //blacklist bots
if (message.subtype == "slackbot_response") return; //blacklist slackbot
//if (message.channel != "CEH8S2P1T") return; //whitelist testing server
var str = message.text;
var res = str.split(" ");
var arrayLength = res.length;
for (var i = 0; i < arrayLength; i++) {
if(fruitnames.includes(res[i].toLowerCase())) {
slack.chat.postMessage({
channel: message.channel,
text: `That's not inclusive. :thumbsdown:`,
thread_ts: message.ts
})
return;
}
}
negativity(slack, message);
});
// *** Handle errors ***
slackEvents.on('error', (error) => {
if (error.code === slackEventsApi.errorCodes.TOKEN_VERIFICATION_FAILURE) {
// This error type also has a `body` propery containing the request body which failed verification.
console.error(`An unverified request was sent to the Slack events Request URL. Request body: \
${JSON.stringify(error.body)}`);
} else {
console.error(`An error occurred while handling a Slack event: ${error.message}`);
}
});
// Start the express application
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`server listening on port ${port}`);
});