Update Messages and List Scheduled Messages

Learn to use Slack API calls to update messages and list scheduled messages.

There are instances where we may have to update a message that has already been sent on the channel. For example, we might want to update previous messages regarding the weather forecast with better, updated, and more accurate predictions. We’ll use the chat.update endpoint to edit such messages. We could also run a script with chat.scheduledMessages.list to update some already scheduled messages we might have set.

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

  1. chat.update: This endpoint updates a message.
  2. chat.scheduledMessages.list: This endpoint returns a list of scheduled messages.

Update a message

To update a message, we’ll access the https://slack.com/api/chat.update endpoint. We specify a previously sent message using its timestamp to edit it. There are multiple ways to obtain a message’s timestamp. However, for now, we’ll use the timestamp that is sent back to us as a response to posting a message using the chat.postMessage endpoint.

Request parameters

Some important query parameters for the chat.update 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 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.

ts

string

required

This parameter specifies the timestamp of the message that needs to be updated.

attachments

string

optional

This is a new string with an array of JSON-structured attachments. We need this field when we're not presenting text.

blocks

string

optional

This is a new string with an array of JSON-structured attachments. We can use Slack's block builder to make this string.

text

string

optional

This is the new text that will be overwritten.

Let’s call the chat.update endpoint. Click the “Run” button to send a message to the Slack channel (where you’ve added your application) using the chat.postMessage endpoint. Then, update it using the chat.update endpoint.

Press + to interact
import fetch from "node-fetch";
const url = "https://slack.com/api/chat.update";
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
// Defining a function to update the message
async function updateMessage() {
try {
// Sending a message and retrieving its timestamp
const timestamp = await sendMessage();
const bodyParameters = JSON.stringify({
channel: "{{CHANNEL_ID}}",
text: "This is an updated message!",
ts: timestamp,
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
updateMessage();

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

  • Line 14: We make a call to the chat.postMessage endpoint and retrieve the timestamp of the message.
  • Lines 16–20: We specify the request body, including the ts parameter.

Response fields

A successful response contains the following fields:

Parameter

Type

Description

ok

boolean

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

channel

string

The channel where the original message was sent.

ts

string

The Unix timestamp of the updated message.

text

string

The text of the updated message.

message

object

The details of the message.

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

Get a list of scheduled messages

To get a list of scheduled messages, we access the https://slack.com/api/chat.scheduledMessages.list endpoint. It will return a JSON of all the scheduled messages sent by the application.

Request parameters

Some important query parameters for the chat.scheduleMessages.list 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 tokens as an HTTP Authorization header or as a POST parameter.

channel

string

optional

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.

Let’s call the chat.scheduledMessages.list endpoint. Click the “Run” button to make a scheduled message using the chat.scheduledMessage endpoint. Then, retrieve a list of scheduled messages using the chat.scheduledMessages.list endpoint.

Press + to interact
import fetch from "node-fetch";
const url = "https://slack.com/api/chat.scheduledMessages.list";
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const bodyParameters = JSON.stringify({
channel: "{{CHANNEL_ID}}",
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
async function getScheduledMessages() {
try {
// Scheduling a message for 6 minutes later
let message = "Hello World! In 6 minutes";
let minutes = 6;
await scheduleMessage(message, minutes);
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
getScheduledMessages();

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

  • Line 25: We schedule a message for six minutes later.
  • Line 27: We make a call to the chat.scheduledMessages.list endpoint.

Response fields

A successful response includes a list of all currently scheduled_messages, where each element of the list contains the following properties:

Parameter

Type

Description

id

string

The unique ID of the scheduled message.

channel_id

string

The channel where the scheduled message will be sent.

post_at

string

The Unix timestamp of the scheduled time of the message.

date_created

string

The Unix timestamp of when the scheduled time was created.

text

string

The textual content of the message.

If the list of scheduled messages is too large to include in a single response, the list is broken up into multiple pages. The response then includes a next_cursor within the response_metadata to allow navigation between pages.