mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
ADD: Telegram bot as notification provider
This commit is contained in:
@@ -70,11 +70,7 @@ class Fredy {
|
||||
}
|
||||
|
||||
_notify(newListings) {
|
||||
const sendNotifications = newListings.map(payload => {
|
||||
return notify.send(this._source.name, payload);
|
||||
}
|
||||
);
|
||||
|
||||
const sendNotifications = notify.send(this._source.name, newListings);
|
||||
return Promise.all(sendNotifications).then(() => newListings)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@ const config = require('../../../conf/config.json');
|
||||
/**
|
||||
* simply prints out the found data to the console
|
||||
* @param serviceName e.g immoscout
|
||||
* @param payload the actual payload that is used to construct the message
|
||||
* @param newListings an array with newly found listings
|
||||
* @returns {Promise<Void> | void}
|
||||
*/
|
||||
exports.send = (serviceName, payload) => {
|
||||
return Promise.resolve(console.info(`Found entry from service ${serviceName}:`, payload))
|
||||
exports.send = (serviceName, newListings) => {
|
||||
return Promise.resolve(console.info(`Found entry from service ${serviceName}:`, newListings))
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,11 +7,11 @@ const {token, channel} = config.notification.slack;
|
||||
/**
|
||||
* sends a new listing to slack
|
||||
* @param serviceName e.g immoscout
|
||||
* @param payload the actual payload that is used to construct the message
|
||||
* @param newListings an array with newly found listings
|
||||
* @returns {Promise<Chat.PostMessage.Response> | void}
|
||||
*/
|
||||
exports.send = (serviceName, payload) => {
|
||||
return msg({
|
||||
exports.send = (serviceName, newListings) => {
|
||||
return newListings.map(payload => msg({
|
||||
token,
|
||||
channel,
|
||||
text: `*(${serviceName})* - ${payload.title}`,
|
||||
@@ -42,7 +42,7 @@ exports.send = (serviceName, payload) => {
|
||||
ts: new Date().getTime() / 1000
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
@@ -50,5 +50,5 @@ exports.send = (serviceName, payload) => {
|
||||
* each integration needs to implement this method
|
||||
*/
|
||||
exports.enabled = () => {
|
||||
return config.notification.slack.enabled;
|
||||
return config.notification.slack.enabled;
|
||||
};
|
||||
|
||||
35
lib/notification/adapter/telegram.js
Normal file
35
lib/notification/adapter/telegram.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const config = require('../../../conf/config.json');
|
||||
const TelegramBot = require('tg-yarl');
|
||||
|
||||
const opts = { parse_mode: 'Markdown' };
|
||||
const bot = new TelegramBot(config.notification.telegram.token);
|
||||
|
||||
|
||||
/**
|
||||
* sends new listings to telegram
|
||||
* @param serviceName e.g immoscout
|
||||
* @param newListings an array with newly found listings
|
||||
* @returns {Promise<Void> | void}
|
||||
*/
|
||||
exports.send = (serviceName, newListings) => {
|
||||
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(config.notification.telegram.chatId, message, opts);
|
||||
};
|
||||
|
||||
/**
|
||||
* each integration needs to implement this method
|
||||
*/
|
||||
exports.enabled = () => {
|
||||
return config.notification.telegram.enabled;
|
||||
};
|
||||
|
||||
|
||||
function shorten (str, len = 30) {
|
||||
return str.length > len ? str.substring(0, len) + '...' : str;
|
||||
}
|
||||
Reference in New Issue
Block a user