Skip to content

Instantly share code, notes, and snippets.

@mobeigi
Last active October 17, 2022 19:24
Show Gist options
  • Save mobeigi/8e5372f1e14e2a302e186d1753f9a649 to your computer and use it in GitHub Desktop.
Save mobeigi/8e5372f1e14e2a302e186d1753f9a649 to your computer and use it in GitHub Desktop.
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-1657006109.008" +
"&_x_version_ts=noversion" +
"&_x_gantry=true" +
"&fp=90",
{
method: 'post',
credentials: 'include',
headers: { "Content-type": "application/x-www-form-urlencoded" },
body: "token=" + localConfigJson.teams[slackTeamId].token +
"&only_self_subteams=true" +
"&flannel_api_ver=4" +
"&include_min_version_bump_check=1" +
"&version_ts=1656843447" +
"&build_version_ts=1656843447" +
"&_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=f33ee0a5-1657006109.008" +
"&_x_csid=ZVZ58-3PvLk" +
"&slack_route=" + slackTeamId +
"&_x_version_ts=1656843447" +
"&_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.");
});
@krageon
Copy link

krageon commented Jul 5, 2022

I've fixed it so it works again:

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-1657006109.008" +
    "&_x_version_ts=noversion" +
    "&_x_gantry=true" +
    "&fp=90",
  {
    method: 'post',
    credentials: 'include',
    headers: { "Content-type": "application/x-www-form-urlencoded" },
    body: "token=" + localConfigJson.teams[slackTeamId].token +
        "&only_self_subteams=true" +
        "&flannel_api_ver=4" +
        "&include_min_version_bump_check=1" +
        "&version_ts=1656843447" +
        "&build_version_ts=1656843447" +
        "&_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=f33ee0a5-1657006109.008" +
            "&_x_csid=ZVZ58-3PvLk" +
            "&slack_route=" + slackTeamId +
            "&_x_version_ts=1656843447" +
            "&_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.");
});

@mobeigi
Copy link
Author

mobeigi commented Jul 30, 2022

@krageon Thanks, updated original gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment