Files
fredy/lib/notification/adapter/telegram.js
Christian Kellner 770d30af95 Concurrent-Jobs (#7)
Fredy 2.0.0 introduces the concept of search jobs. You can now configure multiple of these jobs running in the same instance of Fredy. Also a new API has been created 🎉
2020-02-26 09:05:20 +01:00

33 lines
994 B
JavaScript

const TelegramBot = require('tg-yarl');
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
* @returns {Promise<Void> | void}
*/
exports.send = (serviceName, newListings, notificationConfig) => {
const {enabled, token, chatId} = notificationConfig.telegram;
if (!enabled) {
return [Promise.resolve()];
}
const bot = new TelegramBot(token);
let message = `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`);
return bot.sendMessage(chatId, message, opts);
};
function shorten(str, len = 30) {
return str.length > len ? str.substring(0, len) + '...' : str;
}