Integrate Slack

This is a how-to for creating and integrating a Slack App with Pliant.io

We will be creating an entirely new Slack App, integrating Pliant.io with it, and testing it in a development Slack workspace.

We will not be covering distributing the application to other users/workspaces.

Instructions

To create our Slack App:

  1. Login to Slack

  2. Navigate to https://api.slack.com/apps

  3. Click on "Create an App"



  4. Fill in the name for the app and select the workspace to be used as a development workspace from the drop-down, then click Create App.



  5. Upon creating the app, on the Basic Information link from the left hand side of the screen, we enable Incoming Webhooks by invoking the Add features and Functionality dropdown menu and enabling Incoming webhooks.

  6. From the basic information page, click Event Subscriptions and turn on. You will specify the Pliant workflow's RESTful endpoint in the Request URL here.
    (New approach is to deploy the workflow and choose POST method and “anonymous authentication“ while doing it. THen use the generated URL to put here)
    * The flow we point to must include input/output variable named “challenge“ (Examples with screenshots can be found down in this guide)

  7. Expand the subscribe to events on behalf of users option, and subscribe to message.channels, message.groups, and message.im.

  8. While on the Basic Information page, scroll down to App Credentials and write down the "Verification Token", we will need it later:



  9. Navigating to the App Home link, we see that slack automatically creates a bot for us. If desired, click Edit to change the name of the bot user. The default name of the bot user will be the "username" field in the authKey we create later.



  10. From the basic information page, invoke the dropdown menu for Install your app, and click Install to workspace. Select a channel to allow the app to post to and click "Install App to Workspace".



  11. Navigate to the OAuth & Permissions link on the left menu. Take note of the "Bot User OAuth Access Token". The Bot User OAuth Access Token will be the password for the slack authKey we make later.

  12. In the sane OAuth & Permissions link we can set other permissions for the bot when we scroll down to Scopes → Bot token Scopes and add whatever scope we need:

(2/4) At this point, any message we post in the channel we specified in step 10 will send an HTTP request to the endpoint specified in step 6. It will be sent in an object, called event. As such, you will need to make an input variable called event in the pliant workflow that can handle this input. Attached here is an example slack event handler workflow. To post a reply or message into a slack channel, use the following block:

  1. From the system flows panel navigate to Slack → chat, and drag and drop the "chat_postMessage" flow:



  2. Click on the "more" link below the "authKey" to expand the parameters list:




  3. Fill in the following fields as follows:

    1. authKey

      1. username = [Default Name from step 9]

      2. token = [Bot User OAuth Token from step 11]

    2. "username" - the  username given to the Bot (from Step 9)

    3. "text" - a message to send to a Slack channel

    4. "channel" - the ID channel the bot has access to post messages to (for simplicity – specify this as $event.channel – so it will reply into the same channel the event originated from)

      Example Username: pliant
      Example Token: xoxb-497157966881-1843806047443-22oIDHq8akWVOxtD1bmvdWvY



  4. In order for the integration in the opposite direction (from Slack) to be successful, we need to implement a way for the Slack test message (challenge) to pass-through.
    We do this by declaring a "challenge" variable that is both input and output, thereby passing the challenge through unmodified.



  5. Click on Save to save the flow.

(4/4) What follows is a quick example workflow build. We should be careful to determine which events we want to respond to (by checking the value of $event.text). Feel free to use this prebuilt workflow as a starting point.

  1. Back in our Pliant.io flow, add two variables:

     

    1. "slack_token" with a default value equal to the "Verification Token" we took a note of in the beginning.

    2. "should_respond" with a default value of false

  2. Add another two Input variables:
     

     

    1. "token" with a default value of ""

    2. "event" with a default value of {}

  3. Navigate to Home → Common and drag and drop an "Assign" block to be the first in our flow



  4. Fill in the parameters:



    1. select "variable" then "should_respond" as the target variable parameter

    2. Fill in the value: ($token === $slack_token) && $event && $event.text && ($event.username !== $bot_username)

  5. Drag and drop an "IF" statement block



  6. Move (drag and drop) the chat_postMessage block inside the IF section



  7. Select variable → "should_respond" in the Condition parameter of the IF block



  8. Change the "text" parameter of the chat_postMessage from "const" to "expression" with the following value: "OK, I will " + $event.text



  9. Change the "channel" parameter of the chat_postMessage from "const" to "expression" with the following value: $event.channel



  10. Save the flow.

We now have a working bi-directional integration with a Slack workspace.