0

Search Messages

by
Published today

Search messages across the workspace. Requires a user token (search:read).

Script slack Verified

The script

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

3
/**
4
 * Search Messages
5
 * Search messages the connected user can see, with Slack search syntax (e.g. `deploy in:#ops from:@alice after:2026-01-01`). Requires a user token with search:read. A bot token fails with `not_allowed_token_type`. Paginate with `page` (up to `messages.paging.pages`).
6
 */
7
export async function main(
8
  auth: RT.Slack,
9
  query: string,
10
  sort?: "score" | "timestamp",
11
  sort_dir?: "asc" | "desc",
12
  count?: number,
13
  page?: number
14
) {
15
  const client = new WebClient(auth.token)
16
  return await client.search.messages(
17
    dropEmpty({ query, sort, sort_dir, count, page })
18
  )
19
}
20

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