Files
fredy/lib/notification/adapter/telegram.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

const TelegramBot = require('tg-yarl');
2020-07-17 16:34:10 +02:00
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
2020-07-17 16:34:10 +02:00
* * @param jobKey name of the current job that is being executed
* @returns {Promise<Void> | void}
*/
2020-07-17 16:34:10 +02:00
exports.send = (serviceName, newListings, notificationConfig, jobKey) => {
const { enabled, token, chatId } = notificationConfig.telegram;
if (!enabled) {
return [Promise.resolve()];
}
2020-07-17 16:34:10 +02:00
const bot = new TelegramBot(token);
2020-07-17 16:34:10 +02:00
let message = `Job: ${jobKey} | Service _${serviceName}_ found _${newListings.length}_ new listings:\n\n`;
2020-07-17 16:34:10 +02:00
message += newListings.map(
(o) =>
`*${shorten(o.title.replace(/\*/g, ''), 45)}*\n` +
[o.address, o.price, o.size].join(' | ') +
'\n' +
`[LINK](${o.link})\n\n`
);
2020-07-17 16:34:10 +02:00
return bot.sendMessage(chatId, message, opts);
};
function shorten(str, len = 30) {
2020-07-17 16:34:10 +02:00
return str.length > len ? str.substring(0, len) + '...' : str;
}