Edits history of script submission #23051 for ' Update User Profile (slack)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { WebClient } from "@slack/web-api"
    
    /**
     * Update User Profile
     * Update fields of the connected user's profile, e.g. { "display_name": "Ada", "pronouns": "she/her" }. Only the fields passed change; fields the workspace locks or does not show (often `title`) are ignored without an error, so check the returned profile. Custom profile fields go under "fields", keyed by field ID (from team.profile.get). An admin token on a paid plan can pass `user` to edit someone else. Requires a user token with users.profile:write.
     */
    export async function main(
      auth: RT.Slack,
      profile: { [key: string]: any },
      user?: string
    ) {
      const client = new WebClient(auth.token)
      return await client.users.profile.set(dropEmpty({ profile, user }))
    }
    
    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 2 hours ago