1

List Conversation History

by
Published Jun 20, 2022

Fetch a conversation's top-level messages. 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 7 days ago
1
import { WebClient } from "@slack/web-api"
2

3
/**
4
 * List Conversation History
5
 * Fetch top-level messages from a channel, DM or group DM, newest first (thread replies need Get Thread Replies). A bot token (channels:history) can read public channels the app is in. Private channels, DMs and group DMs need a user token from someone in them, with groups:history, im:history or mpim:history. Paginate with `response_metadata.next_cursor`.
6
 */
7
export async function main(
8
  slack_auth: RT.Slack,
9
  channel: string,
10
  oldest?: string,
11
  latest?: string,
12
  inclusive?: boolean,
13
  limit?: number,
14
  cursor?: string
15
) {
16
  const web = new WebClient(slack_auth.token)
17

18
  const response = await web.conversations.history(
19
    dropEmpty({ channel, oldest, latest, inclusive, limit, cursor })
20
  )
21

22
  return { response: response }
23
}
24

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

Other submissions
  • Submitted by rossmccrann Deno
    Created 505 days ago
    1
    import { WebClient } from "https://deno.land/x/[email protected]/mod.ts";
    2
    /*
    3
    @param: {string} channel - Conversation ID to fetch history for.
    4
    */
    5
    
    
    6
    type Slack = {
    7
      token: string;
    8
    };
    9
    export async function main(slack_auth: Slack, channel: string) {
    10
      const web = new WebClient(slack_auth.token);
    11
    
    
    12
      let response = await web.conversations.history({
    13
        channel: channel,
    14
      });
    15
    
    
    16
      return { response: response };
    17
    }
    18