1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import process from "node:process";
export async function updateTicketCoordinator() {
// read user profiles and fint those who want to be a tickets master and add those to group @tickets-coordinators
// Require the Node Slack SDK package (github.com/slackapi/node-slack-sdk)
const { WebClient, LogLevel } = import("npm:@slack/web-api");
// read all
const slackAccessToken = process.env.slackAccessToken;
if (!slackAccessToken) {
console.error("Missing environment variable: slackAccessToken");
process.exit(1);
}
const teamId = process.env.teamId || "T1234567890";
const fieldId = process.env.fieldId || "Xf05EN1QF3HN";
const groupId = process.env.groupId || "S05DVA7MFHT";
console.log(
`Use teamId: ${teamId}. Specify environment variable "teamId" to override.`,
);
console.log(
`Use fieldId: ${fieldId}. Specify environment variable "fieldId" to override.`,
);
console.log(
`Use groupId: ${groupId}. Specify environment variable "groupId" to override.`,
);
try {
// InitializeSlack API client
const client = new WebClient(slackAccessToken, { logLevel: LogLevel.WARN });
// Fetch all users
const result = await client.users.list({ team_id: teamId });
let users = result.members;
users = users.filter((user) => !user.deleted);
users = users.filter((user) => !!user.is_email_confirmed);
users = users.filter((user) =>
!user.is_restricted && !user.is_ultra_restricted
);
console.log(`Found ${users.length} users in team ${teamId}`);
// Fetch extended user profiles with custom fields and filter only coordinators
const profiles = await Promise.all(users.map(async (user) => {
const result = await client.users.profile.get({ user: user.id });
const fields = result?.profile?.fields;
return {
id: user.id,
displayName: user.profile.display_name,
ticketsCoordinator: !!(fields[fieldId]?.value == "Yes"),
};
}));
console.log(`Fetched ${profiles.length} profiles in team ${teamId}`);
const ticketsCoordinators = profiles.filter((profile) =>
profile.ticketsCoordinator
);
console.log(
`Filtered ${ticketsCoordinators.length} tickets coordinators in team ${teamId}`,
);
// Update target user group
await client.usergroups.users.update({
usergroup: groupId,
team_id: teamId,
users: ticketsCoordinators.map((user) => user.id),
});
console.log(
`Group ${groupId} updated with ${ticketsCoordinators.length} users`,
);
}
catch (error) {
console.error(error);
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023