Edits history of script submission #23041 for ' Search Messages (slack)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { WebClient } from "@slack/web-api"
    
    /**
     * Search Messages
     * 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`).
     */
    export async function main(
      auth: RT.Slack,
      query: string,
      sort?: "score" | "timestamp",
      sort_dir?: "asc" | "desc",
      count?: number,
      page?: number
    ) {
      const client = new WebClient(auth.token)
      return await client.search.messages(
        dropEmpty({ query, sort, sort_dir, count, page })
      )
    }
    
    function dropEmpty<T extends { [key: string]: any }>(params: T) {
      return Object.fromEntries(
        Object.entries(params).filter(
          ([, v]) => v !== undefined && v !== null && v !== ""
        )
      ) as T
    }
    

    Submitted by hugo989 1 hour ago