-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerSlack.psm1
148 lines (116 loc) · 3.21 KB
/
PowerSlack.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
[string] $script:Slacktoken = "";
[string] $slackUri = "https://slack.com/api"
# Inspired from https://gist.github.com/rdsimes/4333461
function ConvertTo-UnixTimestamp {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[DateTime] $date
)
$epoch = Get-Date -Year 1970 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0
[math]::truncate($date.ToUniversalTime().Subtract($epoch).TotalSeconds)
}
function Validate-Token{
[CmdletBinding()]
param ()
if ([string]::IsNullOrEmpty($script:Slacktoken))
{
throw "Token cannot be empty. Use Set-SlackToken before any call."
}
}
function Set-SlackToken {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $token
)
Write-Verbose "Setting Slack token to $token"
$script:SlackToken = $token
}
function Get-SlackChannels {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[switch] $excludeArchived = $false
)
Validate-Token
$uri = "$slackUri/channels.list"
$params = @{"token" = $script:Slacktoken}
if ($excludeArchived) {
$params.Set_Item("exclude_archived", "1")
}
$result = Invoke-RestMethod -Method "Get" -Uri $uri -ContentType "Application/Json" -body $params
if ($result.ok -eq "true") {
Write-Verbose "Response is good"
foreach ($message in $result.channels) {
$message
}
}
#todo : error handling
}
function Get-SlackChannelHistory {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $channel,
[Parameter(Mandatory = $false)]
[Nullable[DateTime]] $latest = $null,
[Parameter(Mandatory = $false)]
[Nullable[DateTime]] $oldest = $null,
[Parameter(Mandatory = $false)]
[switch] $inclusive = $false,
[ValidateRange(1,1000)]
[Parameter(Mandatory = $false)]
[Nullable[int]] $count = $null,
[Parameter(Mandatory = $false)]
[switch] $unread = $false
)
Validate-Token
$uri = "$slackUri/channels.history"
$params = @{"token" = $script:Slacktoken; "channel" = $channel}
if ($latest) {
$val = ConvertTo-UnixTimestamp $latest
Write-Verbose "Setting latest parameter to $val"
$params.Set_Item("latest", $val)
}
if ($oldest) {
$val = ConvertTo-UnixTimestamp $oldest
Write-Verbose "Setting oldest parameter to $val"
$params.Set_Item("oldest", $val)
}
if ($inclusive) {
$params.Set_Item("inclusive", 1)
}
if ($count) {
$params.Set_Item("count", $count)
}
if ($unread) {
$params.Set_Item("unread", 1)
}
$result = Invoke-RestMethod -Method "Get" -Uri $uri -ContentType "Application/Json" -body $params
if ($result.ok -eq "true") {
Write-Verbose "Response is good"
foreach ($message in $result.messages) {
$message
}
}
#todo : error handling
}
function Remove-SlackMessage {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string] $channel,
[Parameter(Mandatory = $true)]
[string] $ts
)
Validate-Token
$uri = "$slackUri/chat.delete"
$params = @{"token" = $script:Slacktoken; "channel" = $channel; "ts" = $ts}
Invoke-RestMethod -Method "Get" -Uri $uri -ContentType "Application/Json" -body $params
# todo : Error handling
}
Export-ModuleMember -Function Get-SlackChannelHistory
Export-ModuleMember -Function Set-SlackToken
Export-ModuleMember -Function Get-SlackChannels
Export-ModuleMember -Function Remove-SlackMessage