Skip to content

Commit

Permalink
Fix Get-SlackChannel, Add Get-SlackHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
RamblingCookieMonster committed May 17, 2016
1 parent cd3dbd8 commit 14dc580
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 35 deletions.
35 changes: 35 additions & 0 deletions PSSlack/PSSlack.Format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>Default</Name>
<ViewSelectedBy>
<TypeName>PSSlack.History</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Width>20</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>20</Width>
</TableColumnHeader>
<TableColumnHeader>
<Width>60</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<Wrap />
<TableColumnItems>
<TableColumnItem>
<PropertyName>Username</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>SubType</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Text</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>Default</Name>
<ViewSelectedBy>
Expand Down
2 changes: 1 addition & 1 deletion PSSlack/Private/Parse-SlackChannel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Function Parse-SlackChannel {
[cmdletbinding()]
param( $InputObject )

foreach($Channel in $InputObject.channels)
foreach($Channel in $InputObject)
{
$TopicSet = $null
$PurposeSet = $null
Expand Down
71 changes: 52 additions & 19 deletions PSSlack/Private/Parse-SlackMessage.ps1
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
}
}
}
}
4 changes: 3 additions & 1 deletion PSSlack/Public/Find-SlackMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@

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
Parse-SlackMessage -InputObject $Response -Match
}
}
else {
Expand Down
39 changes: 25 additions & 14 deletions PSSlack/Public/Get-SlackChannel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
One or more channel names to return. Defaults to all. Accepts wildcards.
.PARAMETER ExcludeArchived
Whether to exclude archived channels. 1 or 0. Defaults to 0 (return all channels, including archived channels)
Whether to exclude archived channels. Default is to include all.
.PARAMETER Raw
If specified, we provide raw output and do not parse any responses
Expand All @@ -28,42 +28,53 @@
param (
$Token = $Script:PSSlack.Token,
[string[]]$Name,

[ValidateSet(0,1)]
[int]$ExcludeArchived = 0,

[switch]$ExcludeArchived,
[switch]$Raw

)
end
{
Write-Verbose "$($PSBoundParameters | Out-String)"

$body = @{exclude_archived = $ExcludeArchived}
$body = @{}
if($ExcludeArchived)
{
$body.add('exclude_archived', 1)
}
$params = @{
Body = $body
Token = $Token
Method = 'channels.list'
}
$RawChannels = Send-SlackApi @params

$HasWildCard = $False
foreach($Item in $Name)
{
if($Item -match '\*')
{
$HasWildCard = $true
break
}
}

if($Name -and $Name -notmatch '\*')
if($Name -and -not $HasWildCard)
{
# torn between independent queries, or filtering channels.list
# submit a PR if this isn't performant enough or doesn't make sense.
$Channels = Send-SlackApi @params |
$Channels = $RawChannels.channels |
Where {$Name -Contains $_.name}
}
elseif ($Name -and $Name -match '\*')
elseif ($Name -and$HasWildCard)
{
$AllChannels = Send-SlackApi @params
$AllChannels = $RawChannels.Channels

# allow like operator on each channel requested in the param, avoid dupes
$ChannelHash = [ordered]@{}
foreach($SlackChannel in $AllChannels)
{
foreach($Chan in $Name)
{
if($SlackChannel.Name -like $Chan -and -not $ChannelHash.ContainsKey($SlackChannel.id))
if($SlackChannel.Name -like $Chan -and -not $ChannelHash.Contains($SlackChannel.id))
{
$ChannelHash.Add($SlackChannel.Id, $SlackChannel)
}
Expand All @@ -73,12 +84,12 @@
}
else # nothing specified
{
$Channels = Send-SlackApi @params
$Channels = $RawChannels.channels
}

if($Raw)
{
$Channels
$RawChannels
}
else
{
Expand Down
106 changes: 106 additions & 0 deletions PSSlack/Public/Get-SlackHistory.ps1
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
}
}
}
}

0 comments on commit 14dc580

Please sign in to comment.