mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 09:57:38 +00:00
feat(notification): discord integration (#82)
Co-authored-by: Jef LeCompte <jeffreylec@gmail.com>
This commit is contained in:
@@ -42,6 +42,10 @@ const notifications = {
|
||||
accessToken: process.env.TELEGRAM_ACCESS_TOKEN ?? '',
|
||||
chatId: process.env.TELEGRAM_CHAT_ID ?? ''
|
||||
},
|
||||
discord: {
|
||||
webHookUrl: process.env.DISCORD_WEB_HOOK ?? '',
|
||||
notifyGroup: process.env.DISCORD_NOTIFY_GROUP ?? ''
|
||||
},
|
||||
test: process.env.NOTIFICATION_TEST === 'true'
|
||||
};
|
||||
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ async function main() {
|
||||
* Send test email.
|
||||
*/
|
||||
if (Config.notifications.test) {
|
||||
sendNotification('test');
|
||||
sendNotification('http://test.com/', {brand: 'THE BEST BRAND', model: 'VENTUS', oosLabels: [], url: '', cartUrl: ''});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import {Webhook, MessageBuilder} from 'discord-webhook-node';
|
||||
import {Config} from '../config';
|
||||
import {Logger} from '../logger';
|
||||
import {Link} from '../store/model';
|
||||
|
||||
const hook = new Webhook(Config.notifications.discord.webHookUrl);
|
||||
const notifyGroup = Config.notifications.discord.notifyGroup;
|
||||
|
||||
export function sendDiscordMessage(cartUrl: string, link: Link) {
|
||||
(async () => {
|
||||
try {
|
||||
const embed = new MessageBuilder();
|
||||
embed.setTitle('Stock Notification');
|
||||
embed.addField('URL', cartUrl, true);
|
||||
embed.addField('Brand', link.brand, true);
|
||||
embed.addField('Model', link.model, true);
|
||||
|
||||
if (notifyGroup !== '') {
|
||||
embed.addField('Attention', notifyGroup, true);
|
||||
}
|
||||
|
||||
embed.setColor(0x76B900);
|
||||
embed.setTimestamp();
|
||||
await hook.send(embed);
|
||||
Logger.info(`✔ discord message sent: ${cartUrl}`);
|
||||
} catch (error) {
|
||||
Logger.error(error);
|
||||
}
|
||||
})();
|
||||
}
|
||||
@@ -5,10 +5,12 @@ import {playSound} from './sound';
|
||||
import {sendSlackMessage} from './slack';
|
||||
import {sendPushoverNotification} from './pushover';
|
||||
import {sendTelegramMessage} from './telegram';
|
||||
import {sendDiscordMessage} from './discord';
|
||||
import {Link} from '../store/model';
|
||||
|
||||
const notifications = Config.notifications;
|
||||
|
||||
export function sendNotification(cartUrl: string) {
|
||||
export function sendNotification(cartUrl: string, link: Link) {
|
||||
if (notifications.email.username && notifications.email.password) {
|
||||
sendEmail(cartUrl);
|
||||
}
|
||||
@@ -21,6 +23,10 @@ export function sendNotification(cartUrl: string) {
|
||||
sendTelegramMessage(cartUrl);
|
||||
}
|
||||
|
||||
if (notifications.discord.webHookUrl) {
|
||||
sendDiscordMessage(cartUrl, link);
|
||||
}
|
||||
|
||||
if (notifications.phone.number) {
|
||||
const carrier = notifications.phone.carrier.toLowerCase();
|
||||
if (carrier && notifications.phone.availableCarriers.has(carrier)) {
|
||||
|
||||
+1
-1
@@ -74,7 +74,7 @@ export async function lookup(browser: Browser, store: Store) {
|
||||
await open(givenUrl);
|
||||
}
|
||||
|
||||
sendNotification(givenUrl);
|
||||
sendNotification(givenUrl, link);
|
||||
}
|
||||
|
||||
await page.close();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
interface Link {
|
||||
export interface Link {
|
||||
cartUrl?: string;
|
||||
brand: string;
|
||||
model: string;
|
||||
|
||||
Reference in New Issue
Block a user