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,
|
setKnownListings,
|
||||||
getKnownListings,
|
getKnownListings,
|
||||||
setNumberOfTotalFoundProviderListings,
|
setNumberOfTotalFoundProviderListings,
|
||||||
getForTesting
|
getForTesting,
|
||||||
} = require('./services/store');
|
} = require('./services/store');
|
||||||
|
|
||||||
const notify = require('./notification/notify');
|
const notify = require('./notification/notify');
|
||||||
@@ -79,7 +79,7 @@ class FredyRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_findNew(listings) {
|
_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) {
|
if (newListings.length === 0) {
|
||||||
throw new NoNewListingsError();
|
throw new NoNewListingsError();
|
||||||
@@ -89,14 +89,14 @@ class FredyRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_notify(newListings) {
|
_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);
|
return Promise.all(sendNotifications).then(() => newListings);
|
||||||
}
|
}
|
||||||
|
|
||||||
_save(newListings) {
|
_save(newListings) {
|
||||||
setKnownListings(this._jobKey, this._providerId, [
|
setKnownListings(this._jobKey, this._providerId, [
|
||||||
...getKnownListings(this._jobKey, this._providerId),
|
...getKnownListings(this._jobKey, this._providerId),
|
||||||
...newListings.map(l => l.id)
|
...newListings.map((l) => l.id),
|
||||||
]);
|
]);
|
||||||
return newListings;
|
return newListings;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,14 @@
|
|||||||
* @param serviceName e.g immoscout
|
* @param serviceName e.g immoscout
|
||||||
* @param newListings an array with newly found listings
|
* @param newListings an array with newly found listings
|
||||||
* @param notificationConfig config of this notification adapter
|
* @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;
|
const { enabled } = notificationConfig.console;
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
return [Promise.resolve()];
|
return [Promise.resolve()];
|
||||||
}
|
}
|
||||||
/* eslint-disable no-console */
|
/* 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 */
|
/* eslint-enable no-console */
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,9 +5,10 @@ const sgMail = require('@sendgrid/mail');
|
|||||||
* @param serviceName e.g immoscout
|
* @param serviceName e.g immoscout
|
||||||
* @param newListings an array with newly found listings
|
* @param newListings an array with newly found listings
|
||||||
* @param notificationConfig config of this notification adapter
|
* @param notificationConfig config of this notification adapter
|
||||||
|
* * @param jobKey name of the current job that is being executed
|
||||||
* @returns {Promise<Chat.PostMessage.Response> | void}
|
* @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;
|
const { apiKey, enabled, receiver, from, templateId } = notificationConfig.sendGrid;
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
return [Promise.resolve()];
|
return [Promise.resolve()];
|
||||||
@@ -17,9 +18,9 @@ exports.send = (serviceName, newListings, notificationConfig) => {
|
|||||||
templateId,
|
templateId,
|
||||||
to: receiver,
|
to: receiver,
|
||||||
from,
|
from,
|
||||||
subject: `Service ${serviceName} found ${newListings.length} new listing(s)`,
|
subject: `Job ${jobKey} | Service ${serviceName} found ${newListings.length} new listing(s)`,
|
||||||
dynamic_template_data: {
|
dynamic_template_data: {
|
||||||
serviceName,
|
serviceName: `Job: (${jobKey}) | Service: ${serviceName}`,
|
||||||
numberOfListings: newListings.length,
|
numberOfListings: newListings.length,
|
||||||
listings: newListings,
|
listings: newListings,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,18 +6,19 @@ const msg = Slack.chat.postMessage;
|
|||||||
* @param serviceName e.g immoscout
|
* @param serviceName e.g immoscout
|
||||||
* @param newListings an array with newly found listings
|
* @param newListings an array with newly found listings
|
||||||
* @param notificationConfig config of this notification adapter
|
* @param notificationConfig config of this notification adapter
|
||||||
|
* * @param jobKey name of the current job that is being executed
|
||||||
* @returns {Promise<Chat.PostMessage.Response> | void}
|
* @returns {Promise<Chat.PostMessage.Response> | void}
|
||||||
*/
|
*/
|
||||||
exports.send = (serviceName, newListings, notificationConfig) => {
|
exports.send = (serviceName, newListings, notificationConfig, jobKey) => {
|
||||||
const { token, channel, enabled } = notificationConfig.slack;
|
const { token, channel, enabled } = notificationConfig.slack;
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
return [Promise.resolve()];
|
return [Promise.resolve()];
|
||||||
}
|
}
|
||||||
return newListings.map(payload =>
|
return newListings.map((payload) =>
|
||||||
msg({
|
msg({
|
||||||
token,
|
token,
|
||||||
channel,
|
channel,
|
||||||
text: `*(${serviceName})* - ${payload.title}`,
|
text: `*(${serviceName} - ${jobKey})* - ${payload.title}`,
|
||||||
attachments: [
|
attachments: [
|
||||||
{
|
{
|
||||||
fallback: payload.title,
|
fallback: payload.title,
|
||||||
@@ -28,23 +29,23 @@ exports.send = (serviceName, newListings, notificationConfig) => {
|
|||||||
{
|
{
|
||||||
title: 'Price',
|
title: 'Price',
|
||||||
value: payload.price,
|
value: payload.price,
|
||||||
short: false
|
short: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Size',
|
title: 'Size',
|
||||||
value: payload.size,
|
value: payload.size,
|
||||||
short: false
|
short: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Address',
|
title: 'Address',
|
||||||
value: payload.address,
|
value: payload.address,
|
||||||
short: false
|
short: false,
|
||||||
}
|
},
|
||||||
],
|
],
|
||||||
footer: 'Powered by Fredy',
|
footer: 'Powered by Fredy',
|
||||||
ts: new Date().getTime() / 1000
|
ts: new Date().getTime() / 1000,
|
||||||
}
|
},
|
||||||
]
|
],
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,33 +1,36 @@
|
|||||||
const TelegramBot = require('tg-yarl');
|
const TelegramBot = require('tg-yarl');
|
||||||
|
|
||||||
const opts = {parse_mode: 'Markdown'};
|
const opts = { parse_mode: 'Markdown' };
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sends new listings to telegram
|
* sends new listings to telegram
|
||||||
* @param serviceName e.g immoscout
|
* @param serviceName e.g immoscout
|
||||||
* @param newListings an array with newly found listings
|
* @param newListings an array with newly found listings
|
||||||
* @param notificationConfig config of this notification adapter
|
* @param notificationConfig config of this notification adapter
|
||||||
|
* * @param jobKey name of the current job that is being executed
|
||||||
* @returns {Promise<Void> | void}
|
* @returns {Promise<Void> | void}
|
||||||
*/
|
*/
|
||||||
exports.send = (serviceName, newListings, notificationConfig) => {
|
exports.send = (serviceName, newListings, notificationConfig, jobKey) => {
|
||||||
const {enabled, token, chatId} = notificationConfig.telegram;
|
const { enabled, token, chatId } = notificationConfig.telegram;
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
return [Promise.resolve()];
|
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 =>
|
message += newListings.map(
|
||||||
`*${shorten(o.title.replace(/\*/g, ''), 45)}*\n` +
|
(o) =>
|
||||||
[o.address, o.price, o.size].join(' | ') + '\n' +
|
`*${shorten(o.title.replace(/\*/g, ''), 45)}*\n` +
|
||||||
`[LINK](${o.link})\n\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) {
|
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';
|
const path = './adapter';
|
||||||
|
|
||||||
/** Read every integration existing in ./adapter **/
|
/** Read every integration existing in ./adapter **/
|
||||||
const adapter = fs
|
const adapter = fs.readdirSync('./lib/notification/adapter').map((integPath) => require(`${path}/${integPath}`));
|
||||||
.readdirSync('./lib/notification/adapter')
|
|
||||||
.map(integPath => require(`${path}/${integPath}`));
|
|
||||||
|
|
||||||
if (adapter.length === 0) {
|
if (adapter.length === 0) {
|
||||||
throw new Error('Please specify at least one notification provider');
|
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
|
//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