Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Slack User Notification Preference Bulk Update
// Slack User Notification Preference Bulk Update
// By Mo Beigi
const slackTeamId = "EXAMPLE17";
const localConfigJson = JSON.parse(localStorage.localConfig_v2);
const slackUrl = localConfigJson.teams[slackTeamId].url;
let channel_ids = [];
// client.boot contains list of channel ids user is subscribed to amongst other things
await fetch(slackUrl + "api/client.boot?" +
"_x_id=noversion-1609923716.289" +
"&_x_version_ts=noversion" +
"&_x_gantry=true",
{
method: 'post',
credentials: 'include',
headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" },
body: "token=" + localConfigJson.teams[slackTeamId].token +
"&only_self_subteams=1" +
"&flannel_api_ver=4" +
"&include_min_version_bump_check=1" +
"&version_ts=1609901609" +
"&_x_reason=deferred-data" +
"&_x_sonic=true",
},
)
.then(result => result.json())
.then(result => channel_ids = result.channels);
// iterate channel id list and set notification prefs asynchronously
let fetchPromises = [];
for (i = 0; i < channel_ids.length; ++i) {
console.log("Setting user notification prefs for channel id: " + channel_ids[i].id + " ...");
fetchPromises.push(
fetch(slackUrl + "api/users.prefs.setNotifications?" +
"_x_id=03bd03b3-1609923922.760" +
"&_x_csid=YEOKuX4kPF4" +
"&slack_route=" + localConfigJson.teams[slackTeamId].enterprise_id + ":" + slackTeamId +
"&_x_version_ts=1609901609" +
"&_x_gantry=true",
{
method: 'post',
credentials: 'include',
headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" },
body: "name=suppress_at_channel" +
"&value=true" +
"&channel_id=" + channel_ids[i].id +
"&global=false" +
"&sync=false" +
"&token=" + localConfigJson.teams[slackTeamId].token +
"&_x_reason=prefs-store/setChannelNotificationOverride" +
"&_x_mode=online" +
"&_x_sonic=true"
}
)
.then(result => result.json())
.then(console.log("Done (" + channel_ids[i].id + ")"))
);
// Generous delay to get around rate limit
await new Promise(r => setTimeout(r, 250));
}
Promise.all(fetchPromises).then(function() {
console.log("Successfully set user notification prefs for " + channel_ids.length + " channels.");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment