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

60 lines
1.8 KiB
JavaScript
Raw Normal View History

const { markdown2Html } = require('../../services/markdown');
2021-05-14 10:36:17 +02:00
const axios = require('axios');
/**
* sends new listings to telegram
* @param serviceName e.g immowelt
* @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}
*/
exports.send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
const { token, chatId } = notificationConfig.find((adapter) => adapter.id === 'telegram').fields;
2021-05-14 10:36:17 +02:00
let message = `Job: ${jobKey} | Service <b>${serviceName}</b> found <b>${newListings.length}</b> new listings:\n\n`;
2020-07-17 16:34:10 +02:00
message += newListings.map(
(o) =>
2021-05-14 10:36:17 +02:00
`<b>${shorten(o.title.replace(/\*/g, ''), 45)}</b>\n` +
2020-07-17 16:34:10 +02:00
[o.address, o.price, o.size].join(' | ') +
'\n' +
2021-05-14 10:36:17 +02:00
`<a href="${o.link}">${o.link}</a>\n\n`
2020-07-17 16:34:10 +02:00
);
2021-05-14 10:36:17 +02:00
return axios.post(`https://api.telegram.org/bot${token}/sendMessage`, {
chat_id: chatId,
text: message,
parse_mode: 'HTML',
disable_web_page_preview: true,
});
};
function shorten(str, len = 30) {
2020-07-17 16:34:10 +02:00
return str.length > len ? str.substring(0, len) + '...' : str;
}
/**
* exported config is being used in the frontend to generate the fields
* incoming values will be the keys (and values) of the fields
*
*/
exports.config = {
id: __filename.slice(__dirname.length + 1, -3),
name: 'Telegram',
readme: markdown2Html('lib/notification/adapter/telegram.md'),
description: 'Fredy will send new listings to your mobile, using Telegram.',
fields: {
token: {
type: 'text',
label: 'Token',
description: 'The token needed to access this service.',
},
chatId: {
type: 'chatId',
label: 'Chat Id',
description: 'The chat id to send messages to you.',
},
},
};