Skip to content

Instantly share code, notes, and snippets.

@btipling
Last active June 30, 2020 20:14
Show Gist options
  • Save btipling/68d9cce7c230211cf29d8006fa24920e to your computer and use it in GitHub Desktop.
Save btipling/68d9cce7c230211cf29d8006fa24920e to your computer and use it in GitHub Desktop.
Setting my slack status via powershell
param ([string] $Status)
function Invoke-SlackRequest([string]$Url, [hashtable]$data, [hashtable]$headers) {
Write-Host "Making a request to slack @ $Url"
$Body = ConvertTo-Json -InputObject $data
Write-Host "Sending body: $Body"
$headers["Content-type"] = "application/json; charset=utf-8";
$result = Invoke-RestMethod -Method 'Post' -Uri $url -Body $body -Headers $headers
if (!$result.ok) {
Write-Error "Not OK!"
Write-Error $result
Write-Error $result.extended
exit 1
}
return $result
}
if ($status.Length -gt 100) {
Write-Error "Status length too long!"
$length = $status.Length
Write-Error "Your status was $length"
exit 2
}
Write-Host "Setting slack status to:`n'$Status'"
$identityToken = $Env:slack_identity_token
$token = $Env:slack_token
$Url = "https://slack.com/api/users.identity"
$headers = @{"Authorization" = "Bearer $identityToken" }
$result = Invoke-SlackRequest -Url $Url -Body $Body -headers $headers
if ($null -ne $result) {
$userID = $result.user.id
Write-Host "User ID: $userID"
$Url = "https://slack.com/api/users.info?user=$userID"
$headers = @{"Authorization" = "Bearer $token" }
$result = Invoke-SlackRequest -Url $Url -headers $headers
if ($null -ne $result) {
Write-Host "got a user profile"
Write-Host $result.user
Write-Host "user: "
Write-Host $result.user.id
$headers = @{
"Authorization" = "Bearer $token";
}
Write-Host "Setting status"
$Url = "https://slack.com/api/users.profile.set"
$data = @{
token = $token;
user = $userID;
profile = @{
status_text = $Status;
status_expiration = 0;
}
}
$result = Invoke-SlackRequest -Url $Url -data $data -headers $headers
}
}
Write-Host "~~all done~~"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment