Skip to content

Commit 31a4ee0

Browse files
committed
Added Slack API
1 parent 1368a64 commit 31a4ee0

11 files changed

+270
-5
lines changed

Private/Get-RequestHeader.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

Private/Invoke-SlackAPI.ps1

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

Public/Get-FullHistory.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

Public/api/Get-Channel.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

Public/api/Get-History.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

Public/api/Get-Thread.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

Public/api/Test-Auth.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

Slack.Backup.psd1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
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 = '*'

Slack.Backup.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Get public and private function definition files.
2-
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
3-
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )
2+
$Public = @( Get-ChildItem -Path $PSScriptRoot\Public -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
3+
$Private = @( Get-ChildItem -Path $PSScriptRoot\Private -Recurse -Filter *.ps1 -ErrorAction SilentlyContinue )
44

55
#Dot source the files
66
Foreach ($import in @($Public + $Private)) {

0 commit comments

Comments
 (0)