diff --git a/README.md b/README.md index 15a9e5e..f05387c 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ You will find further information on the [schnack page](https://schnack.cool/). * slack * rss * sendmail +* mailgun ### Who is behind Schnack? diff --git a/config.tpl.json b/config.tpl.json index 4fe9785..65898df 100644 --- a/config.tpl.json +++ b/config.tpl.json @@ -41,6 +41,12 @@ }, "slack": { "webhook_url": "xxxxx" + }, + "mailgun": { + "api_key": "xxxxx", + "domain": "xxxxx", + "from": "xxxxx", + "to": "xxxxx" } }, "date_format": "MMMM DD, YYYY - h:mm a" diff --git a/package.json b/package.json index 973a430..2fe714a 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "express": "^4.16.4", "express-session": "^1.15.6", "lodash.countby": "^4.6", + "mailgun-js": "^0.22.0", "marked": "^0.3.19", "moment": "^2.22.2", "nconf": "^0.10.0", diff --git a/src/push/index.js b/src/push/index.js index 33e436e..9219d4a 100644 --- a/src/push/index.js +++ b/src/push/index.js @@ -5,6 +5,7 @@ const Pushover = require('pushover-notifications'); const queries = require('../db/queries'); const slack = require('./slack'); const sendmail = require('./sendmail'); +const mailgun = require('./mailgun'); const { send_file, send_string, @@ -111,4 +112,3 @@ function init(app, db, awaiting_moderation) { module.exports = { init }; - diff --git a/src/push/mailgun.js b/src/push/mailgun.js new file mode 100644 index 0000000..be82327 --- /dev/null +++ b/src/push/mailgun.js @@ -0,0 +1,37 @@ +const mailgun = require('mailgun-js'); +const config = require('../config'); +const schnackEvents = require('../events'); + +const notify = config.get('notify'); + + + +if (notify.mailgun) { + const client = mailgun({ + apiKey: notify.mailgun.api_key, + domain: notify.mailgun.domain + }) + schnackEvents.on('new-comment', (event) => { + const postUrl = config.get('page_url').replace('%SLUG%', event.slug); + const user = event.user.display_name || event.user.name; + const from = `${user} <${notify.mailgun.from}>` + const to = `Schnack Admin <${notify.mailgun.to}>` + const subject = `New comment: ${postUrl}`; + const text = + ` + New comment on your post ${postUrl} + + Author: ${user} + + ${event.comment} + + You can see all comments on this post here: + ${postUrl}#comments + + Permalink: ${postUrl}#comment-${event.id} + `.split(/\n/).join('\r\n').trim(); + client.messages().send({ from, to, subject, text }, function (err, body) { + console.error(err); + }); + }); +}