0

Update Message

by
Published today

Edit a message's text or blocks. Needs chat:write; only messages posted by the same token's user or app can be edited.

Script slack Verified

The script

Submitted by hugo989 Bun
Verified 1 hour ago
1
import { WebClient } from "@slack/web-api"
2

3
/**
4
 * Update Message
5
 * Replace the text and/or Block Kit blocks of a message identified by its channel and `ts`. A bot token can only edit the app's own messages, a user token only the user's own. Needs chat:write.
6
 */
7
export async function main(
8
  auth: RT.Slack,
9
  channel: string,
10
  ts: string,
11
  text?: string,
12
  blocks?: { [key: string]: any }[]
13
) {
14
  const client = new WebClient(auth.token)
15
  return await client.chat.update(
16
    dropEmpty({ channel, ts, text, blocks }) as any
17
  )
18
}
19

20
function dropEmpty<T extends { [key: string]: any }>(params: T) {
21
  return Object.fromEntries(
22
    Object.entries(params).filter(
23
      ([, v]) => v !== undefined && v !== null && v !== ""
24
    )
25
  ) as T
26
}
27