Skip to content

Commit

Permalink
feat: notify (#379)
Browse files Browse the repository at this point in the history
* feat(env): Add notification config variables for new notification service

* refactor(config): Remove discord configuration and add notify configuration

* refactor(utils): Simplify message sending logic in Chad flex method
  • Loading branch information
wajeht authored Aug 31, 2024
1 parent 3fb2693 commit 406da41
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ DISCORD_ID=""
DISCORD_TOKEN=""
DISCORD_URL=""

# notify
NOTIFY_URL="https://notify.jaw.dev/"
NOTIFY_X_API_KEY="deez"

# email
EMAIL_HOST="mailhot"
EMAIL_PORT=1025
Expand Down
11 changes: 5 additions & 6 deletions src/config/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ export const email = {
auth_pass: process.env.EMAIL_AUTH_PASS,
};

export const discord = {
id: process.env.DISCORD_ID,
token: process.env.DISCORD_TOKEN,
url: process.env.DISCORD_URL,
};

export const admin = {
email: process.env.ADMIN_EMAIL,
username: process.env.ADMIN_USERNAME,
Expand All @@ -76,3 +70,8 @@ export const GITHUB = {
export const MY_IP = process.env.MY_IP;

export const SENTRY_URL = process.env.SENTRY_URL;

export const notify = {
url: process.env.NOTIFY_URL,
xApiKey: process.env.NOTIFY_X_API_KEY
}
68 changes: 38 additions & 30 deletions src/utils/chad.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
import logger from './logger.js';
import { discord, env } from '../config/env.js';
import { env, notify } from '../config/env.js';

import axios from 'axios';

/* It sends a message to a Discord channel */
// https://gist.github.com/Birdie0/78ee79402a4301b1faf412ab5f1cdcf9
export default class Chad {
static async flex(msg, object = null) {
try {
let params = null;

// use different format to send if we have object passed in
if (object == null) {
params = { username: 'Chad', content: msg };
} else {
params = {
username: 'Chad',
content: msg,
embeds: [
{
title: msg,
description: object,
},
],
};
}
if (env !== 'production') {
const res = await fetch(notify.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-KEY': notify.xApiKey,
},
body: JSON.stringify({
message: msg,
details: object,
}),
});

let res = null;
console.log(await res.json());

// only send chad message to discord in production environment
// prettier-ignore
if (env === 'production') {
res = await axios({ method: 'POST', headers: { 'Content-Type': 'application/json', }, data: JSON.stringify(params), url: discord.url });
if (res?.status === 204) logger.info(`Chad sent ${msg}`);
} else {
logger.warn(`Skipping Chad message in dev environment!`)
if (res.ok) {
logger.info(`Chad sent ${msg}`);
} else {
logger.error(`Failed to send Chad message: ${res.statusText}`);
}

} else {
logger.warn('Skipping Chad message in dev environment!');
}

} catch (e) {
logger.error(e);

logger.error(e.message);

await fetch(notify.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-KEY': notify.xApiKey,
},
body: JSON.stringify({
message: e.message,
details: e.stack,
}),
});

}
}
}

0 comments on commit 406da41

Please sign in to comment.