DEV Community

Cover image for How to create a Slack workflow with webhooks in Momento Topics
Allen Helton for Momento

Posted on • Originally published at gomomento.com

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. 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 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

Click on Create Workflow.

Screenshot of the create workflow button in the workflow builder

Select From a webhook.

Screenshot of the workflow builder with the start from scratch menu

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

Dynamic variables configured in the workflow

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

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

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

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

Create a webhook in Momento Console

Navigate to Momento Console and create a cache if you don’t already have one.

Screenshot of the cache configuration in Momento

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

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

Now that we have our webhook created and our workflow published, let’s test it out! Go to the Topics page in Momento Console and select the cache and topic name we just configured, then hit Subscribe.

Screenshot showing the topics dashboard

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

Screenshot showing a test message being sent in Momento

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

Screenshot of a slack message with part of the data missing

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 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.


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();
Enter fullscreen mode Exit fullscreen mode

After I run this script, I get a message in Slack with my username and message!

Screenshot of a slack message with username included

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 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!

Top comments (0)