Created
November 30, 2023 16:01
-
-
Save narze/b5506036995d9b7c7a73a908545c3431 to your computer and use it in GitHub Desktop.
Fetch & count users in Slack general channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
async function getUsersInGeneralChannel(marker = null) { | |
const response = await fetch( | |
"https://edgeapi.slack.com/cache/TSK2FNWP3/users/list?fp=ed", | |
{ | |
headers: { | |
accept: "*/*", | |
"accept-language": "en-US,en;q=0.9", | |
"content-type": "text/plain;charset=UTF-8", | |
"sec-ch-ua": '"Not)A;Brand";v="24", "Chromium";v="116"', | |
"sec-ch-ua-mobile": "?0", | |
"sec-ch-ua-platform": '"macOS"', | |
"sec-fetch-dest": "empty", | |
"sec-fetch-mode": "cors", | |
"sec-fetch-site": "same-site", | |
cookie: | |
"b=.235697e4fa56914f1fb02ade40ef7a52; shown_ssb_redirect_page=1; ssb_instance_id=7356ec58-264f-4ff4-863a-4e9cf71e4d6e; shown_download_ssb_modal=1; show_download_ssb_banner=1; no_download_ssb_banner=1; tz=420; x=235697e4fa56914f1fb02ade40ef7a52.1693504339; OptanonConsent=isGpcEnabled=0&datestamp=Fri+Sep+01+2023+00%3A52%3A20+GMT%2B0700+(Indochina+Time)&version=202211.1.0&isIABGlobal=false&hosts=&consentId=3ca93d64-4d5d-4b3e-a591-8762845ce57e&interactionCount=1&landingPath=NotLandingPage&groups=1%3A1%2C3%3A1%2C2%3A1%2C4%3A0&AwaitingReconsent=false; d-s=1693504349; d=xoxd-jjDs34SoZLhrPjaPL9buAriA5Zggg5cQN3tPgM2QXBkwMjL9tA0zSXEgYihwmrXNJABD0qCmtRwxp%2B4TIFG260UqJwi10lMKuiYkR%2BVjnZj9wEA2vpHX51w9qi7%2F0OkPGmaKD2HPlg5dz%2FURkUeSgLwdlZ1EkrHePOKTQFmKk86WFgcXY%2BPzefTE4H60rsBTG73cAHnJU70%3D", | |
}, | |
referrerPolicy: "no-referrer", | |
body: JSON.stringify({ | |
token: | |
"xoxc-903083778785-2282680854020-4318664012532-2f8d3d868eece196b1267eb0307a20c19b2ed2d4bacc33dc1675d6eea13ce1ed", | |
include_profile_only_users: true, | |
count: 50, | |
channels: ["CSX43GMK9"], | |
filter: "people", | |
index: "users_by_display_name", | |
locale: "en-US", | |
present_first: false, | |
marker: marker, | |
fuzz: 1, | |
}), | |
method: "POST", | |
} | |
); | |
return await response.json(); | |
} | |
async function whosHere() { | |
let users = []; | |
let next_marker = null; | |
while (true) { | |
const resp = await getUsersInGeneralChannel(next_marker); | |
console.log({ resp }); | |
users = [ | |
...users, | |
...resp.results.map((member) => { | |
if (member.profile.display_name.length) { | |
return `- @${member.profile.display_name} [${member.profile.real_name}]`; | |
} else { | |
return `- @${member.profile.real_name}`; | |
} | |
}), | |
]; | |
console.log({ next_marker: resp.next_marker, count: users.length }); | |
if (resp.next_marker) { | |
next_marker = resp.next_marker; | |
await wait(); | |
} else { | |
break; | |
} | |
} | |
users = users.sort((a, b) => { | |
const nameA = a.toLowerCase(); | |
const nameB = b.toLowerCase(); | |
if (nameA < nameB) return -1; | |
if (nameA > nameB) return 1; | |
return 0; | |
}); | |
return users; | |
} | |
function wait() { | |
const min = 500; // 0.5 seconds in milliseconds | |
const max = 2000; // 2 seconds in milliseconds | |
const delay = Math.floor(Math.random() * (max - min + 1) + min); | |
return new Promise((resolve) => setTimeout(resolve, delay)); | |
} | |
function pbcopy(data) { | |
var proc = require("child_process").spawn("pbcopy"); | |
proc.stdin.write(data); | |
proc.stdin.end(); | |
} | |
const result = await whosHere(); | |
let data = [ | |
`# Updated ${new Date().toISOString()}`, | |
`Count: ${result.length}`, | |
"", | |
].join("\n"); | |
data += result.join("\n"); | |
pbcopy(data); | |
console.log(`Done! (${result.length} results are copied to clipboard)`); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment