File tree Expand file tree Collapse file tree 11 files changed +270
-5
lines changed Expand file tree Collapse file tree 11 files changed +270
-5
lines changed Original file line number Diff line number Diff line change 1+ function Get-RequestHeader {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory = $true , Position = 0 )]
5+ [string ]$Token
6+ )
7+
8+ begin {
9+ Write-Verbose " Cmdlet Get-RequestHeader - Begin"
10+ }
11+
12+ process {
13+ Write-Verbose " Cmdlet Get-RequestHeader - Process"
14+
15+ $headers = @ {' Authorization' = " Bearer $Token " ; }
16+ $headers
17+ }
18+
19+ end {
20+ Write-Verbose " Cmdlet Get-RequestHeader - End"
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ function Invoke-SlackAPI {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory = $true , Position = 0 )]
5+ [string ]$Method ,
6+ [Parameter (Mandatory = $true , Position = 1 )]
7+ [string ]$Token ,
8+ [Parameter (Mandatory = $false , Position = 2 )]
9+ [System.Collections.Hashtable ]$Parameters
10+ )
11+
12+ begin {
13+ Write-Verbose " Cmdlet Invoke-SlackAPI - Begin"
14+ }
15+
16+ process {
17+ Write-Verbose " Cmdlet Invoke-SlackAPI - Process"
18+ $headers = Get-RequestHeader $Token
19+
20+ $uri = " https://slack.com/api/$Method "
21+ if ($Parameters.Keys.Count -gt 0 ) {
22+ $qs = [System.Web.HttpUtility ]::ParseQueryString([String ]::Empty)
23+ foreach ($key in $Parameters.Keys ) {
24+ $qs.Add ($key , $Parameters .$key )
25+ }
26+ $uriRequest = [System.UriBuilder ]$uri
27+ $uriRequest.Query = $qs.ToString ()
28+ $uri = $uriRequest.ToString ()
29+ }
30+
31+ $response = Invoke-WebRequest - Method Get - Uri $uri - Headers $headers
32+ $response = $response.Content | ConvertFrom-Json
33+ if ($response.ok ) {
34+ $response
35+ }
36+ else {
37+ Write-Error $response.error
38+ }
39+ }
40+
41+ end {
42+ Write-Verbose " Cmdlet Invoke-SlackAPI - End"
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ function Get-FullHistory {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory = $true , Position = 0 )]
5+ [string ]$Token ,
6+ [Parameter (Mandatory = $true , Position = 1 )]
7+ [string ]$ChannelId ,
8+ [Parameter (Mandatory = $true , Position = 2 )]
9+ [string ]$Start
10+ )
11+
12+ begin {
13+ Write-Verbose " Cmdlet Get-FullHistory - Begin"
14+ }
15+
16+ process {
17+ Write-Verbose " Cmdlet Get-FullHistory - Process"
18+ $channelMessages = @ ()
19+ do {
20+ $response = Get-History - Token $Token - ChannelId $ChannelId - Oldest $Start - Limit 1000
21+ if ($response.ok -eq $false ) {
22+ Write-Error $response.error
23+ }
24+ $sorted = $response.messages | Sort-Object { Convert-EpochToDate ($_.ts ) }
25+ $last = $sorted | Select-Object - Last 1
26+ $Start = $last.ts
27+ $channelMessages += $sorted
28+ } while ($response.has_more -and $response )
29+ $channelMessages | ? { $_.thread_ts -ne $null } | % {
30+ $response = Get-Thread - Token $Token - ChannelId $ChannelId - ThreadTs $_.ts
31+ if ($response.ok -eq $false ) {
32+ Write-Error $response.error
33+ }
34+ $channelMessages += $response.messages | Select-Object - Skip 1
35+ }
36+ $channelMessages | Sort-Object { Convert-EpochToDate ($_.ts ) }
37+ }
38+
39+ end {
40+ Write-Verbose " Cmdlet Get-FullHistory - End"
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ function Get-Channel {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory = $true , Position = 0 )]
5+ [string ]$Token ,
6+ [Parameter (Mandatory = $false , Position = 1 )]
7+ [int ]$Limit = 1000
8+ )
9+
10+ begin {
11+ Write-Verbose " Cmdlet Get-Channel - Begin"
12+ }
13+
14+ process {
15+ Write-Verbose " Cmdlet Get-Channel - Process"
16+ $params = @ {
17+ ' limit' = $Limit
18+ }
19+ Invoke-SlackAPI - Method ' conversations.list' - Token $Token - Parameters $params
20+ }
21+
22+ end {
23+ Write-Verbose " Cmdlet Get-Channel - End"
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ function Get-History {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory = $true , Position = 0 )]
5+ [string ]$Token ,
6+ [Parameter (Mandatory = $true , Position = 1 )]
7+ [string ]$ChannelId ,
8+ [Parameter (Mandatory = $true , Position = 2 )]
9+ [string ]$Oldest ,
10+ [Parameter (Mandatory = $false , Position = 3 )]
11+ [int ]$Limit = 100
12+ )
13+
14+ begin {
15+ Write-Verbose " Cmdlet Get-History - Begin"
16+ }
17+
18+ process {
19+ Write-Verbose " Cmdlet Get-History - Process"
20+ $params = @ {
21+ ' channel' = $ChannelId ;
22+ ' oldest' = $Oldest ;
23+ ' limit' = $Limit
24+ }
25+ Invoke-SlackAPI - Method ' conversations.history' - Token $Token - Parameters $params
26+ }
27+
28+ end {
29+ Write-Verbose " Cmdlet Get-History - End"
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ function Get-Thread {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory = $true , Position = 0 )]
5+ [string ]$Token ,
6+ [Parameter (Mandatory = $true , Position = 1 )]
7+ [string ]$ChannelId ,
8+ [Parameter (Mandatory = $true , Position = 2 )]
9+ [string ]$ThreadTs ,
10+ [Parameter (Mandatory = $false , Position = 3 )]
11+ [int ]$Limit = 1000
12+ )
13+
14+ begin {
15+ Write-Verbose " Cmdlet Get-Thread - Begin"
16+ }
17+
18+ process {
19+ Write-Verbose " Cmdlet Get-Thread - Process"
20+ $params = @ {
21+ ' channel' = $ChannelId ;
22+ ' ts' = $ThreadTs ;
23+ ' limit' = $Limit
24+ }
25+ Invoke-SlackAPI - Method ' conversations.replies' - Token $Token - Parameters $params
26+ }
27+
28+ end {
29+ Write-Verbose " Cmdlet Get-Thread - End"
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ function Test-Auth {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory = $true , Position = 0 )]
5+ [string ]$Token
6+ )
7+
8+ begin {
9+ Write-Verbose " Cmdlet Test-Auth - Begin"
10+ }
11+
12+ process {
13+ Write-Verbose " Cmdlet Test-Auth - Process"
14+
15+ $response = Invoke-SlackAPI - Method ' auth.test' - Token $Token
16+ if ($response.ok -eq $false ) {
17+ Write-Error $response.error
18+ $false
19+ }
20+ else {
21+ $true
22+ }
23+ }
24+
25+ end {
26+ Write-Verbose " Cmdlet Test-Auth - End"
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ function Convert-DateToEpoch {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory = $true , Position = 0 )]
5+ [datetime ]$end
6+ )
7+
8+ begin {
9+ Write-Verbose " Cmdlet Convert-DateToEpoch - Begin"
10+ }
11+
12+ process {
13+ Write-Verbose " Cmdlet Convert-DateToEpoch - Process"
14+ [datetime ]$origin = ' 1970-01-01 00:00:00'
15+ (New-TimeSpan - Start $origin - End $end ).TotalSeconds
16+ }
17+
18+ end {
19+ Write-Verbose " Cmdlet Convert-DateToEpoch - End"
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ function Convert-EpochToDate {
2+ [CmdletBinding ()]
3+ param (
4+ [Parameter (Mandatory = $true , Position = 0 )]
5+ [double ]$ctime
6+ )
7+
8+ begin {
9+ Write-Verbose " Cmdlet Convert-EpochToDate - Begin"
10+ }
11+
12+ process {
13+ Write-Verbose " Cmdlet Convert-EpochToDate - Process"
14+ [datetime ]$origin = ' 1970-01-01 00:00:00'
15+ $origin.AddSeconds ($ctime )
16+ }
17+
18+ end {
19+ Write-Verbose " Cmdlet Convert-EpochToDate - End"
20+ }
21+ }
Original file line number Diff line number Diff line change 33 RootModule = ' Slack.Backup.psm1'
44 ModuleVersion = ' 1.0.0'
55 GUID = ' 5fca212b-de20-40cb-ba38-e6114263bfaa'
6- Author = ' Alan Płócieniak '
7- CompanyName = ' Alan Płócieniak '
8- Copyright = ' (c) 2022 Alan Płócieniak . All rights reserved.'
6+ Author = ' Alan Plocieniak '
7+ CompanyName = ' Alan Plocieniak '
8+ Copyright = ' (c) 2022 Alan Plocieniak . All rights reserved.'
99 Description = ' PowerShell module for the Slack backup.'
1010 PowerShellVersion = ' 5.0'
1111 FunctionsToExport = ' *'
You can’t perform that action at this time.
0 commit comments