0

Update User Profile

by
Published today

Update fields of the connected user's profile. Requires a user token with users.profile:write.

Script slack Verified

The script

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

3
/**
4
 * Update User Profile
5
 * 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.
6
 */
7
export async function main(
8
  auth: RT.Slack,
9
  profile: { [key: string]: any },
10
  user?: string
11
) {
12
  const client = new WebClient(auth.token)
13
  return await client.users.profile.set(dropEmpty({ profile, user }))
14
}
15

16
function dropEmpty<T extends { [key: string]: any }>(params: T) {
17
  return Object.fromEntries(
18
    Object.entries(params).filter(
19
      ([, v]) => v !== undefined && v !== null && v !== ""
20
    )
21
  ) as T
22
}
23