Send Messages

Learn to use Slack API calls to send messages.

We can send different kinds of messages through the Slack application, including regular messages, “me” messagesA “me” message is a type of message used to indicate action., and scheduled messages.

Let’s look at the following endpoints in this lesson:

  1. chat.meMessage: This endpoint sends an italicized action text message to a channel.
  2. chat.postMessage: This endpoint sends a message to a channel.
  3. chat.scheduleMessage: This endpoint schedules a message to be sent to a channel in the future.

Send a “me” message

We’ll start with the most straightforward endpoint—the https://slack.com/api/chat.meMessage endpoint. This endpoint allows us to send an italicized “me” message to a specific channel. This endpoint is relatively straightforward and has no optional arguments.

Request parameters

Some important query parameters for the chat.meMessage endpoint are as follows:

Parameter

Type

Category

Description

token

token

Required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these as an HTTP Authorization header or as a POST parameter.

channel

string

Required

This parameter specifies the channel to which the message will be posted. It can be a public channel, a private group, or an IM channel (direct message). The value for this parameter can either be an encoded ID or a name of a channel.

text

string

Required

The text of the message to be sent.

Let’s call the chat.meMessage endpoint. Click the “Run” button to post a “Hello World!” message using this endpoint.

Press + to interact
import fetch from "node-fetch";
const url = "https://slack.com/api/chat.meMessage";
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const bodyParameters = JSON.stringify({
channel: "{{CHANNEL_ID}}",
text: "Hello World!",
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
async function sendMeMessage() {
try {
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
sendMeMessage();

Let’s look at the highlighted lines in the code widget shown above:

  • Line 3: We specify the URL for the chat.meMessage endpoint.
  • Lines 5–8: We specify the request headers.
  • Lines 10–13: We specify the channel and text parameters that will be passed to the request. We use channel to specify the channel ID.
  • Lines 15–19: We define the options for the API call, specifying the type as POST and providing the request headers and body.
  • Lines 21–28: We define a function called sendMeMessage to make the API call and print the response.
    • Lines 23–24: We make a request to the API and print the JSON response.
    • Line 26: We catch any errors that may have occurred and print them to the console.
  • Line 30: We invoke the sendMeMessage function.

Once we’ve run the code above, we should see a “Hello World!” message in the channel where we added the application. The text will be in italics, indicating a “me” message.

Response fields

Apart from the message sent to the channel, the API also sends a JSON response to our POST request. The response object to a successful request contains the following fields:

Property

Type

Description

ok

boolean

A boolean value indicating whether the request was successful or not.

channel

string

The channel where the "me" message was sent.

ts

string

The Unix timestamp of the shared "me" message.

In case of an unsuccessful request, the response contains an error field indicating what went wrong.

Post a message

To post a message, we access the https://slack.com/api/chat.postMessage endpoint. Unlike chat.meMessage, it can send attachments, blocks, and text. We can use this endpoint to post messages to a public, private, or IMA direct message. channel.

We can use the endpoint to send the following types of messages on Slack:

  1. Attachments (legacy): This is a JSON-based array of structured attachments like "[{"pretext": "pre-hello", "text": "text-world"}]".
  2. Blocks: This is a JSON-based array of structured blocksA block is a way to send a more structured and interactive message., like "[{"type": "section", "text": {"type": "plain_text", "text": "Hello world"}}]".
  3. Text: This is a simple text like "Hey, from educative!".

Explore the Slack-provided block builder with this link.

Request parameters

Some important query parameters for the chat.postMessage endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. Tokens are usually passed as an HTTP Authorization header or can also be sent as a POST parameter.

channel

string

required

This parameter specifies the channel to which the message will be posted. It can be a public channel, a private group, or an IM channel (direct message). The value for this parameter can either be an encoded ID or the name of a channel.

blocks/attachments/text

string

required

Any one of these arguments is required as the content of the message. If attachments or blocks are included, text will be used to show in notifications only.

mrkdwn

boolean

optional

This parameter defaults to true and we can set it as false to disable Slack's markup parsing.

thread_ts

string

optional

This provides another message's timestamp (ts is the timestamp of a delivered message) value to make this message a reply. Avoid using a reply's timestamp (ts) value. Use its parent instead.

In the two code widgets below, we’ll send a block message and then use the extracted timestamp from the response to send a reply in the second code widget.

Let’s call the chat.postMessage endpoint. Click the “Run” button to post a message containing a block to the channel.

Press + to interact
import fetch from "node-fetch";
const url = "https://slack.com/api/chat.postMessage";
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const bodyParameters = JSON.stringify({
channel: "{{CHANNEL_ID}}",
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: "You have a new request:\n*<exampleEmployeeLink.com | Andy Brown - New device request>*",
},
},
{
type: "section",
fields: [
{
type: "mrkdwn",
text: "*Type:*\n ...",
},
{
type: "mrkdwn",
text: "*When:*\n ...",
},
{
type: "mrkdwn",
text: "*Last Update:*\n ...",
},
{
type: "mrkdwn",
text: "*Reason:*\n ... ",
},
{
type: "mrkdwn",
text: "*Specification:*\n ...",
},
],
},
],
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
async function sendMessage() {
try {
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
sendMessage();

Click the “Run” button to call the same endpoint with different optional arguments. This time, we use the extracted timestamp, ts, from the message we sent in the widget above.

Press + to interact
import fetch from "node-fetch";
const url = "https://slack.com/api/chat.postMessage";
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const bodyParameters = JSON.stringify({
thread_ts: "{{TS}}",
reply_broadcast: true,
channel: "{{CHANNEL_ID}}",
text: "Replying to the previous message!!",
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
async function sendMessageReply() {
try {
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
sendMessageReply();

Response fields

The chat.postMessage endpoint works similarly to the chat.meMessage in the first code widget but can have varying responses. These responses depend on the optional parameters sent with the request. A typical successful response is identical to the response from the chat.meMessage endpoint, with the addition of a message property that contains the message content.

Note: Visit this lesson to view the details of the message object.

In the second code widget, we use the extracted timestamp, ts, and then the optional arguments to reply to a message using the chat.postMessage endpoint.

Schedule a message

To schedule a message, we access the https://slack.com/api/chat.scheduleMessage endpoint. This method schedules a message for sending to public, private, and IM channels at the specified time in the future.

Request parameters

Some important query parameters for the chat.scheduleMessage endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

 Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass the tokens as an HTTP Authorization header or as a POST parameter.

channel

string

required

This parameter specifies the channel to which the message will be posted. It can be a public channel, a private group, or an IM channel (direct message). The value for this parameter can either be an encoded ID or a name of a channel.

text

string

required

This is the text of the message to be sent.

post_at

string

required

This parameter specifies the future Unix EPOCH timestamp to send the message at that time.

Let’s call the chat.scheduleMessage endpoint. Click the “Run” button to schedule a message for one minute later.

Press + to interact
import fetch from "node-fetch";
const url = "https://slack.com/api/chat.scheduleMessage";
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
let epochTime = Math.floor(Date.now() / 1000);
const bodyParameters = JSON.stringify({
channel: "{{CHANNEL_ID}}",
text: "Hello World! - sent to the channel after 1 minute",
post_at: epochTime + 60,
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
async function scheduleMessage() {
try {
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
scheduleMessage();

This endpoint call is similar to the calls we’ve made before, but in this case, we are also specifying the Unix epoch timestamp using the built-in Date class.

  • Line 10: We use the Date.now() method to get the current Unix timestamp as an integer.
  • Line 15: We add one minute (60 seconds) to the current Unix timestamp.

Response fields

A successful response from the chat.scheduleMessage endpoint contains the following properties:

Property

Type

Description

ok

boolean

A boolean value indicating whether the request was successful or not.

channel

string

The channel where the message was sent.

scheduled_message_id

string

The unique ID of the scheduled message.

post_at

string

The Unix timestamp of the scheduled time for the message to be sent.

message

string

The details of the scheduled message.

Note: Visit this lesson to view the details of the message object.