🤖

A Slack bot that replies to mentions

You can build a Slack bot using vals.

Vals can receive events from Slack’s Events API via the Express API. In this guide, you’ll build a bot that replies whenever it’s mentioned.

image

1. Create a Slack app

Visit https://api.slack.com/apps?new_app=1, create a new app From Scratch, and choose your App Name and your workspace.

image

2. Save your app’s verification token

When your handler val receives requests, you can verify that they were sent by Slack by looking for the verification token.

To find your app’s verification token, go to SettingsBasic Information in the side bar. Scroll down to App Credentials.

image

Save the Verification Token as a Val Town secret as slackVerificationToken.

3. Create a val that will respond to the challenge and reply when it’s mentioned

When you add a Request URL, Slack will immediately send a challenge to verify the endpoint is correct.

Fork this val that responds to the challenge, and replies to app_mention events.

4. Set up event subscriptions

On your forked val, copy the Web endpoint via the menu at Endpoints > Copy web endpoint.

Back on Slack, navigate to the Event Subscription page. It’s under Features in the side bar.

Toggle on Enable Events, and paste your endpoint in the Request URL field.

image

A few seconds later, you should see a verified message above the input box.

4. Subscribe to the app_mention event

On the Event Subscriptions page, scroll down to the Subscribe to bot events section, and subscribe to the app_mention event. Make sure to Save Changes.

image

5. Add the necessary scopes to your app

In FeaturesOAuth & Permissions, scroll down to Scopes and enable the scopes you need.

So the bot can reply when it’s mentioned, add app_mentions:read and chat:write.

image

6. Install your app to your workspace

In SettingsInstall App, install the app to your workspace.

image

7. Get your app’s OAuth token

In FeaturesOAuth & Permissions, copy the Bot User OAuth Token, and save it as a Val Town secret as slackToken.

image

7. Add your bot to the relevant Slack channel

The bot will receive an event when it’s mentioned in a channel that it’s been invited to.

Invite the bot to your channel.

image

Mention the bot, and the val you forked earlier (@vtdocs.slackReplyToMessage) will reply to the message!

image

Vals not behaving like you’d expect? Get help on the Val Town Discord.