0

List Files

by
Published today

List files in the workspace, optionally filtered by channel, user, type and date.

Script slack Verified

The script

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

3
/**
4
 * List Files
5
 * List files visible to the token, newest first, optionally filtered by `channel`, uploader `user`, `types` (comma-separated: all, spaces, snippets, images, gdocs, zips, pdfs) and a Unix-seconds `ts_from`/`ts_to` window. Pages with `count` (default 100) + `page`, bounded by `paging.pages`. Needs files:read (user-token default; tick it for a bot token).
6
 */
7
export async function main(
8
  auth: RT.Slack,
9
  channel?: string,
10
  user?: string,
11
  types?: string,
12
  ts_from?: number,
13
  ts_to?: number,
14
  count?: number,
15
  page?: number
16
) {
17
  const client = new WebClient(auth.token)
18
  return await client.files.list(
19
    dropEmpty({ channel, user, types, ts_from, ts_to, count, page }) as any
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