ADD: Telegram bot as notification provider

This commit is contained in:
Christian Kellner
2018-02-06 13:57:32 +01:00
parent 97d7ba7f9a
commit f0d9c0cb00
9 changed files with 579 additions and 23 deletions

View File

@@ -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)
}

View File

@@ -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))
};
/**

View File

@@ -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;
};

View 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;
}