Fetch Slack messages to your Accode workspace

In this guide we will implement a workflow that periodically fetches new messages from a Slack channel into your Accode workspace. This will teach you how to:

  1. Create a data model that contains your Slack app information for authentication.
  2. Create a data model that contains your Slack messages.
  3. Define a simple workflow that fetches new Slack messages periodically.

This guide assumes that you already have an Accode workspace in use and have installed the @accode/auxiliary component package.

Step 0. Create a Slack app

If you already have a Slack app, you can skip this section. However, you need to grab the Slack app token and the id for a channel that you want to fetch messages from. The Slack app needs to have the channels:read and channels:history scopes.

First, we need to figure out how we are going to authenticate ourselves so that we can access the our Slack messages. This step has nothing to do with Accode, but rather is a guide on to authenticate any Slack app. You can also reference the guide at the Slack API documentation. More specifically, you can follow the guide at Slack: Retrieving messages, for the specific endpoints that we are going to be using.

To authenticate with Slack, you need to create a Slack app, which will then provide a token that can be used for authentication. In short, you need to:

  1. Create a Slack app: Go to the Slack app dashboard and click on "Create New App." Give your app a name and select the workspace where you want to install it.
  2. Enable permissions: To be able to retrieve messages, you need to give the Slack app the channels:read and channels:history scopes. Check here Slack: Retrieving messages for more details.
  3. Install your app in the workspace: In the app dashboard, go to the "OAuth & Permissions" section. Scroll down to the "Scopes" section and click on the "Install to Workspace" button. This will generate an installation URL. Open the URL in your browser and authorize the app to access your Slack workspace. Access the token from this page.

Finally, open the Slack web app and navigate to the channel that you want to use. The channel id will be the last element of the URL after the rightmost slash (it usually starts with a C).

Step 1. Create a model for your Slack app in Accode

Now that we have the requisite information for accessing our Slack messages, we can return to our Accode workspace to begin building our workflow. First off, we need to define a data model that contains the relevant authentication information for our Slack app (created in the previous step).

A data model defines the structure, or schema, of data in Accode. Each model consists of an identifier, a set of fields, related metadata, and a mechanism for storing the data.

To create a new model, we navigate to the top-right corner to the “+ Create” button. We can use the quick option for creating a new model in a pop-out modal without having to navigate away from the current page.

We define the Slack app model as follows:

We populate this model with a row that contains our token and channelId.

Press the “Save” button to save the Slack app data.

Step 2. Create a model for your Slack messages

The next step is to create a model that will contain the Slack messages we fetch. We define it as follows:

We model our data based on the API example response here: https://api.slack.com/methods/conversations.history

Step 3. Create a workflow for fetching Slack messages

To create a workflow, navigate to the now-familiar top-right “+ Create” menu again:

We want to create a new workflow that is triggered periodically.

Next, we want to select the correct data source, which is our newly created Slack app model.

Finally, we can configure our workflow to run at certain intervals, and to filter the data that enters it. We can also select a predefined workflow template, or even use one of our existing workflows as base. We elect not to select anything here, as we do not (yet) want our workflow to run periodically. We will be able to run the workflow manually.

The actual business logic for fetching the Slack messages is very simple, and can be achieved with three actions:

  • the Fetch action from @accode/auxiliary for calling the Slack API
  • the Script action from @accode/actions for transforming the response to work with our data model
  • the Create from output action from @accode/auxiliary for saving the data into our data model

Using the Fetch action, we POST to the API endpoint https://slack.com/api/conversations.history . We provide the Authorization header with the bearer token using the built-in templating language {{{ entity.token }}} which allows us to select data from the current entity (in this case one instance of the SlackApp model). Both the Headers and Body section accept a JSON-formatted object.

Using the Script action, we can transform the previous action output (previous.payload ), in order to access the nested messages that are forwarded by the Fetch action. Running the next(outcome, nextPayload) function allows us to pick the .messages property from the provided payload.

The code can either be written as:

// We have to use optional chaining as the previous.payload is defined as always-optional
next(true, previous.payload?.messages)

// --- OR ---

// You can just return the next action output with this shorthand
return previous.payload?.messages

// These statements achieve identical results

Using the Create from output action, we then store the messages passed along by the Script action.

You can now test this workflow by navigating to the “Config” section of the sidebar and pressing the “Manual start” button. In this config view, you can also set a cron value to run the workflow periodically.

The final workflows looks like this:

Conclusion

In this guide, we learnt how to define data models and how to populate them both manually and via workflows.

Future exercises:

  • Create a “Subscribe to changes” workflow that listens to new messages being fetched, filters out relevant messages based on specific words in the message, and POSTs a response to those messages.
  • Add a unique by criteria to the SlackMessage model to prevent prevent duplicates (check the various options for how to deal with duplicates in Create from output)
Previous
Data timeline