mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
sending job key with the listings
This commit is contained in:
@@ -3,7 +3,7 @@ const {
|
||||
setKnownListings,
|
||||
getKnownListings,
|
||||
setNumberOfTotalFoundProviderListings,
|
||||
getForTesting
|
||||
getForTesting,
|
||||
} = require('./services/store');
|
||||
|
||||
const notify = require('./notification/notify');
|
||||
@@ -79,7 +79,7 @@ class FredyRuntime {
|
||||
}
|
||||
|
||||
_findNew(listings) {
|
||||
const newListings = listings.filter(o => getKnownListings(this._jobKey, this._providerId).indexOf(o.id) === -1);
|
||||
const newListings = listings.filter((o) => getKnownListings(this._jobKey, this._providerId).indexOf(o.id) === -1);
|
||||
|
||||
if (newListings.length === 0) {
|
||||
throw new NoNewListingsError();
|
||||
@@ -89,14 +89,14 @@ class FredyRuntime {
|
||||
}
|
||||
|
||||
_notify(newListings) {
|
||||
const sendNotifications = notify.send(this._providerId, newListings, this._notificationConfig);
|
||||
const sendNotifications = notify.send(this._providerId, newListings, this._notificationConfig, this._jobKey);
|
||||
return Promise.all(sendNotifications).then(() => newListings);
|
||||
}
|
||||
|
||||
_save(newListings) {
|
||||
setKnownListings(this._jobKey, this._providerId, [
|
||||
...getKnownListings(this._jobKey, this._providerId),
|
||||
...newListings.map(l => l.id)
|
||||
...newListings.map((l) => l.id),
|
||||
]);
|
||||
return newListings;
|
||||
}
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
* @param serviceName e.g immoscout
|
||||
* @param newListings an array with newly found listings
|
||||
* @param notificationConfig config of this notification adapter
|
||||
* @param jobKey name of the current job that is being executed
|
||||
*/
|
||||
exports.send = (serviceName, newListings, notificationConfig) => {
|
||||
exports.send = (serviceName, newListings, notificationConfig, jobKey) => {
|
||||
const { enabled } = notificationConfig.console;
|
||||
if (!enabled) {
|
||||
return [Promise.resolve()];
|
||||
}
|
||||
/* eslint-disable no-console */
|
||||
return [Promise.resolve(console.info(`Found entry from service ${serviceName}:`, newListings))];
|
||||
return [Promise.resolve(console.info(`Found entry from service ${serviceName}, Job: ${jobKey}:`, newListings))];
|
||||
/* eslint-enable no-console */
|
||||
};
|
||||
|
||||
@@ -5,9 +5,10 @@ const sgMail = require('@sendgrid/mail');
|
||||
* @param serviceName e.g immoscout
|
||||
* @param newListings an array with newly found listings
|
||||
* @param notificationConfig config of this notification adapter
|
||||
* * @param jobKey name of the current job that is being executed
|
||||
* @returns {Promise<Chat.PostMessage.Response> | void}
|
||||
*/
|
||||
exports.send = (serviceName, newListings, notificationConfig) => {
|
||||
exports.send = (serviceName, newListings, notificationConfig, jobKey) => {
|
||||
const { apiKey, enabled, receiver, from, templateId } = notificationConfig.sendGrid;
|
||||
if (!enabled) {
|
||||
return [Promise.resolve()];
|
||||
@@ -17,9 +18,9 @@ exports.send = (serviceName, newListings, notificationConfig) => {
|
||||
templateId,
|
||||
to: receiver,
|
||||
from,
|
||||
subject: `Service ${serviceName} found ${newListings.length} new listing(s)`,
|
||||
subject: `Job ${jobKey} | Service ${serviceName} found ${newListings.length} new listing(s)`,
|
||||
dynamic_template_data: {
|
||||
serviceName,
|
||||
serviceName: `Job: (${jobKey}) | Service: ${serviceName}`,
|
||||
numberOfListings: newListings.length,
|
||||
listings: newListings,
|
||||
},
|
||||
|
||||
@@ -6,18 +6,19 @@ const msg = Slack.chat.postMessage;
|
||||
* @param serviceName e.g immoscout
|
||||
* @param newListings an array with newly found listings
|
||||
* @param notificationConfig config of this notification adapter
|
||||
* * @param jobKey name of the current job that is being executed
|
||||
* @returns {Promise<Chat.PostMessage.Response> | void}
|
||||
*/
|
||||
exports.send = (serviceName, newListings, notificationConfig) => {
|
||||
exports.send = (serviceName, newListings, notificationConfig, jobKey) => {
|
||||
const { token, channel, enabled } = notificationConfig.slack;
|
||||
if (!enabled) {
|
||||
return [Promise.resolve()];
|
||||
}
|
||||
return newListings.map(payload =>
|
||||
return newListings.map((payload) =>
|
||||
msg({
|
||||
token,
|
||||
channel,
|
||||
text: `*(${serviceName})* - ${payload.title}`,
|
||||
text: `*(${serviceName} - ${jobKey})* - ${payload.title}`,
|
||||
attachments: [
|
||||
{
|
||||
fallback: payload.title,
|
||||
@@ -28,23 +29,23 @@ exports.send = (serviceName, newListings, notificationConfig) => {
|
||||
{
|
||||
title: 'Price',
|
||||
value: payload.price,
|
||||
short: false
|
||||
short: false,
|
||||
},
|
||||
{
|
||||
title: 'Size',
|
||||
value: payload.size,
|
||||
short: false
|
||||
short: false,
|
||||
},
|
||||
{
|
||||
title: 'Address',
|
||||
value: payload.address,
|
||||
short: false
|
||||
}
|
||||
short: false,
|
||||
},
|
||||
],
|
||||
footer: 'Powered by Fredy',
|
||||
ts: new Date().getTime() / 1000
|
||||
}
|
||||
]
|
||||
ts: new Date().getTime() / 1000,
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,33 +1,36 @@
|
||||
const TelegramBot = require('tg-yarl');
|
||||
|
||||
const opts = {parse_mode: 'Markdown'};
|
||||
|
||||
const opts = { parse_mode: 'Markdown' };
|
||||
|
||||
/**
|
||||
* sends new listings to telegram
|
||||
* @param serviceName e.g immoscout
|
||||
* @param newListings an array with newly found listings
|
||||
* @param notificationConfig config of this notification adapter
|
||||
* * @param jobKey name of the current job that is being executed
|
||||
* @returns {Promise<Void> | void}
|
||||
*/
|
||||
exports.send = (serviceName, newListings, notificationConfig) => {
|
||||
const {enabled, token, chatId} = notificationConfig.telegram;
|
||||
if (!enabled) {
|
||||
return [Promise.resolve()];
|
||||
}
|
||||
exports.send = (serviceName, newListings, notificationConfig, jobKey) => {
|
||||
const { enabled, token, chatId } = notificationConfig.telegram;
|
||||
if (!enabled) {
|
||||
return [Promise.resolve()];
|
||||
}
|
||||
|
||||
const bot = new TelegramBot(token);
|
||||
const bot = new TelegramBot(token);
|
||||
|
||||
let message = `Service _${serviceName}_ found _${newListings.length}_ new listings:\n\n`;
|
||||
let message = `Job: ${jobKey} | Service _${serviceName}_ found _${newListings.length}_ new listings:\n\n`;
|
||||
|
||||
message += newListings.map(o =>
|
||||
`*${shorten(o.title.replace(/\*/g, ''), 45)}*\n` +
|
||||
[o.address, o.price, o.size].join(' | ') + '\n' +
|
||||
`[LINK](${o.link})\n\n`);
|
||||
message += newListings.map(
|
||||
(o) =>
|
||||
`*${shorten(o.title.replace(/\*/g, ''), 45)}*\n` +
|
||||
[o.address, o.price, o.size].join(' | ') +
|
||||
'\n' +
|
||||
`[LINK](${o.link})\n\n`
|
||||
);
|
||||
|
||||
return bot.sendMessage(chatId, message, opts);
|
||||
return bot.sendMessage(chatId, message, opts);
|
||||
};
|
||||
|
||||
function shorten(str, len = 30) {
|
||||
return str.length > len ? str.substring(0, len) + '...' : str;
|
||||
}
|
||||
return str.length > len ? str.substring(0, len) + '...' : str;
|
||||
}
|
||||
|
||||
@@ -2,15 +2,13 @@ const fs = require('fs');
|
||||
const path = './adapter';
|
||||
|
||||
/** Read every integration existing in ./adapter **/
|
||||
const adapter = fs
|
||||
.readdirSync('./lib/notification/adapter')
|
||||
.map(integPath => require(`${path}/${integPath}`));
|
||||
const adapter = fs.readdirSync('./lib/notification/adapter').map((integPath) => require(`${path}/${integPath}`));
|
||||
|
||||
if (adapter.length === 0) {
|
||||
throw new Error('Please specify at least one notification provider');
|
||||
}
|
||||
|
||||
exports.send = (serviceName, payload, notificationConfig) => {
|
||||
exports.send = (serviceName, payload, notificationConfig, jobKey) => {
|
||||
//this is not being used in tests, therefor adapter are always set
|
||||
return adapter.map(a => a.send(serviceName, payload, notificationConfig));
|
||||
return adapter.map((a) => a.send(serviceName, payload, notificationConfig, jobKey));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user