How to: Use Slack API to Get Notification When Something Happens in your App/Site

Most Startups use Slack these days to communicate. They also integrate Jira, Asana and other tools into Slack to improve the communication and integration between the team members. But what if you want to get a notification when a user registers to your site or app, when a user makes a payment or a complaint. Wouldn’t it be cool to integrate your business events with Slack? I found it very useful, and here is how I did it:

Step 1Go to the Slack API page and choose your integration method – There are a lot of supported use cases and methods of integration. I chose the Incoming Webhooks as I wanted to post on Slack every time a user registered to my service.

Step 2Setup your integration – to set up an Incoming Webhook integration, click on the Set up an incoming webhook integration and pick the channel you want to publish to:

Screen Shot 2015-09-16 at 1.12.44 PM

Note that at the time of this article, if you choose to create a new channel you will need to refresh the page to actually see the channel you have just created. Once you have chosen a channel click on the “Add Incoming WebHooks Integration”. You will get to a page that will give you the URL and keys needed to post to that channel as well as other useful customization.

Step 3 – Code!

Here is how my integration looked on my python server:

# remember to import httplib and urllib
# send slack notification
username = self.request.get('email')
data = urllib.urlencode({'payload': '{"text": "Got new user: ('+username+')"}'})
h = httplib.HTTPSConnection('hooks.slack.com')
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}

h.request('POST', '/services/[your]/[keys]/[here]', data, headers)
r = h.getresponse()

ack = r.read()

#do something interesting with the ack

Step 3 – Test

I ran the code  and got a “ok” ack value, and was pleasantly surprised to get a new notification in my slack app:

Screenshot_2015-09-16-08-48-38

 

Now me and my team get a notification on every user that joins our service! cha-ching!

Share the love...Tweet about this on TwitterShare on LinkedInShare on Google+Share on Facebook

Amir Shevat

Amir Shevat is the global Startup Outreach lead in Google Developer Relations (g.co/launch). Previously, Amir Led Google Campus Tel Aviv and was the co founder of several startups.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *