mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
Telegram: Use job name instead of ID and link in title (#44)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
const { markdown2Html } = require('../../services/markdown');
|
const { markdown2Html } = require('../../services/markdown');
|
||||||
|
const { getJob } = require('../../services/storage/jobStorage');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,23 +20,24 @@ const arrayChunks = (inputArray, perChunk) =>
|
|||||||
* @param serviceName e.g immowelt
|
* @param serviceName e.g immowelt
|
||||||
* @param newListings an array with newly found listings
|
* @param newListings an array with newly found listings
|
||||||
* @param notificationConfig config of this notification adapter
|
* @param notificationConfig config of this notification adapter
|
||||||
* * @param jobKey name of the current job that is being executed
|
* @param jobKey name of the current job that is being executed
|
||||||
* @returns {Promise<Void> | void}
|
* @returns {Promise<Void> | void}
|
||||||
*/
|
*/
|
||||||
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;
|
||||||
|
const job = getJob(jobKey);
|
||||||
|
const jobName = job == null ? jobKey : job.name;
|
||||||
|
|
||||||
//we have to split messages into chunk, because otherwise messages are going to become too big and will fail
|
//we have to split messages into chunk, because otherwise messages are going to become too big and will fail
|
||||||
const chunks = arrayChunks(newListings, 3);
|
const chunks = arrayChunks(newListings, 3);
|
||||||
|
|
||||||
const promises = chunks.map((chunk) => {
|
const promises = chunks.map((chunk) => {
|
||||||
let message = `Job: ${jobKey} | Service <b>${serviceName}</b> found <b>${newListings.length}</b> new listings:\n\n`;
|
let message = `<i>${jobName}</i> (${serviceName}) found <b>${newListings.length}</b> new listings:\n\n`;
|
||||||
message += chunk.map(
|
message += chunk.map(
|
||||||
(o) =>
|
(o) =>
|
||||||
`<b>${shorten(o.title.replace(/\*/g, ''), 45)}</b>\n` +
|
`<a href="${o.link}"><b>${shorten(o.title.replace(/\*/g, ''), 45).trim()}</b></a>\n` +
|
||||||
[o.address, o.price, o.size].join(' | ') +
|
[o.address, o.price, o.size].join(' | ') +
|
||||||
'\n' +
|
'\n\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`, {
|
||||||
|
|||||||
Reference in New Issue
Block a user