2025-12-11 10:40:55 +01:00
|
|
|
/*
|
2026-01-12 15:00:36 +01:00
|
|
|
* Copyright (c) 2026 by Christian Kellner.
|
2025-12-11 10:40:55 +01:00
|
|
|
* Licensed under Apache-2.0 with Commons Clause and Attribution/Naming Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-13 13:42:43 +01:00
|
|
|
import Slack from 'slack';
|
|
|
|
|
import { markdown2Html } from '../../services/markdown.js';
|
2025-08-30 21:21:34 +02:00
|
|
|
import { normalizeImageUrl } from '../../utils.js';
|
|
|
|
|
|
|
|
|
|
const buildBlocks = (serviceName, jobKey, p) => {
|
|
|
|
|
const blocks = [
|
|
|
|
|
{
|
|
|
|
|
type: 'header',
|
|
|
|
|
text: { type: 'plain_text', text: `New Listing from ${serviceName} (${jobKey})`, emoji: false },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: 'section',
|
|
|
|
|
text: { type: 'mrkdwn', text: `*<${p.link}|${p.title}>*` },
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
type: 'section',
|
|
|
|
|
fields: [
|
|
|
|
|
{ type: 'mrkdwn', text: `*Price*\n${p.price ?? 'n/a'}` },
|
|
|
|
|
{ type: 'mrkdwn', text: `*Size*\n${p.size ?? 'n/a'}` },
|
|
|
|
|
{ type: 'mrkdwn', text: `*Address*\n${p.address ?? 'n/a'}` },
|
2020-07-17 16:34:10 +02:00
|
|
|
],
|
2025-08-30 21:21:34 +02:00
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const img = normalizeImageUrl(p.image);
|
|
|
|
|
if (img) {
|
|
|
|
|
blocks.push({
|
|
|
|
|
type: 'image',
|
|
|
|
|
image_url: img,
|
|
|
|
|
alt_text: p.title || 'listing image',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blocks.push({
|
|
|
|
|
type: 'context',
|
|
|
|
|
elements: [{ type: 'mrkdwn', text: 'Powered by Fredy' }],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return blocks;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
|
|
|
|
|
const { token, channel } = notificationConfig.find((a) => a.id === config.id).fields;
|
|
|
|
|
|
|
|
|
|
return Promise.allSettled(
|
|
|
|
|
newListings.map((p) =>
|
|
|
|
|
Slack.chat.postMessage({
|
|
|
|
|
token,
|
|
|
|
|
channel,
|
|
|
|
|
text: `${serviceName} ${jobKey}: ${p.title}`,
|
|
|
|
|
blocks: buildBlocks(serviceName, jobKey, p),
|
|
|
|
|
unfurl_links: false,
|
|
|
|
|
unfurl_media: false,
|
|
|
|
|
}),
|
|
|
|
|
),
|
2020-02-26 09:05:20 +01:00
|
|
|
);
|
2018-01-20 20:23:27 +01:00
|
|
|
};
|
2025-08-30 21:21:34 +02:00
|
|
|
|
2023-03-13 13:42:43 +01:00
|
|
|
export const config = {
|
|
|
|
|
id: 'slack',
|
2021-01-21 16:09:23 +01:00
|
|
|
name: 'Slack',
|
|
|
|
|
readme: markdown2Html('lib/notification/adapter/slack.md'),
|
|
|
|
|
description: 'Fredy will send new listings to the slack channel of your choice..',
|
|
|
|
|
fields: {
|
|
|
|
|
token: {
|
|
|
|
|
type: 'text',
|
|
|
|
|
label: 'Token',
|
|
|
|
|
description: 'The token needed to send notifications to slack.',
|
|
|
|
|
},
|
|
|
|
|
channel: {
|
|
|
|
|
type: 'channel',
|
|
|
|
|
label: 'Channel',
|
|
|
|
|
description: 'The channel where fredy should send notifications to.',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|