In the following guide, you'll create a basic Slack app that can send messages using webhooks.
Create a Slack app via the apps page by selecting the following button:
If you need to update the name of your app later, you can do so from your app's Home tab. Changing the app name entry under Basic Information will update the bot name rather than the app name β this is the name that appears when the app performs actions such as posting in a channel or sending a direct message.
Next, you'll need to request scopes for your app. Scopes give your app permission to perform actions, such as posting messages in your workspace.
Slack apps can't post to any public channel by default; they gain that ability by asking for permission explicitly with the use of scopes. Request the chat:write.public
scope to gain the ability to post in all public channels without joining. Otherwise, you'll need to use the conversations.join
scope, or have your app invited into a channel by a user before it can post.
chat:write
scope.channels:read
scope.In general, you'll add scopes to your bot token, not your user token. A notable exception is if you need to act as a specific user (for example, posting messages on behalf of a user, or setting a user's status).
Slack apps cannot access the Real Time Messaging (RTM) API The Events API allows your app to listen to Slack events in a structured, safe way. If you require access to RTM (for example, because you're building your app behind a corporate firewall), you'll need to create a legacy Slack app and use its bot token to call rtm.connect
. For more information, refer to Legacy: Real Time Message API.
When you follow this flow, you're playing the part of the installing user, not the app. If you were adding your app to a different workspace besides your own, this flow would be completed by a user from that workspace instead of you.
After installation, navigate back to the OAuth & Permissions page. You'll see an access token under OAuth Tokens.
Access tokens represent the permissions delegated to your app by the installing user. Keep it secret. Keep it safe. At a minimum, avoid checking them into public version control. Instead, access them via an environment variable.
Your access token allows you to call the methods described by the scopes you requested. For example, your chat:write
scope allows your app to post messages.
Your app isn't a member of any channels yet, so pick a channel to add some test messages in and /invite
your app as in the following example slash command:
/invite @Grocery Reminders
You'll see a message posted in the channel confirming that your app was added.
Slack apps listen and respond to events. We've already touched on one way an app can respond: by calling chat.postMessage
to post a message. Apps can also respond to events such as mentions in a channel, menu selections, or users sending the app a direct message. Apps listen with the Events API. Let's subscribe to the app_mention
event.
app_mention
. As with scopes, always subscribe to events with a bot user.You'll notice that the app_mention
event requires the app_mentions:read
scope. Events are like API methods: they allow your app access to information in Slack, so you'll need permissions for them. Reinstall your app to the workspace with this new scope. Now you'll be notified when your app is mentioned, and can determine how your app will respond.
POST https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Content-type: application/json
{
"text": "Gotta get the bread and milk!"
}
Navigate to the channel your app was installed in to see the message posted by your app.
Congratulations on creating your very own Slack app! Keep learning about all the things your app can do by checking out the following: