mirror of
https://github.com/orangecoding/fredy.git
synced 2026-06-16 12:31:07 +00:00
improve pushover notification service
This commit is contained in:
@@ -7,9 +7,11 @@ export const send = ({ serviceName, newListings, notificationConfig, jobKey }) =
|
|||||||
const job = getJob(jobKey);
|
const job = getJob(jobKey);
|
||||||
const jobName = job == null ? jobKey : job.name;
|
const jobName = job == null ? jobKey : job.name;
|
||||||
const promises = newListings.map((newListing) => {
|
const promises = newListings.map((newListing) => {
|
||||||
const message = `Address: ${newListing.address} Size: ${newListing.size.replace(/2m/g, '$m^2$')} Price: ${
|
const message = `
|
||||||
newListing.price
|
Address: ${newListing.address}
|
||||||
}`;
|
Size: ${newListing.size.replace(/2m/g, '$m^2$')}
|
||||||
|
Price: ${newListing.price}
|
||||||
|
Link: ${newListing.link}`;
|
||||||
return fetch(server, {
|
return fetch(server, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
|||||||
@@ -1,50 +1,73 @@
|
|||||||
import { markdown2Html } from '../../services/markdown.js';
|
import {markdown2Html} from '../../services/markdown.js';
|
||||||
import { getJob } from '../../services/storage/jobStorage.js';
|
import {getJob} from '../../services/storage/jobStorage.js';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
export const send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
|
export const send = ({serviceName, newListings, notificationConfig, jobKey}) => {
|
||||||
const { token, user, device } = notificationConfig.find((adapter) => adapter.id === config.id).fields;
|
const {token, user, device} = notificationConfig.find((adapter) => adapter.id === config.id).fields;
|
||||||
const job = getJob(jobKey);
|
const job = getJob(jobKey);
|
||||||
const jobName = job == null ? jobKey : job.name;
|
const jobName = job == null ? jobKey : job.name;
|
||||||
const promises = newListings.map((newListing) => {
|
const promises = newListings.map((newListing) => {
|
||||||
const title = `${jobName} at ${serviceName}: ${newListing.title}`;
|
const title = `${jobName} at ${serviceName}: ${newListing.title}`;
|
||||||
const message = `Address: ${newListing.address}\nSize: ${newListing.size}\nPrice: ${newListing.price}\nLink: ${newListing.link}`;
|
const message = `Address: ${newListing.address}\nSize: ${newListing.size}\nPrice: ${newListing.price}\nLink: ${newListing.link}`;
|
||||||
return fetch('https://api.pushover.net/1/messages.json', {
|
return fetch('https://api.pushover.net/1/messages.json', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
token: token,
|
token: token,
|
||||||
user: user,
|
user: user,
|
||||||
message: message,
|
message: message,
|
||||||
device: device,
|
device: device,
|
||||||
title: title,
|
title: title,
|
||||||
}),
|
}),
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
return Promise.all(promises);
|
return Promise.all(promises)
|
||||||
|
.then((responses) => {
|
||||||
|
// Convert all responses to JSON
|
||||||
|
return Promise.all(responses.map((response) => response.json()));
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
// Check for errors in the data
|
||||||
|
const error = data
|
||||||
|
.map((item) => (item.errors != null && item.errors.length > 0 ? item.errors.join(', ') : null))
|
||||||
|
.filter((err) => err !== null);
|
||||||
|
|
||||||
|
if (error.length > 0) {
|
||||||
|
// Reject with the combined error messages
|
||||||
|
return Promise.reject(error.join('; '));
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
return Promise.resolve();
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
return Promise.reject(error);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
id: 'pushover',
|
id: 'pushover',
|
||||||
name: 'Pushover',
|
name: 'Pushover',
|
||||||
readme: markdown2Html('lib/notification/adapter/pushover.md'),
|
readme: markdown2Html('lib/notification/adapter/pushover.md'),
|
||||||
description: 'Fredy will send new listings to your mobile using Pushover.',
|
description: 'Fredy will send new listings to your mobile using Pushover.',
|
||||||
fields: {
|
fields: {
|
||||||
token: {
|
token: {
|
||||||
type: 'text',
|
type: 'text',
|
||||||
label: 'API token',
|
label: 'API token',
|
||||||
description: 'Your application\'s API token.',
|
description: 'Your application\'s API token.',
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
type: 'text',
|
||||||
|
label: 'User key',
|
||||||
|
description: 'Your user/group key.',
|
||||||
|
},
|
||||||
|
device: {
|
||||||
|
type: 'text',
|
||||||
|
label: 'Device name',
|
||||||
|
description: 'The device name to send your notification to. Messages may be addressed to multiple specific devices by joining them with a comma.',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
user: {
|
|
||||||
type: 'text',
|
|
||||||
label: 'User key',
|
|
||||||
description: 'Your user/group key.',
|
|
||||||
},
|
|
||||||
device: {
|
|
||||||
type: 'text',
|
|
||||||
label: 'Device name',
|
|
||||||
description: 'The device name to send your notification to. Messages may be addressed to multiple specific devices by joining them with a comma.',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "fredy",
|
"name": "fredy",
|
||||||
"version": "11.0.1",
|
"version": "11.0.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 prod.js",
|
"start": "node prod.js",
|
||||||
|
|||||||
Reference in New Issue
Block a user