Real-time Chat Blog

How to Use Slack Incoming Webhooks from a PubNub Function

3 min readChandler Mayo on Mar 15, 2019
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

Slack Incoming Webhooks lets you build real-time, interactive alerts and notifications from other sources of data directly into Slack workspaces.

This tutorial explains how to post messages to Slack from a PubNub Function using Slack Incoming Webhooks. Using Incoming Webhooks, you can create notifications or alerts and even make them interactive. You can use Incoming Webhooks to send an alert when a user has paid, an error occurred, or if a service isn’t responding. You can also use Incoming Webhooks to ask if a user has permission for a resource with the ability to respond from Slack.

What are PubNub Functions?

Functions Logo

PubNub Functions is a serverless environment for executing functions. PubNub Functions are JavaScript event handlers that can be executed on in-transit PubNub messages or in the request/response style of a RESTful API over HTTPS.

Advantages of PubNub Functions:

  • Reduced Cost: Pay for what you use and not for idle servers.
  • Decreased Time to Market: Focus on building your product and not managing the infrastructure to support it.
  • Reduced LatencyYour users are global. Your product should be global. PubNub operates points of presence across the globe.

Getting Started

Check out the Slack Incoming Webhook Block for a faster way to get started.

Setting up a Slack Incoming Webhook Function

Go to your PubNub Admin Dashboard and select your app. Then click on the keyset that was created for that app.

Next, click “Functions” in the sidebar and then create a new module named “Slack Incoming Webhook.”

Create a Function with the name “Slack Incoming Webhook.”

Set the event type to “On Request”, and set the path to “slack-post”. You can use the other event types if you want to post to Slack before or after messages are sent on PubNub.

Delete any code in the editor and replace it with a function to post to Slack:

// FOR A PUBNUB SERVERLESS ENV. 
// Learn More: https://api.slack.com/incoming-webhooks
export default (request, response) => {
    const xhr = require('xhr');
    // Message
    let message = `Hello World!`;
    // Slack Webhook URL
    const url = "SLACK-WEBHOOK-URL-HERE";
    // Basic Slack Post
    const http_options = {
        "headers": {
            "Content-Type": "application/json"
         },
        "method": "POST",
        "body": JSON.stringify({
            "text": message,
        })
    };
    return xhr.fetch(url, http_options).then((x) => {
        //console.log(body);
        return response.send();
    });    
};

Replace ‘SLACK-WEBHOOK-URL-HERE’ with your Incoming Webhook URL and edit the message text to be whatever you want.

How to Test the Slack Incoming Webhooks Function

If you set the event type to “On Request”, then you can test the function by making an empty request to the function.

You can use Curl to send a request to the function. Replace “YOUR_SUB_KEY_HERE” with your Subscribe Key from your PubNub Admin Dashboard.

curl https://pubsub.pubnub.com/v1/blocks/sub-key/YOUR_SUB_KEY_HERE/slack-post

Slack Incoming Webhook

What’s Next?

Here are a few ideas:

Have suggestions or questions about the content of this post? Reach out at devrel@pubnub.com.

More from PubNub

Can Empathy Exist in the Metaverse
News May 16, 20221 min read

Can Empathy Exist in the Metaverse

A roundtable discussion led by PubNub’s COO, Casey Clegg, exploring the topics of what it means to be human in a virtual world.

PubNub Staff

PubNub Staff

How to Advance Telehealth and Virtual Care Technologies
News May 2, 20221 min read

How to Advance Telehealth and Virtual Care Technologies

Dr. Joe Kvedar, Chair of the Board for the American Telemedicine Association, joins our COO, Casey Clegg, to discuss why...

PubNub Staff

PubNub Staff

Another Step Towards Data Security: ISO-27001 Implementation
BuildMay 2, 20221 min read

Another Step Towards Data Security: ISO-27001 Implementation

Today, we are glad to announce that we are currently in the process of implementing ISO-27001 security standards.

PubNub Staff

PubNub Staff