Telegram: Use job name instead of ID and link in title (#44)

This commit is contained in:
Jochen Schalanda
2022-01-26 14:39:53 +01:00
committed by GitHub
parent 47adb88cb5
commit 70e78492ec

View File

@@ -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`, {