import { IncomingWebhook } from '@slack/client' import config from '../config' import { logger } from './logger' const slackChannelToUrlMap = { walterChat: process.env.SLACK_WEBHOOK_URL_CHAT_WALTER, order: process.env.SLACK_WEBHOOK_URL_ORDER, newProjectCondoManager: process.env.SLACK_WEBHOOK_URL_NEW_PROJECT_FROM_CONDO_MANAGER, } type SendInputType = { channel: 'walterChat' | 'order' | 'newProjectCondoManager' subject: string text?: string } export async function send({ channel, subject, text }: SendInputType) { // Some project are still sync locally and we want to send slack notification to them // That's the reason for 'channel === 'newProjectCondoManager'' if (config.isProduction || channel === 'newProjectCondoManager') { const webhook = new IncomingWebhook(slackChannelToUrlMap[channel]) await webhook.send(`${subject ? `*${subject}*` : ''}${text ? `\n\n${text}` : ''}`) logger.info(`New slack message sent to channel ${channel}. Subject: ${subject}`) } else { logger.info(`Would have send ${subject} - ${text}`) } }