Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix API deprecation 2021-02-24 #115

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion PSSlack/Public/Get-SlackChannel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

if($Name -and -not $HasWildCard)
{
# torn between independent queries, or filtering channels.list
# torn between independent queries, or filtering conversations.list
# submit a PR if this isn't performant enough or doesn't make sense.
$Channels = $RawChannels.channels |
Where-Object {$Name -Contains $_.name}
Expand Down
18 changes: 10 additions & 8 deletions PSSlack/Public/Get-SlackGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@
end
{
Write-Verbose "$($PSBoundParameters | Remove-SensitiveData | Out-String)"

$body = @{
types = 'private_channel';
};
if($ExcludeArchived)
{
$body = @{ exclude_archived = 1 }
$body['exclude_archived'] = 1
}
else
{
$body = @{ exclude_archived = 0 }
$body['exclude_archived'] = 0
}
$params = @{
Body = $body
Token = $Token
Method = 'groups.list'
Method = 'users.conversations'
}
$RawGroups = Send-SlackApi @params

Expand All @@ -62,14 +64,14 @@

if($Name -and -not $HasWildCard)
{
# torn between independent queries, or filtering groups.list
# torn between independent queries, or filtering users.conversations
# submit a PR if this isn't performant enough or doesn't make sense.
$Groups = $RawGroups.groups |
$Groups = $RawGroups.channels |
Where-Object {$Name -Contains $_.name}
}
elseif ($Name -and$HasWildCard)
{
$AllGroups = $RawGroups.groups
$AllGroups = $RawGroups.channels

# allow like operator on each group requested in the param, avoid dupes
$GroupHash = [ordered]@{}
Expand All @@ -87,7 +89,7 @@
}
else # nothing specified
{
$Groups = $RawGroups.groups
$Groups = $RawGroups.channels
}

if($Raw)
Expand Down
157 changes: 7 additions & 150 deletions PSSlack/Public/Get-SlackGroupHistory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Get history from a Slack group

.DESCRIPTION
Get history from a Slack group
Get history from a Slack group (this is more or less an alias for Get-SlackHistory).

.PARAMETER Token
Specify a token for authorization.
Expand All @@ -25,7 +25,7 @@
If specified, include history from the date specified in Before and/or After parameters

.PARAMETER Count
Number of messages to return per query. Defaults to 100. Max 1000
Maximum number of messages to return per query (see 'limit' on API docs). Defaults to 100. Max 1000

.PARAMETER Paging
If specified, and more data is available with a given 'Count', continue querying Slack until
Expand Down Expand Up @@ -61,156 +61,13 @@
)
begin
{
function Get-UnixTime {
param($Date)
$unixEpochStart = new-object DateTime 1970,1,1,0,0,0,([DateTimeKind]::Utc)
[int]($Date.ToUniversalTime() - $unixEpochStart).TotalSeconds
}

Write-Verbose "$($PSBoundParameters | Remove-SensitiveData | Out-String)"

$body = @{
channel = $null
count = $count
}
if($Paging)
{
$PageDirection = 'Backward'
}
$BeforeTS = $null
$AfterTS = $null
if($PSBoundParameters.ContainsKey('Before'))
{
$BeforeTS = Get-UnixTime -Date $Before
$body.add('latest', $BeforeTS)
}
if($PSBoundParameters.ContainsKey('After'))
{
$AfterTS = Get-UnixTime -Date $After
$body.add('oldest', $AfterTS)
if(-not $PSBoundParameters.ContainsKey('Before') -and $Paging)
{
$PageDirection = 'Forward'
}
}
if($Inclusive)
{
$body.add('inclusive', 1)
}
$params = @{
Token = $Token
Method = 'groups.history'
Body = $body
if ($PSBoundParameters.ContainsKey('GroupID')) {
$PSBoundParameters['ChannelID'] = $PSBoundParameters['GroupID']
$PSBoundParameters.Remove('GroupID')
}
$Queries = 1

}
process
{
foreach($ID in $GroupID)
{
$has_more = $false
$Params.body.channel = $ID
do
{
if($has_more)
{
if($Params.Body.oldest)
{
[void]$Params.Body.remove('oldest')
}
if($Params.Body.latest)
{
[void]$Params.Body.remove('latest')
}
if($PageDirection -eq 'Forward')
{

$ts = $response.messages.ts | Sort-Object |
Microsoft.PowerShell.Utility\Select-Object -last 1
$Params.body.oldest = $ts
Write-Debug "Paging Forward.`n$(
[pscustomobject]@{
After = $After
Before = $Before
LastTS = $response.messages[-1].ts
SortLast = $response.messages.ts | Sort-Object |
Microsoft.PowerShell.Utility\Select-Object -last 1
SortFirst = $response.messages.ts | Sort-Object |
Microsoft.PowerShell.Utility\Select-Object -first 1
ts = $ts
} | Out-String
)"
}
elseif($PageDirection -eq 'Backward')
{
$ts = $response.messages[-1].ts
if($AfterTS -and $ts -lt $AfterTS)
{
Write-Debug "TS is less than AfterTS, breaking!"
break
}
$Params.body.latest = $ts
Write-Debug "Paging Forward.`n$(
[pscustomobject]@{
After = $After
Before = $Before
LastTS = $response.messages[-1].ts
SortLast = $response.messages.ts | Sort-Object |
Microsoft.PowerShell.Utility\Select-Object -last 1
SortFirst = $response.messages.ts | Sort-Object |
Microsoft.PowerShell.Utility\Select-Object -first 1
ts = $ts
} | Out-String
)"
}

$has_more = $false
Write-Debug "Body is now:$($params.body | out-string)"
}
$response = Send-SlackApi @params

Write-Debug "$($Response | Format-List -Property * | Out-String)"

if ($response.ok)
{

if($response.psobject.properties.name -contains 'has_more' -and $response.has_more)
{
Write-Debug 'Paging engaged!'
$has_more = $true
}

if($Raw)
{
$link = "$($Script:PSSlack.ArchiveUri)/$($response.group)/p$($response.ts -replace '\.')"
$response | Add-Member -MemberType NoteProperty -Name link -Value $link
$response
}
else
{
#Order our messages appropriately according to page direction
if($Paging -and $PageDirection -eq 'Forward')
{
Parse-SlackMessage -InputObject $Response | Sort-Object TimeStamp
}
else
{
Parse-SlackMessage -InputObject $Response
}
}
}
else
{
$response
}
$Queries++
}
until (
-not $Paging -or
-not $has_more -or
($MaxQueries -and $Queries -gt $MaxQueries)
)
}
end {
Get-SlackHistory @PSBoundParameters
}
}
6 changes: 3 additions & 3 deletions PSSlack/Public/Get-SlackHistory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
If specified, include history from the date specified in Before and/or After parameters

.PARAMETER Count
Number of messages to return per query. Defaults to 100. Max 1000
Maximum number of messages to return per query (see 'limit' on API docs). Defaults to 100. Max 1000

.PARAMETER Paging
If specified, and more data is available with a given 'Count', continue querying Slack until
Expand Down Expand Up @@ -64,7 +64,7 @@
Write-Verbose "$($PSBoundParameters | Remove-SensitiveData | Out-String)"
$body = @{
channel = $null
count = $count
limit = $count
}
if($Paging)
{
Expand Down Expand Up @@ -92,7 +92,7 @@
}
$params = @{
Token = $Token
Method = 'channels.history'
Method = 'conversations.history'
Body = $body
}
$Queries = 1
Expand Down
2 changes: 1 addition & 1 deletion PSSlack/Public/Send-SlackAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function Send-SlackApi
$Params = @{
Uri = "https://slack.com/api/$Method"
ErrorAction = 'Stop'
Header = @{ Authorization = "Bearer $Token" }
}
if($Proxy) {
$Params['Proxy'] = $Proxy
Expand All @@ -74,7 +75,6 @@ function Send-SlackApi
if($ForceVerbose) {
$Params.Add('Verbose', $true)
}
$Body.token = $Token

try {
$Response = $null
Expand Down