Files
fredy/lib/notification/adapter/sendGrid.js

58 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-07-17 15:39:11 +02:00
const sgMail = require('@sendgrid/mail');
const { markdown2Html } = require('../../services/markdown');
2020-07-17 15:39:11 +02:00
/**
* sends a new listing using SendGrid
* @param serviceName e.g immowelt
2020-07-17 15:39:11 +02:00
* @param newListings an array with newly found listings
* @param notificationConfig config of this notification adapter
* @param jobKey name of the current job that is being executed
2020-07-17 15:39:11 +02:00
* @returns {Promise<Chat.PostMessage.Response> | void}
*/
exports.send = ({ serviceName, newListings, notificationConfig, jobKey }) => {
const { apiKey, receiver, from, templateId } = notificationConfig.find((adapter) => adapter.id === 'sendGrid').fields;
2020-07-17 15:39:11 +02:00
sgMail.setApiKey(apiKey);
const msg = {
templateId,
to: receiver,
from,
2020-07-17 16:34:10 +02:00
subject: `Job ${jobKey} | Service ${serviceName} found ${newListings.length} new listing(s)`,
2020-07-17 15:39:11 +02:00
dynamic_template_data: {
2020-07-17 16:34:10 +02:00
serviceName: `Job: (${jobKey}) | Service: ${serviceName}`,
2020-07-17 15:39:11 +02:00
numberOfListings: newListings.length,
listings: newListings,
},
};
return sgMail.send(msg);
};
exports.config = {
id: __filename.slice(__dirname.length + 1, -3),
name: 'SendGrid',
description: 'SendGrid is being used to send new listings via mail.',
readme: markdown2Html('lib/notification/adapter/sendGrid.md'),
fields: {
apiKey: {
type: 'text',
label: 'Api Key',
description: 'The api key needed to access this service.',
},
receiver: {
type: 'email',
label: 'Receiver Email',
description: 'The email address (single one) which Fredy is using to send notifications to.',
},
from: {
type: 'email',
label: 'Sender Email',
description:
'The email address from which Fredy send email. Beware, this email address needs to be verified by Sendgrid.',
},
templateId: {
type: 'text',
label: 'Template Id',
description: 'Sendgrid supports templates which Fredy is using to send out emails that looks awesome ;)',
},
},
};