Skip to content

Commit

Permalink
feat: slack integration (#34)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan Gentis <[email protected]>
  • Loading branch information
Esg450 and Evan Gentis authored Sep 18, 2020
1 parent c2a210c commit c0a881a
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
EMAIL_USERNAME="[email protected]"
EMAIL_PASSWORD="secretpassword"
SLACK_CHANNEL="SlackChannelName"
SLACK_TOKEN="slack-token"
STORES="bestbuy,bandh,nvidia"
NOTIFICATION_METHODS="email"
173 changes: 173 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"winston": "^3.3.3"
},
"devDependencies": {
"@slack/web-api": "^5.12.0",
"@types/node": "^14.11.1",
"@types/nodemailer": "^6.4.0",
"@types/puppeteer": "^3.0.2",
Expand Down
11 changes: 10 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ const page = {

const stores = process.env.STORES ?? 'nvidia';

const notificationMethods = process.env.NOTIFICATION_METHODS ?? 'email';

const slack = {
channel: process.env.SLACK_CHANNEL,
token: process.env.SLACK_TOKEN
};

export const Config = {
email,
notifications,
page,
rateLimitTimeout: 5000,
stores
stores,
slack,
notificationMethods
};
7 changes: 6 additions & 1 deletion src/notification/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import {Config} from '../config';
import sendEmail from './email';
import sendSlaskMessage from './slack';

export default function sendNotification(cartUrl: string) {
if (Config.notifications.email) {
if (Config.notificationMethods.toLocaleLowerCase().includes('email')) {
sendEmail(cartUrl);
}

if (Config.notificationMethods.toLocaleLowerCase().includes('slack')) {
sendSlaskMessage(cartUrl);
}
}
23 changes: 23 additions & 0 deletions src/notification/slack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {WebClient} from '@slack/web-api';
import {Config} from '../config';
import {Logger} from '../logger';

const channel = Config.slack.channel ?? '';
const token = Config.slack.token ?? '';
const web = new WebClient(token);

export default function sendSlackMessage(text: string) {
(async () => {
try {
const result = await web.chat.postMessage({text, channel});
if (!result.ok) {
Logger.error(result.error);
return;
}

Logger.info(`✔ slack message sent to '${channel}': ${text}`);
} catch (error) {
Logger.error(error);
}
})();
}

0 comments on commit c0a881a

Please sign in to comment.