Send slack message on bouncer-block

This commit is contained in:
2017-02-16 14:06:05 +00:00
parent deefcaadfc
commit c939c09991
2 changed files with 11 additions and 8 deletions

View File

@@ -8,9 +8,9 @@ slack.setWebhook(slackWebhook);
// Debounce sending the slack message to avoid spamming // Debounce sending the slack message to avoid spamming
const slackTimers = {}; const slackTimers = {};
export const sendSlackMsg = function sendSlackMsg(text) { export const sendSlackMsg = function sendSlackMsg(text, channel) {
slack.webhook({ slack.webhook({
channel: '#wedding-rsvps', channel,
username: 'weddingbot', username: 'weddingbot',
text, text,
}, err => { }, err => {
@@ -20,12 +20,12 @@ export const sendSlackMsg = function sendSlackMsg(text) {
}); });
}; };
export const sendSlackMsgWithDebounce = function sendSlackMsgWithDebounce(text, key) { export const sendSlackMsgWithDebounce = function sendSlackMsgWithDebounce(text, channel, key) {
if (key) { if (key) {
// Stop previous msg // Stop previous msg
clearTimeout(slackTimers[key]); clearTimeout(slackTimers[key]);
slackTimers[key] = setTimeout(() => { slackTimers[key] = setTimeout(() => {
sendSlackMsg(text); sendSlackMsg(text, channel);
}, 10000); }, 10000);
} }
}; };

View File

@@ -24,7 +24,7 @@ import assets from './assets'; // eslint-disable-line import/no-unresolved
import { port, auth, starters, mains, rsvpEndDate } from './config'; import { port, auth, starters, mains, rsvpEndDate } from './config';
import Person from './data/models/Person'; import Person from './data/models/Person';
import Rollbar from './core/rollbar'; import Rollbar from './core/rollbar';
import { sendSlackMsgWithDebounce } from './core/slack'; import { sendSlackMsg, sendSlackMsgWithDebounce } from './core/slack';
import sequelize from './data/sequelize'; import sequelize from './data/sequelize';
const app = express(); const app = express();
@@ -39,6 +39,9 @@ bouncer.blocked = (req, res, next, remaining) => {
suffix = 'minutes'; suffix = 'minutes';
} }
const body = `You have made too many incorrect attempts. Please wait ${time} ${suffix}.`; const body = `You have made too many incorrect attempts. Please wait ${time} ${suffix}.`;
const uAgent = req.headers['user-agent'];
const ip = req.headers['x-forwarded-for'];
sendSlackMsg(`Bouncer block\nIP address: ${ip}\nUser agent: ${uAgent}\nDuration: ${time} ${suffix}`, '#wedding-site');
res.status(429).send(body); res.status(429).send(body);
}; };
@@ -166,7 +169,7 @@ app.post('/rsvp/save', (req, res) => {
const main = (data.attending && data.main !== -1) ? `\nMain: ${mains[data.main]}` : ''; const main = (data.attending && data.main !== -1) ? `\nMain: ${mains[data.main]}` : '';
const dietary = (data.attending && data.dietary) ? `\nDietary requirements: ${data.dietary}` : ''; const dietary = (data.attending && data.dietary) ? `\nDietary requirements: ${data.dietary}` : '';
sendSlackMsgWithDebounce(`${data.firstname} ${data.lastname} has saved their RSVP:${attending}${starter}${main}${dietary}`, req.body.key); sendSlackMsgWithDebounce(`${data.firstname} ${data.lastname} has saved their RSVP:${attending}${starter}${main}${dietary}`, '#wedding-rsvps', req.body.key);
} }
res.json({ res.json({
success: true, success: true,