2021 Call for Code Awards: Live from New York, with SNL’s Colin Jost! Learn more

How to read Slack messages with serverless functions

In a development work environment where people share their ideas and emotions constantly, good communication among team members plays a key role. Often times, you talk to each other face to face, but sometimes you may not be able to communicate with each other in person because of distance. Therefore you may use messaging applications, such as Slack, to keep in touch.

For many reasons, your development team may want to investigate messages from Slack, alter them in a proper way, or take some actions according to some specific input. You may prefer this way because of a heavy workload that prevents you from answering every single question and automation is needed. Or you may want to analyze the context of a long text that is written in order to answer it in a rapid way. Or you just want to derive some insights from the text data to practice what you learnt at school.

This tutorial shows how you can easily receive messages from Slack using serverless functions to solve the problems mentioned above.


Estimated time

Completing this tutorial should take 15 minutes.


Step 1. Create a Slack application

  • Open your browser.
  • Sign in to your Slack workspace by entering your workspace URL name and clicking Enter.
  • Go to api.slack.com and click Your Apps from the header.
  • On the Your Apps page, click Create an App.
  • In the Create a Slack App window, enter a name for your application in the App Name field.
  • Select your workspace name from the Development Slack Workspace list.
  • Click Create App.
  • From the Features menu bar, select Bot Users.
  • Click Add a Bot User.
  • Enter a name for your bot in the Display name field.

    screen capture of the bot user set up fields

  • Enter a short name for your bot in the Default username field.

  • Set Always Show My Bot as Online to On.
  • Click Add Bot User.
  • From the Features menu bar, select OAuth & Permissions.
  • Click Install App to Workspace and click Allow if necessary. This will install your app to your Slack workspace by generating credentials and binding it to the workspace.

Step 2. Create an action in IBM Cloud Functions

  • Open a new tab in your browser and navigate to IBM Cloud Functions.
  • Click Log in to IBM Cloud and sign into your account. You should be redirected back to the IBM Cloud Functions home page. If not, go there by entering cloud.ibm.com/functions into your browser.
  • Click Start Creating.

    screen capture of IBM Cloud Functions home page

  • Select Create Action.

  • Within the Create Action form, fill the Action Name field with a name of your choice.
  • Leave the rest of the fields with the default options and click Create.

    screen capture of Create Action page

  • The action overview page will open with the code part of your action shown in the editor panel. Inside the function definition, replace the default code (return { message: 'Hello World' };) with the code snippet below. This will ensure that your action is connected to your recently created Slack app. (Slack sends data with a challenge parameter and your app sends it back to Slack by returning the challenge parameter.)

     if (params.type !== undefined && params.type ===      'url_verification') {
        return({ challenge: params.challenge });
  • After the if clause, enter the additional code snippet below, which checks if a direct message is received from Slack to the bot you created and returns the text as an output.

     let payload = params;
     let text = "Empty Text!";
     if (payload.event && payload.event.type === "message" && payload.event.subtype != "bot_message" && typeof payload.event.files === "undefined") {
       text = payload.event.text;
       return {message:text};
  • Click Save.

  • From the menu bar, select Endpoints.
  • Select the Enable as Web Action checkbox.
  • Click Save.
  • A URL will be generated for you. Copy it into your favorite editor.

    screen capture of IBM Cloud Functions home page

Step 4. Subscribe your action to Slack

  • In Slack, go back to the Your Apps page.
  • Choose the app you created in Step 1.
  • From the Features menu bar, select Event Subscriptions.
  • Set Enable Events to On.
  • Enter the URL that you copied within Step 2 into the Request URL field, but also add a .json extension at the end of the URL. Ensure that the Verified notification appears.
  • Expand Subscribe to bot events and click Add Bot User Event.
  • Select message.im from the list. This will subscribe the bot to listen to direct messages coming to it.
  • Expand Subscribe to workspace events and click Add Workspace Event.
  • Select message.im from the list.
  • Click Save Changes.
  • From the notification message that appears, click reinstall your app.
  • Click Allow.

    screen capture of Add Bot User Event in Slack

Step 5. View received text

  • Go to your terminal and log in to your IBM Cloud CLI by entering the following command:

     ibmcloud login
  • Choose your organization and space by entering the following command:

     ibmcloud target --cf
  • Go to your Slack application and send a direct message to your bot.

    screen capture of Slack message to bot

  • Go back to your terminal and enter the following command:

     ibmcloud fn activation list
  • Copy the first activation ID that appears in the list.

  • Enter the following command with the activation ID included:

     ibmcloud fn activation get <your copied activation ID>

    screen capture of activation list in terminal

  • You should see the text that you messaged to your Slack bot in the results section of the response.

    screen capture of results in terminal

Note: Be sure that you are in the correct Cloud Foundry organization and space from your terminal. You can verify your current selections on the Namespace settings page within IBM Cloud Functions.


This tutorial demonstrated how to read Slack messages with serverless functions, a first step towards creating automated message analysis and response. The role of text analysis and response in the business world is crucial to satisfy customers, gain insights, and solve issues. By automating communications, your development team can focus more on creating solutions for customers and less time performing administrative tasks. As a next step, learn how to create serverless functions to send push notifications.