npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-slack-mailgun

v2.0.1

Published

Slack integration for Mailgun notifications.

Downloads

39

Readme

Slack Mailgun (SlackGun) Build Status

Slack Mailgun (SlackGun) is a simple webhook system to make it easy to route Mailgun's Webhooks to Slack. This was built because there existed no simple way that I saw that could map Mailgun to Slack without paying an arm and a leg for it.

Why pay for it when it's easy enough to simply build?

Install

You can integrate SlackGun into your existing web application or use it standalone from the command line.

Using it with Express

npm install node-slack-mailgun

var SlackGun = require('node-slack-mailgun');
var slack_hook_url = 'https://hooks.slack.com/services/aaa/bbb/ccc';
var mailgun_apikey = '123abc';
var express = require('express');

var app = express();

app.use('/api/mailgun', SlackGun({
	slack: { hook: slack_hook_url },
	mailgun: { apikey: mailgun_apikey }
}));

Now any requests from Mailgun to /api/mailgun will be routed to slack as a message.

Using it with the build in http/s Node.js Module

Not yet implemented

Using it from the command line

Not fully implemented

npm install -g node-slack-mailgun

Then you should be able to run it from the command line (tested on Linux):

$ slackgun

  Usage: slackgun [options] <hook>

  Options:

    -h, --help              output usage information
    -V, --version           output the version number
    -p, --port [port]       Specify Port to Listen On. Default: <3000>
    -h, --hostname [ip]     Specify hostname to Listen On. Default: <0.0.0.0>
    -u, --url [url]         Specify URL Endpoint. Default: '/'
    -m, --mailgun [apikey]  Specify mailgun API key
    -v, --verbose           Turn on verbose error debugging.

Usage

node-slack-mailgun accepts a few options when initalizing.

var SlackGun = require('node-slack-mailgun');

SlackGun({
	slack: { // Options for slack
		hook: 'url', // Required. The hook URL for posting messages to slack.
		options: 'object', // Optional. Options to pass to https://github.com/xoxco/node-slack#install-slack
		channel: '#general', // Optional. By default we'll send to the #general channel.
		username: 'Mailgun', // Optional. By default we'll send using the "Mailgun" username to slack.
		icon_emoji: 'mailbox_with_mail', // Optional. By default we'll set the icon to :mailbox_with_mail:
	},
	mailgun: { // Options for mailgun
		apikey: 'string' // Optional. Used for verifying the HMAC token sent with a request.
	}
});

Customize Messages Sent To Slack

By default we send a generic message rendered by Mustache.js as attachments to Slack.

If you want to customize these messages, you have a few options.

Option #1 - String replacement

Give us a string in the options text for slack of that particular type of event, and we'll render it using Mustache and the body of the Mailgun post. You can use any of Slack's input decoractions here as well, such as using *bold* and _italics_.

If you don't want to write an individual string for each one, just include 'all' in there, and we'll use that for all event types if you don't specifically set one. In the below settings, all would include [complained, bounced, dropped, delivered] but not [opened, clicked, unsubscribed] because those are specified.

If you would prefer not to listen or accept an event type, set it to false and we will ignore it.

Catch all events

var SlackGun = require('node-slack-mailgun');

SlackGun({
	slack: { // Options for slack
		hook: 'url', // Required. The hook URL for posting messages to slack.
	},
	templates: { // Options for templates
		opened: '*{{recipient}}* _{{event}}_ our email.', // "[email protected] opened our email."
		clicked: '*{{recipient}}* _{{event}}_ {{{url}}}', // "[email protected] clicked https://example.com/link"
		unsubscribed: '*{{recipient}}* _{{event}}_ from {{mailing-list}}', // "[email protected] unsubscribed from [email protected]"
		all: '*{{recipient}}*\n*Event:* _{{event}}_' 
	}
});

Ignore Some Events

var SlackGun = require('node-slack-mailgun');

SlackGun({
	slack: { // Options for slack
		hook: 'url', // Required. The hook URL for posting messages to slack.
	},
	templates: { // Options for templates
		opened: false, // ignore this event
		clicked: '*{{recipient}}* _{{event}}_ {{{url}}}', // "[email protected] clicked https://example.com/link"
		unsubscribed: '*{{recipient}}* _{{event}}_ from {{mailing-list}}', // "[email protected] unsubscribed from [email protected]"
	}
});

Option #2 - Attachment Array

Not yet fully implemented. If you return an array in your custom function (like below), we'll attempt to send it as an attachment instead of text.

Option #3 - Custom Function

You can supply us a function and we'll give you the variables. Just give us back the string to use for the text response or an array for the attachment.

var SlackGun = require('node-slack-mailgun');

SlackGun({
	slack: { // Options for slack
		hook: 'url', // Required. The hook URL for posting messages to slack.
	},
	templates: { // Options for templates
		// You can make this for any event type Mailgun sends
		all: function(variables, callback) { // callback(error, [string|array]);
			// Do your magic
			// We will not do any manipulation on the string or array you return to us.
			// Just be sure to callback an error (which will halt sending if not null) and the string or array to be used for sending a message to Slack
		} 
	}
});

List of Event Types and Variables from Mailgun

Tests

mocha

Honey-Do List

  • Utilize the real time notification API for Slack. Webhooks are soo 2010.
  • Send emails from Slack through Mailgun to the user that received the email by responding to a message in Slack. That'd be cool.
  • Expand upon security of replay attacks and other possible security issues using Mailgun's webhooks.
  • Rate limiting sending to Slack 1/sec using node-rate-limiter

Contributing

Submit a pull request and add your name to the list below