forked from RamblingCookieMonster/PSSlack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Get-SlackChannel, Add Get-SlackHistory
- Loading branch information
1 parent
cd3dbd8
commit 14dc580
Showing
6 changed files
with
222 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,65 @@ | ||
# Parse output from search.messages | ||
Function Parse-SlackMessage { | ||
[cmdletbinding()] | ||
param( $InputObject ) | ||
param( | ||
$InputObject, | ||
[switch]$Match | ||
) | ||
|
||
function Extract-Previous { | ||
param($Message) | ||
"@{0}: {1}" -f $Message.Username, $Message.Text | ||
if($Message.username -or $Message.Text) | ||
{ | ||
"@{0}: {1}" -f $Message.Username, $Message.Text | ||
} | ||
else | ||
{ | ||
$null | ||
} | ||
} | ||
|
||
foreach($Message in $InputObject.messages.matches) | ||
if($Match) | ||
{ | ||
[pscustomobject]@{ | ||
PSTypeName = 'PSSlack.SearchResult' | ||
Username = $Message.username | ||
Channel = $Message.channel.name | ||
Text = $Message.text | ||
Attachments = $Message.Attachments | ||
File = $Message.File | ||
Type = $Message.Type | ||
SubType = $Message.subtype | ||
Timestamp = ConvertFrom-UnixTime $Message.ts | ||
Permalink = $Message.permalink | ||
Previous = Extract-Previous $Message.Previous | ||
Previous_2 = Extract-Previous $Message.Previous_2 | ||
Next = Extract-Previous $Message.Next | ||
Next_2 = Extract-Previous $Message.Next_2 | ||
$Messages = $InputObject.messages.matches | ||
$pstypename = 'PSSlack.SearchResult' | ||
|
||
foreach($Message in $Messages) | ||
{ | ||
[pscustomobject]@{ | ||
PSTypeName = $pstypename | ||
Username = $Message.username | ||
Channel = $Message.channel.name | ||
Text = $Message.text | ||
Attachments = $Message.Attachments | ||
File = $Message.File | ||
Type = $Message.Type | ||
SubType = $Message.subtype | ||
Timestamp = ConvertFrom-UnixTime $Message.ts | ||
Permalink = $Message.permalink | ||
Previous = Extract-Previous $Message.Previous | ||
Previous_2 = Extract-Previous $Message.Previous_2 | ||
Next = Extract-Previous $Message.Next | ||
Next_2 = Extract-Previous $Message.Next_2 | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
$Messages = $InputObject.messages | ||
$pstypename = 'PSSlack.History' | ||
|
||
|
||
foreach($Message in $Messages) | ||
{ | ||
[pscustomobject]@{ | ||
PSTypeName = $pstypename | ||
Username = $Message.username | ||
Text = $Message.text | ||
Attachments = $Message.Attachments | ||
File = $Message.File | ||
Type = $Message.Type | ||
SubType = $Message.subtype | ||
Timestamp = ConvertFrom-UnixTime $Message.ts | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
function Get-SlackHistory { | ||
<# | ||
.SYNOPSIS | ||
Get history from a Slack channel | ||
.DESCRIPTION | ||
Get history from a Slack channel | ||
.PARAMETER Token | ||
Specify a token for authorization. | ||
See 'Authentication' section here for more information: https://api.slack.com/web | ||
Test tokens are a simple way to use this | ||
.PARAMETER Id | ||
One or more channel IDs to extract history from. | ||
.PARAMETER Before | ||
Return history from before this date | ||
.PARAMETER After | ||
Return history from after this date | ||
.PARAMETER Inclusive | ||
If specified, include history from the date specified in Before and/or After parameters | ||
.PARAMETER Count | ||
Number of messages to return. Defaults to 100. Max 1000 | ||
.PARAMETER Raw | ||
If specified, we provide raw output and do not parse any responses | ||
.FUNCTIONALITY | ||
Slack | ||
#> | ||
[cmdletbinding()] | ||
param ( | ||
$Token = $Script:PSSlack.Token, | ||
[parameter( ValueFromPipelineByPropertyName = $True)] # a bit iffy... ID is common... | ||
[Alias('ID')] | ||
[string[]]$ChannelID, | ||
|
||
[ValidateRange(1,1000)] | ||
[int]$Count = 100, | ||
[switch]$Inclusive, | ||
[switch]$Raw | ||
|
||
) | ||
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 | Out-String)" | ||
|
||
$body = @{ channel = $null } | ||
if($PSBoundParameters.ContainsKey('Before')) | ||
{ | ||
$BeforeTS = Get-UnixTime -Date $Before | ||
$body.add('oldest', $BeforeTS) | ||
} | ||
if($PSBoundParameters.ContainsKey('After')) | ||
{ | ||
$AfterTS = Get-UnixTime -Date $After | ||
$body.add('latest', $AfterTS) | ||
} | ||
if($Inclusive) | ||
{ | ||
$body.add('inclusive', 1) | ||
} | ||
$params = @{ | ||
Token = $Token | ||
Method = 'channels.history' | ||
Body = $body | ||
} | ||
|
||
} | ||
process | ||
{ | ||
foreach($ID in $ChannelID) | ||
{ | ||
$Params.body.channel = $ID | ||
$response = Send-SlackApi @params | ||
if ($response.ok) | ||
{ | ||
if($Raw) | ||
{ | ||
$link = "$($Script:PSSlack.ArchiveUri)/$($response.channel)/p$($response.ts -replace '\.')" | ||
$response | Add-Member -MemberType NoteProperty -Name link -Value $link | ||
$response | ||
} | ||
else | ||
{ | ||
Parse-SlackMessage -InputObject $Response | ||
} | ||
} | ||
else | ||
{ | ||
$response | ||
} | ||
} | ||
} | ||
} |