From 457b4271435b6cf1d2e0301e751eb627fdf1ac25 Mon Sep 17 00:00:00 2001 From: Andrew Van Tassel Date: Sat, 10 Dec 2016 12:28:04 -0700 Subject: [PATCH] Added Slack integration --- README.md | 2 ++ config.sample | 5 +++++ index.js | 41 +++++++++++++++++++++++++++++++++++++++++ package.json | 9 ++++++--- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f87c97e..19f3cc3 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ This app will loop through all the mail in an IMAP account and geo route all messages. You can use the live feed on your mailhops.com account to monitor your incoming messages. Leave it running and it will route incoming messages. +You can also add a [custom Slack integration](https://mailhops.slack.com/apps/build/custom-integration) to post messages to Slack. Use the subjectFilter or fromAddress in [the config](config.sample) to limit the messages posted to Slack. + ```sh # install npm modules npm install diff --git a/config.sample b/config.sample index add936e..b1b3a00 100644 --- a/config.sample +++ b/config.sample @@ -6,5 +6,10 @@ "searchFilter": ["SEEN"] "mailhops": { "api_key": "" + }, + "slack": { "webhookUri": "" + , "channel": "#channel" + , "subjectFilter": "" + , "fromAddress": "" } } diff --git a/index.js b/index.js index 0703ad6..bb6dac9 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var MailListener = require("mail-listener2"); var MailHops = require("mailhops"); +var Slack = require('slack-node'); var chalk = require('chalk'); var notifier = require('node-notifier'); var logUpdate = require('log-update'); @@ -45,6 +46,13 @@ var mailListener = new MailListener(configuration); var mailhops = new MailHops(mhconfiguration); +var slack; +// setup Slack +if(config && config.slack){ + slack = new Slack(); + slack.setWebhook(config.slack.webhookUri); +} + mailListener.start(); // start listening // stop listening @@ -95,6 +103,39 @@ mailListener.on("mail", function(mail, seqno, attributes){ 'time': 5000 }); } + + let slackit = false; + // slack + if(slack){ + // check slack filters + if(!!configuration.slack.fromAddress){ + if(mail.from[0].address.toLowerCase().indexOf(configuration.slack.fromAddress.toLowerCase()) !== -1){ + slackit = true; + } + } else if(!!configuration.slack.subjectFilter){ + if(mail.subject.toLowerCase().indexOf(configuration.slack.subjectFilter.toLowerCase()) !== -1){ + slackit = true; + } + } else { // no filters + slackit = true; + } + if(slackit){ + slack.webhook({ + channel: configuration.slack.channel, + username: "MailHopsBot", + text: 'New mail from '+mail.from[0].name+' '+mail.from[0].address, + attachments: [{ + title: 'MailHops route from '+start.city+', '+start.state+' ('+start.countryCode+') '+Math.round(mail.mailHops.distance.miles)+' mi.', + title_link: mailhops.mapUrl(ips), + text: mail.text + } + ], + icon_emoji: "https://www.mailhops.com/images/mailhops-64.png" + }, function(err, response) { + if(err) console.log('Slack Error',err); + }); + } + } } }); } diff --git a/package.json b/package.json index 6a784a6..d927582 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mailhops-imap-listener", - "version": "1.0.1", + "version": "1.0.2", "description": "A nodejs app for using MailHops API to test and monitor your IMAP account.", "main": "./index.js", "scripts": { @@ -12,7 +12,9 @@ "url": "http://github.com/mailhops/mailhops-imap-listener" }, "keywords": [ - "MailHops" + "MailHops", + "IMAP", + "Slack" ], "author": "Andrew Van Tassel", "license": "MIT", @@ -21,6 +23,7 @@ "log-update": "^1.0.2", "mail-listener2": "^0.2.0", "mailhops": "^2.0.3", - "node-notifier": "^4.6.1" + "node-notifier": "^4.6.1", + "slack-node": "^0.1.8" } }