mirror of
https://github.com/opelly27/streetmerchant.git
synced 2026-05-20 05:17:35 +00:00
36 lines
903 B
TypeScript
36 lines
903 B
TypeScript
import {Link, Store} from '../store/model';
|
|
import {Print, logger} from '../logger';
|
|
import {WebClient} from '@slack/web-api';
|
|
import {config} from '../config';
|
|
|
|
const slack = config.notifications.slack;
|
|
const channel = slack.channel;
|
|
const token = slack.token;
|
|
const web = new WebClient(token);
|
|
|
|
export function sendSlackMessage(link: Link, store: Store) {
|
|
if (slack.channel && slack.token) {
|
|
logger.debug('↗ sending slack message');
|
|
|
|
(async () => {
|
|
const givenUrl = link.cartUrl ? link.cartUrl : link.url;
|
|
|
|
try {
|
|
const result = await web.chat.postMessage({
|
|
channel,
|
|
text: `${Print.inStock(link, store)}\n${givenUrl}`
|
|
});
|
|
|
|
if (!result.ok) {
|
|
logger.error('✖ couldn\'t send slack message', result);
|
|
return;
|
|
}
|
|
|
|
logger.info('✔ slack message sent');
|
|
} catch (error) {
|
|
logger.error('✖ couldn\'t send slack message', error);
|
|
}
|
|
})();
|
|
}
|
|
}
|