0

List Users

by
Published Jun 6, 2022
Script slack Verified

The script

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

3
/**
4
 * @param cursor Paginate the list of users by setting the cursor parameter
5
 * to a `next_cursor` attribute returned by a previous request's
6
 * `response_metadata`. Default value fetches the first "page" of the users.
7
 * Used in conjunction with `limit`.
8
 *
9
 * @param limit The maximum number of users to return. Fewer than the
10
 * requested number of users may be returned, even if the end of the result
11
 * list hasn't been reached.
12
 * Used in conjunction with `cursor`.
13
 */
14
type Slack = {
15
  token: string;
16
};
17
export async function main(auth: Slack, cursor?: string, limit?: number) {
18
  const client = new WebClient(auth.token);
19
  const params = removeObjectEmptyFields({ cursor, limit });
20
  return await client.users.list(params);
21
}
22

23
function removeObjectEmptyFields(
24
  object?: Record<string, any>,
25
  removeEmptyArraysAndObjects = true,
26
  createNewObject = true,
27
) {
28
  if (!object || typeof object !== "object") return {}
29
  const obj = createNewObject ? { ...object } : object
30
  const emptyValues = [undefined, null, ""]
31
  for (const key in obj) {
32
    const value = obj[key]
33
    if (emptyValues.includes(value)) {
34
      delete obj[key]
35
    } else if (typeof value === "object") {
36
      if (Object.keys(value).length) {
37
        obj[key] = removeObjectEmptyFields(value, removeEmptyArraysAndObjects, false)
38
      }
39
      if (!Object.keys(value).length && removeEmptyArraysAndObjects) {
40
        delete obj[key]
41
      }
42
    }
43
  }
44
  return obj
45
}
46

Other submissions
  • Submitted by rossmccrann Deno
    Created 1449 days ago
    1
    import * as wmill from "https://deno.land/x/[email protected]/index.ts";
    2
    import { WebClient } from "https://deno.land/x/[email protected]/mod.ts";
    3
    /*
    4
    @param: {string} channel - encoded team id to list users in, required if org token is used.
    5
    @param: {boolean} include_locale - Set this to true to receive the locale for users. Defaults to false
    6
    @param: {number} limit - The maximum number of items to return.
    7
    */
    8
    
    
    9
    export async function main(
    10
      slack_auth: wmill.Resource<"slack">,
    11
      team_id: string,
    12
      include_locale: boolean = false,
    13
      limit: number = 0,
    14
    ) {
    15
      const web = new WebClient(slack_auth);
    16
    
    
    17
      let response = await web.users.list({
    18
        team_id: team_id,
    19
        include_locale: include_locale,
    20
        limit: limit,
    21
      });
    22
    
    
    23
      return { "response": response };
    24
    }
  • Submitted by user471 Deno
    Created 1211 days ago
    1
    import { Resource } from "https://deno.land/x/[email protected]/mod.ts"
    2
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    3
    import { SlackAPI } from "https://deno.land/x/[email protected]/mod.ts";
    4
    
    
    5
    /**
    6
     * @param cursor Paginate the list of users by setting the cursor parameter 
    7
     * to a `next_cursor` attribute returned by a previous request's 
    8
     * `response_metadata`. Default value fetches the first "page" of the users. 
    9
     * Used in conjunction with `limit`.
    10
     * 
    11
     * @param limit The maximum number of users to return. Fewer than the 
    12
     * requested number of users may be returned, even if the end of the result 
    13
     * list hasn't been reached. 
    14
     * Used in conjunction with `cursor`.
    15
     */
    16
    export async function main(
    17
      auth: Resource<'slack'>,
    18
      cursor?: string,
    19
      limit?: number
    20
    ) {
    21
      const client = SlackAPI(auth.token)
    22
      const params = removeObjectEmptyFields({ cursor, limit })
    23
      return await client.users.list(params)
    24
    }
    25
    
    
  • Submitted by adam186 Deno
    Created 384 days ago
    1
    import { removeObjectEmptyFields } from "https://deno.land/x/[email protected]/mod.ts";
    2
    import { SlackAPI } from "https://deno.land/x/[email protected]/mod.ts";
    3
    
    
    4
    /**
    5
     * @param cursor Paginate the list of users by setting the cursor parameter
    6
     * to a `next_cursor` attribute returned by a previous request's
    7
     * `response_metadata`. Default value fetches the first "page" of the users.
    8
     * Used in conjunction with `limit`.
    9
     *
    10
     * @param limit The maximum number of users to return. Fewer than the
    11
     * requested number of users may be returned, even if the end of the result
    12
     * list hasn't been reached.
    13
     * Used in conjunction with `cursor`.
    14
     */
    15
    type Slack = {
    16
      token: string;
    17
    };
    18
    export async function main(auth: Slack, cursor?: string, limit?: number) {
    19
      const client = SlackAPI(auth.token);
    20
      const params = removeObjectEmptyFields({ cursor, limit });
    21
      return await client.users.list(params);
    22
    }
    23