chat.postMessage
method
We're still building and not all features are available quite yet. Enjoy this peek into the future!
Not ready for the future? Return to the past at api.slack.com.
Usage info
This method posts a message to a public channel, private channel, or direct message (DM, or IM) conversation.
The text
, blocks
and attachments
fields
The usage of the text
field changes depending on whether you're using blocks
. If you're using blocks
, this is used as a fallback string to display in notifications. If you aren't, this is the main body text of the message. It can be formatted as plain text, or with mrkdwn
.
The text
field is not enforced as required when using blocks
or attachments
. However, we highly recommended that you include text
to provide a fallback when using blocks
, as described above.
Accessibility considerations
It is expected behavior that screen readers will default to the top-level text
field of your post, and will not read the content of any interior blocks
in the underlying structure of the message. Therefore, to make an accessible app, you must either:
- include all necessary content for screen reader users in the top-level
text
field of your message, or - do not include a top-level
text
field if the message hasblocks
, and allow Slack attempt to build it for you by appending content from supportedblocks
to be read by the screen reader.
JSON POST support
When POSTing with application/x-www-form-urlencoded
data, the optional attachments
argument should contain a JSON-encoded array of attachments.
As of October 2017, it's possible to send a well-formatted application/json
POST body to chat.postMessage
and other Web API write methods. No need to carefully URL-encode your JSON attachments
and present all other fields as URL encoded key:value pairs; just send JSON instead.
Response
Formatting messages
Messages are formatted as described in the formatting spec. The formatting behavior will change depending on the value of parse
.
By default, URLs will be hyperlinked. Set parse
to none
to remove the hyperlinks.
The behavior of parse
is different for text formatted with mrkdwn
. By default, or when parse
is set to none
, mrkdwn
formatting is implemented. To ignore mrkdwn
formatting, set parse
to full
.
Unfurling content
By default, we unfurl all links in any messages posted by users and Slack apps. We also unfurl links to media-based content within Block kit blocks.
If you want to suppress link unfurls in messages containing Block Kit blocks, set unfurl_links
and unfurl_media
to false.
For more detailed information about link unfurling, refer to unfurling links in messages.
Truncating content
For best results, limit the number of characters in the text
field to 4,000 characters. Ideally, messages should be short and human-readable. Slack will truncate messages containing more than 40,000 characters. If you need to post longer messages, please consider uploading a snippet instead.
If using blocks
, the limit and truncation of characters will be determined by the specific type of block.
Threads and replies
Provide a thread_ts
value for the posted message to act as a reply to a parent message. Sparingly, set reply_broadcast
to true
if your reply is important enough for everyone in the channel to receive.
Channels
You must specify a public channel, private channel, or an IM channel with the channel
argument. Each one behaves slightly differently based on the authenticated user's permissions and additional arguments, as discussed in the sections below.
Post to a public channel
Pass the channel name or the channel's ID (C123456
) to the channel
parameter and the message will be posted to that channel. The channel's ID can be retrieved through the conversations.list API method.
Post to a private channel
As long as the authenticated user is a member of the private channel, pass the channel's ID (C123456
) to the channel
parameter and the message will be posted to that channel. The private channel's ID can be retrieved through the conversations.list API method.
Post to a multi-person direct message channel
As long as the authenticated user is a member of the multi-person direct message (a "private group" or MPIM), you can pass the group's ID (G123456
) and the message will be posted to that group. The private group's ID can be retrieved through the conversations.list API method.
Post to a direct message channel
Posting to direct messages (also known as DMs or IMs) can be a little more complex, depending on what you actually want to accomplish.
If you want your app's bot user to start a 1:1 conversation with another user in a workspace, provide the user's ID as the channel
value and a direct message conversation will be opened if it isn't open already. Resultant messages and associated direct message objects will have a direct message ID you can use from that point forward, if you'd rather.
Bot users cannot post to a direct message conversation between two users using chat.postMessage
. If your app was involved in the conversation, then it would be a multi-person direct message instead. Apps can post to direct message conversations between users when a shortcut or slash command belonging to that app is used in the conversation.
You will receive a channel_not_found
error if your app doesn't have permission to enter into a DM with the intended user.
channel
value is deprecated, along with the whole concept of usernames on Slack.Please always use channel-like IDs instead to make sure your message gets to where it's going.
Getting a user's ID
A list of user IDs can be retrieved via the users.list
API method.
Begin a conversation in a user's App Home
Start a conversation with users in your App Home.
With the chat:write
scope enabled, call chat.postMessage
and pass a user's ID (U123456
) as the value of channel
to post to that user's App Home channel. You can use their direct message channel ID (as found with coversations.open
, for instance) instead.
Rate limiting
chat.postMessage
has special rate limiting conditions. It will generally allow an app to post 1 message per second to a specific channel. There are limits governing your app's relationship with the entire workspace above that, limiting posting to several hundred messages per minute. Generous burst behavior is also granted.
Channel membership
New Slack apps do not begin life with the ability to post in all public channels.
For your new Slack app to gain the ability to post in all public channels, request the chat:write.public
scope.
Sending messages as other entities
Apps can publish messages that appear to have been created by a user in the conversation. The message will be attributed to the user and show their profile photo beside it.
This is a powerful ability and must only be used when the user themselves gives permission to do so. For this reason, this ability is only available when an app has requested and been granted an additional scope — chat:write.customize
.
Your app should only use this feature in response to an inciting user action. It should never be unexpected or surprising to a user that a message was posted on their behalf, and it should be heavily signposted in advance.
To modify the appearance of the app, make calls to chat.postMessage
while providing any of the following parameters:
username
to specify the username for the published message.icon_url
to specify a URL to an image to use as the profile photo alongside the message.icon_emoji
to specify an emoji (using colon shortcodes, eg.:white_check_mark:
) to use as the profile photo alongside the message.
If the channel
parameter is set to a User ID (beginning with U
), the message will appear in that user's direct message channel with Slackbot. To post a message to that user's direct message channel with the app, use the DM ID (beginning with D
) instead.
Legacy concerns
This feature works differently for classic apps.
Legacy authorship
Classic apps using the umbrella bot
scope can't request additional scopes to adjust message authorship.
Legacy as_user
parameter
For classic apps, the best way to control the authorship of a message was to be explicit with the legacy as_user
parameter. If you didn't use the as_user
parameter, chat.postMessage
would guess the most appropriate as_user
interpretation based on the kind of token you were using. If as_user
was not provided at all, the value was inferred based on the scopes granted to the caller: If the caller could post with as_user
passed as false
, then that was how the method behaved; otherwise, the method behaved as if as_user
were passed as true
.
When the as_user
parameter was set to false
, messages were posted as "bot_messages
", with message authorship attributed to the user name and icons associated with the classic app.
Effect on identity
Token types provide varying default identity values for username
, icon_url
, and icon_emoji
.
- Test tokens inherits the icon and username of the token owner.
- Slack App user token with
chat:write:user
inherits the icon and username of the token owner. - Slack App bot user token inherits Slack App's icon and app's bot username.
Legacy identity rules in DMs
If using icon_url
, icon_emoji
, or username
with chat.postMessage
and a direct message, some special rules apply to ensure the receiver is crystal clear about who is sending the message:
- If the legacy
as_user
argument was false:- Pass the DM channel's ID (
D123456
) as the value ofchannel
to post to that DM channel as the app, bot, or user associated with the token. You can change the icon and username that go with the message using theicon_url
andusername
parameters. The IM channel's ID can be retrieved through the conversations.list API method.
- Pass the DM channel's ID (
- If the legacy
as_user
parameter was true:- Pass the DM channel's ID (
D123456
) or a user's ID (U123456
) as the value ofchannel
to post to that DM channel as the app, bot, or user associated with the token. The IM channel's ID can be retrieved through the conversations.list API method. Whenas_user
is true, the caller may not manipulate the icon and username on the message.
- Pass the DM channel's ID (