0

Get Thread Replies

by
Published today

Fetch a thread's parent message and replies. Needs channels:history. Private channels and DMs need a user token (groups:history / im:history).

Script slack Verified

The script

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

3
/**
4
 * Get Thread Replies
5
 * Fetch a thread: the parent message first, then its replies, oldest first. `ts` is the parent message's timestamp. A bot token (channels:history) can read threads in public channels the app is in. Threads in private channels, DMs and group DMs need a user token with groups:history, im:history or mpim:history. Paginate with `response_metadata.next_cursor`.
6
 */
7
export async function main(
8
  auth: RT.Slack,
9
  channel: string,
10
  ts: string,
11
  oldest?: string,
12
  latest?: string,
13
  inclusive?: boolean,
14
  limit?: number,
15
  cursor?: string
16
) {
17
  const client = new WebClient(auth.token)
18
  return await client.conversations.replies(
19
    dropEmpty({ channel, ts, oldest, latest, inclusive, limit, cursor })
20
  )
21
}
22

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