mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3aa512db3 | ||
|
|
8361d9c8ff |
@@ -1,6 +1,19 @@
|
|||||||
const { markdown2Html } = require('../../services/markdown');
|
const { markdown2Html } = require('../../services/markdown');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* splitting an array into chunks because Telegram only allows for messages up to
|
||||||
|
* 4096 chars, thus we have to split messages into chunks
|
||||||
|
* @param inputArray
|
||||||
|
* @param perChunk
|
||||||
|
*/
|
||||||
|
const arrayChunks = (inputArray, perChunk) =>
|
||||||
|
inputArray.reduce((all, one, i) => {
|
||||||
|
const ch = Math.floor(i / perChunk);
|
||||||
|
all[ch] = [].concat(all[ch] || [], one);
|
||||||
|
return all;
|
||||||
|
}, []);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sends new listings to telegram
|
* sends new listings to telegram
|
||||||
* @param serviceName e.g immowelt
|
* @param serviceName e.g immowelt
|
||||||
@@ -12,22 +25,28 @@ const axios = require('axios');
|
|||||||
exports.send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
|
exports.send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
|
||||||
const { token, chatId } = notificationConfig.find((adapter) => adapter.id === 'telegram').fields;
|
const { token, chatId } = notificationConfig.find((adapter) => adapter.id === 'telegram').fields;
|
||||||
|
|
||||||
let message = `Job: ${jobKey} | Service <b>${serviceName}</b> found <b>${newListings.length}</b> new listings:\n\n`;
|
//we have to split messages into chunk, because otherwise messages are going to become too big and will fail
|
||||||
|
const chunks = arrayChunks(newListings, 3);
|
||||||
|
|
||||||
message += newListings.map(
|
const promises = chunks.map((chunk) => {
|
||||||
(o) =>
|
let message = `Job: ${jobKey} | Service <b>${serviceName}</b> found <b>${newListings.length}</b> new listings:\n\n`;
|
||||||
`<b>${shorten(o.title.replace(/\*/g, ''), 45)}</b>\n` +
|
message += chunk.map(
|
||||||
[o.address, o.price, o.size].join(' | ') +
|
(o) =>
|
||||||
'\n' +
|
`<b>${shorten(o.title.replace(/\*/g, ''), 45)}</b>\n` +
|
||||||
`<a href="${o.link}">${o.link}</a>\n\n`
|
[o.address, o.price, o.size].join(' | ') +
|
||||||
);
|
'\n' +
|
||||||
|
`<a href="${o.link}">${o.link}</a>\n\n`
|
||||||
|
);
|
||||||
|
|
||||||
return axios.post(`https://api.telegram.org/bot${token}/sendMessage`, {
|
return axios.post(`https://api.telegram.org/bot${token}/sendMessage`, {
|
||||||
chat_id: chatId,
|
chat_id: chatId,
|
||||||
text: message,
|
text: message,
|
||||||
parse_mode: 'HTML',
|
parse_mode: 'HTML',
|
||||||
disable_web_page_preview: true,
|
disable_web_page_preview: true,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return Promise.all(promises);
|
||||||
};
|
};
|
||||||
|
|
||||||
function shorten(str, len = 30) {
|
function shorten(str, len = 30) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "fredy",
|
"name": "fredy",
|
||||||
"version": "5.3.1",
|
"version": "5.3.2",
|
||||||
"description": "[F]ind [R]eal [E]states [d]amn eas[y].",
|
"description": "[F]ind [R]eal [E]states [d]amn eas[y].",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js",
|
"start": "node index.js",
|
||||||
|
|||||||
Reference in New Issue
Block a user