How to create a Slack workflow with webhooks in Momento Topics
Tags: serverless, momento, eventdriven
Published: 2024-02-01 21:43:29

---
title: How to create a Slack workflow with webhooks in Momento Topics
published: true
description: Learn how to send a message directly to Slack when you publish to a topic.
tags: 
  - serverless
  - momento
  - eda
cover_image: https://assets-global.website-files.com/628fadb065a50abf13a11485/65b967906019c6e18f423c44_webhooks_slack_banner_image_v2.png
canonical_url: https://www.gomomento.com/blog/how-to-create-a-slack-workflow-with-webhooks-in-momento-topics
---

Have you ever heard of Slack? It’s this niche, up-and-coming communication platform that’s really starting to gain traction in the workplace. 😉‍

Okay, but really, Slack is everywhere. We all know it. Many organizations are even dropping email in favor of Slack—for internal *and* external comms. But the real power of Slack isn’t in channels, threads, or DMs—it’s their [Workflow Builder](https://slack.com/help/articles/360035692513-Guide-to-Slack-Workflow-Builder). This tool turns Slack into an integration machine, letting you tackle almost any task with just some minor configuration and API connections.‍

In this blog post, we'll show how simple it is to use [webhooks in Momento Topics](https://docs.momentohq.com/topics/webhooks/overview) to create a seamless Slack workflow that sends a message whenever an event is published to a topic. With that in place, you can easily extend the workflow in any number of ways—from task automation to alerts to chat monitoring. Let’s dive in.‍

## How to build a Slack workflow

First, select the Slack workspace in which you want the workflow to live. Then, navigate to the *Workflow Builder*.

![Screenshot of where the Workflow Builder is in Slack](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b95569518a48e08ef7665c_slack-workflow-builder-screen.png)

Click on *Create Workflow*.

![Screenshot of the create workflow button in the workflow builder](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b95580cad04bf60f13dc1d_save-to-keep.png)

Select *From a webhook*.

![Screenshot of the workflow builder with the start from scratch menu](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b955cb62198fe24654d36f_start-from-scratch.png)

Click on the *Set Up Variables* button and add variables for `token_id` and `text`.

![Dynamic variables configured in the workflow](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b956018dcee483d35ef704_key%20and%20data%20type.png)

At the bottom of this window, copy the URL from the *Web request URL* field and save it for later.

![Screenshot of where to copy the link of your webhook](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b9564b265569f636521df1_web%20request%20url.png)

Type “send” in the search bar and click on *Send a message to a channel*.

![Screenshot of the action to send a message to a channel](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b9565eae04f06420121ba6_momento%20topic%20slack%20message.png)

Click the pencil icon to configure the message that gets sent to Slack. We’ll use the variables we set up earlier to show who sent the message and what they said. When you’re done, hit the *Save* button.

![Screenshot of the message to post in slack](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b9568b64bf2964133d8769_send%20a%20message%20to%20a%20channel.png)

On the main workflow screen, click the *Publish* button and give your workflow a name and meaningful description. Hit *Next* to publish your workflow.

![Screenshot of the publish dialog for a workflow](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b956b6919c52c074d0c385_publish%20page.png)

## Create a webhook in Momento Console

Navigate to [Momento Console](https://console.gomomento.com/caches) and create a cache if you don’t already have one.

![Screenshot of the cache configuration in Momento](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b956f1ae04f0642012697d_pick%20cloud%20provider.png)

Navigate to your new cache and click on the *Webhooks* menu option on the left.

![Screenshot showing where the webhooks menu item is in Momento](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b9571eb8f74599bbc6bad3_cache%20name%20and%20region.png)

Click the *Create Webhook* button and fill out the form by providing a name for your webhook, which topic it should trigger on, and the value you copied from the Slack workflow. Then hit *Create*.

![Screenshot showing the webhook configuration fields](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b95742122857601ce15e23_create%20webhook.png)

Now that we have our webhook created and our workflow published, let’s test it out! Go to the [Topics page](https://console.gomomento.com/topics) in Momento Console and select the cache and topic name we just configured, then hit *Subscribe*.

![Screenshot showing the topics dashboard](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b9576d8dcee483d35feef6_welcome%20to%20momento%20topics.png)

In the “Publish messages” section, type in your message and hit *Publish*.

![Screenshot showing a test message being sent in Momento](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b957951160efc9ca000212_publish%20your%20message.png)

Zip on over to Slack and check out your new message!

![Screenshot of a slack message with part of the data missing](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b957c281816f9dc0614309_webhook%20demo%20successful.png)

## Add username to the messages

Now you might notice there is no username displaying to the left of the colon. That’s because we published a message without using an auth token with an embedded identifier. In a real world scenario, all our messages would uniquely identify a user. So let’s try that out.‍

I wrote this small script using the [Momento JavaScript SDK](https://docs.momentohq.com/sdks/nodejs/topics.html) to create a short-lived token scoped to publish only to our `slack-bot` topic. This token has my username embedded in it so it should show up in our Slack channel when we publish a message.

```javascript

const { AuthClient, Configurations, CredentialProvider, TopicClient, ExpiresIn } = require('@gomomento/sdk');
const dotenv = require('dotenv');
dotenv.config();

const USERNAME = 'allen';
const MESSAGE = 'It worked - yeehaw!';

const publishMessage = async () => {
  const authClient = new AuthClient({
    credentialProvider: CredentialProvider.fromEnvironmentVariable({ environmentVariableName: 'MOMENTO_API_KEY' })
  });

  const tokenScope = {
    permissions: [
      {
        role: 'publishonly',
        cache: 'slack',
        topic: 'slack-bot'
      }
    ]
  };

  const token = await authClient.generateDisposableToken(tokenScope, ExpiresIn.minutes(1), { tokenId: USERNAME });

  const topicClient = new TopicClient({
    configuration: Configurations.Laptop.v1(),
    credentialProvider: CredentialProvider.fromString({ authToken: token.authToken })
  });

  topicClient.publish('slack', 'slack-bot', MESSAGE);
};

publishMessage();
```
After I run this script, I get a message in Slack with my username and message!

![Screenshot of a slack message with username included](https://assets-global.website-files.com/628fadb065a50abf13a11485/65b9582d75bc1bc7a93cfacb_webhook%20demo%20success%202.png)

Super cool and super easy! After you do it once, this integration only takes a few minutes to set up. Plus, Slack workflows are incredibly easy to extend, so you could even set it up to send a message back over a topic! More on that in another post. 🙂‍

You can use this integration for alerts, status updates, monitoring chat sessions, and much, much more. We’re super excited about webhook functionality in [Momento Topics](https://www.gomomento.com/services/topics) and further enabling you to build the best developer experience for your users and development teams!‍

**So what are you waiting for? Go out there and build!**‍

Happy coding!