|
| 1 | +Function Set-SlackNotification { |
| 2 | + <# |
| 3 | + .DESCRIPTION Enable or Disable @channel and @here notifications for Slack Channel |
| 4 | + .NOTES Author: William Lam |
| 5 | + .NOTES Site: www.virtuallyghetto.com |
| 6 | + .NOTES Reference: http://www.virtuallyghetto.com/2019/10/automate-disabling-channel-here-notifications-using-private-slack-api.html |
| 7 | + .PARAMETER SlackAccessToken |
| 8 | + This is your OAuth Access Token that you will need to either provide or generate as part of the OAuth login workflow |
| 9 | + .PARAMETER SlackBrowserAccessToken |
| 10 | + This is the OAuth Access Token which will be retrieved from the browser and is scoped to access the private Slack API |
| 11 | + .PARAMETER SlackBrowserSessionCookie |
| 12 | + This is the internal Slack cookie that is required to be able to access the private Slack API |
| 13 | + .PARAMETER Operation |
| 14 | + Enable or Disable |
| 15 | + .PARAMETER ExcludeChannels |
| 16 | + List of Slack Channels to ignore |
| 17 | + .EXAMPLE |
| 18 | + $SlackAccessToken = "xxx" |
| 19 | + $SlackBrowserAccessToken = "xxx" |
| 20 | + $SlackBrowserSessionCookie = "xxx" |
| 21 | + $ExcludeChannels = @( |
| 22 | + "channel1", |
| 23 | + "channel2", |
| 24 | + "channel3" |
| 25 | + ) |
| 26 | +
|
| 27 | + Set-SlackNotification -SlackAccessToken $SlackAccessToken -SlackBrowserAccessToken $SlackBrowserAccessToken -SlackBrowserSessionCookie $SlackBrowserSessionCookie -Operation Disable -ExcludeChannels $ExcludeChannels |
| 28 | + #> |
| 29 | + param( |
| 30 | + [Parameter(Mandatory=$true)][string]$SlackAccessToken, |
| 31 | + [Parameter(Mandatory=$true)][string]$SlackBrowserAccessToken, |
| 32 | + [Parameter(Mandatory=$true)][string]$SlackBrowserSessionCookie, |
| 33 | + [Parameter(Mandatory=$true)][ValidateSet("Enable","Disable")][string]$Operation, |
| 34 | + [Parameter(Mandatory=$false)][string[]]$ExcludeChannels |
| 35 | + ) |
| 36 | + |
| 37 | + $headers = @{ |
| 38 | + "Accept" = "application/x-www-form-urlencoded"; |
| 39 | + } |
| 40 | + |
| 41 | + # Retrieve all Slack channels user is part of |
| 42 | + $conversation_results = Invoke-WebRequest -Uri "https://slack.com/api/users.conversations" -Method GET -Headers $headers -Body @{"token"=$SlackAccessToken;"limit"=1000;"exclude_archived"="true";"types"="public_channel,private_channel";} |
| 43 | + $slack_channels = ($conversation_results.Content|ConvertFrom-Json).channels | Select id,name |
| 44 | + |
| 45 | + # Determine the notification operation |
| 46 | + if($Operation -eq "Enable") { |
| 47 | + Write-Host "Enabling @channel and @here notification for the following channels" |
| 48 | + $notification_value = "false" |
| 49 | + } else { |
| 50 | + Write-Host "Disabling @channel and @here notification for the following channels" |
| 51 | + $notification_value = "true" |
| 52 | + } |
| 53 | + |
| 54 | + # Construct the required cookie to call Private Slack API |
| 55 | + $websession = New-Object Microsoft.PowerShell.Commands.WebRequestSession |
| 56 | + $cookie = New-Object System.Net.Cookie |
| 57 | + $cookie.Name = "d" |
| 58 | + $cookie.Value = $SlackBrowserSessionCookie |
| 59 | + $cookie.Domain = "slack.com" |
| 60 | + $websession.Cookies.Add($cookie) |
| 61 | + |
| 62 | + foreach ($slack_channel in $slack_channels) { |
| 63 | + if( ($ExcludeChannels.toLower()) -NotContains ($slack_channel.name).toLower() ) { |
| 64 | + |
| 65 | + $body = @{ |
| 66 | + "token" = $SlackBrowserAccessToken; |
| 67 | + "name" = "suppress_at_channel"; |
| 68 | + "value" = $notification_value; |
| 69 | + "channel_id" = $($slack_channel.id); |
| 70 | + "global" = "false"; |
| 71 | + "sync" = "false"; |
| 72 | + } |
| 73 | + |
| 74 | + Write-Host "`tUpdating $($slack_channel.name) channel ..." |
| 75 | + try { |
| 76 | + $requests = Invoke-WebRequest -Uri "https://vmware.slack.com/api/users.prefs.setNotifications" -Method Post -Headers $headers -Body $body -WebSession $websession |
| 77 | + if(-not ($requests.Content|ConvertFrom-Json).ok) { |
| 78 | + Write-Error "Failed to update $($slack_channel.name) channel ($($slack_channel.id))" |
| 79 | + Write-Error "`n($requests.Content)`n" |
| 80 | + break |
| 81 | + } |
| 82 | + } catch { |
| 83 | + Write-Error "Error in updating $($slack_channel.name) channel ($($slack_channel.id))" |
| 84 | + Write-Error "`n($_.Exception.Message)`n" |
| 85 | + break |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + Write-Host "Successfully $($Operation)d @channel and @here notification preferences for $($slack_channels.count) Slack Channels!" |
| 90 | +} |
0 commit comments