Setting Up Your Meteor App to Post to Slack

Ry

It's really easy to get your Meteor app to talk to you via Slack. Once we get things setup, posting to slack will be as easy as logging to the console:

Slack.notify "*#{Meteor.user().email()}* just signed up"

And you'll see something like this in Slack...

So let's get started...

:::

Step 1: Get webhook URL from Slack

We’re going to use Slack's web hook integration feature.

  1. Visit https://XXXXXX.slack.com/services/new/incoming-webhook
  2. Choose a channel
  3. Get the URL

:::

Step 2: A little Meteor work

We're using meteor-workers in this example to queue background work, so the API call doesn’t block the main node process on the server. FYI, you should have a background worker strategy for your Meteor app, and meteor-workers is a pretty easy way to get started.

Add Meteor  packages

meteor add http
meteor add differential:workers

Add code

settings.json

{
"slack": {
"url": "https://hooks.slack.com/services/XXXXXXXX/XXXXXX/XXXXXXXXXXXXXXXXXX"
}
}

slack.coffee

class @Slack
@notify: (text) ->
if process.env.NODE_ENV is "production"
Job.push new SlackJob text: text

slackJob.coffee

class SlackJob extends Job
handleJob: ()->
HTTP.post Meteor.settings.slack.url,
data:
channel: "#useractivity"
username: "The App"
text: @params.text
icon_url: "https://usercycle.com/img/icon.png"
link_names: 1

:::

It's that easy.