1
0
mirror of https://github.com/MailHops/mailhops-imap-listener.git synced 2025-05-15 09:20:08 -07:00

Added Slack integration

This commit is contained in:
Andrew Van Tassel 2016-12-10 12:28:04 -07:00
parent f2042f5c99
commit 457b427143
4 changed files with 54 additions and 3 deletions

View File

@ -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

View File

@ -6,5 +6,10 @@
"searchFilter": ["SEEN"]
"mailhops": {
"api_key": ""
},
"slack": { "webhookUri": ""
, "channel": "#channel"
, "subjectFilter": ""
, "fromAddress": ""
}
}

View File

@ -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);
});
}
}
}
});
}

View File

@ -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"
}
}