-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslackbot.ps1
308 lines (265 loc) · 10.7 KB
/
slackbot.ps1
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
param(
[string]$year = $ENV:AOC_YEAR,
[string]$session = $ENV:AOC_SESSION_COOKIE,
[string]$leaderboard = $ENV:AOC_LEADERBOARD_ID,
[string]$live_refresh = $ENV:AOC_REFRESH_DATA, # default false
[string]$timeout_seconds = $ENV:AOC_REFRESH_RATE_SECONDS,
[string]$send_leaderboard_update = $ENV:AOC_SEND_LEADERBOARD_STATE, # default true
[string]$webhook = $ENV:SLACK_WEBHOOK,
[string]$email = $ENV:EMAIL,
[string]$send_slack_message = $ENV:SLACK_SEND_MESSAGE, # default false
[string]$debug = $ENV:SCRIPT_DEBUG # default false
)
$ErrorActionPreference = "Stop"
Import-Module (Join-Path $PSScriptRoot "Logging.psm1") -Force
#region Parameter validation
if ([string]::IsNullOrEmpty($live_refresh)) {
$live_refresh = "false"
}
if ([string]::IsNullOrEmpty($timeout_seconds)) {
$timeout_seconds = "900" # 15 minutes, the minimum refresh rate
}
if ([string]::IsNullOrEmpty($debug)) {
[bool]$debug = $true
}
else {
[bool]$debug = [bool]::Parse($debug)
}
if ([string]::IsNullOrEmpty($send_slack_message)) {
[bool]$send_slack_message = $false
}
else {
[bool]$send_slack_message = [bool]::Parse($send_slack_message)
}
if ([string]::IsNullOrEmpty($send_leaderboard_update)) {
[bool]$send_leaderboard_update = $true
}
else {
[bool]$send_leaderboard_update = [bool]::Parse($send_leaderboard_update)
}
if ([string]::IsNullOrEmpty($year)) {
$year = (Get-Date).Year
}
Write-Info ""
Write-Info "leaderboard: $leaderboard"
Write-Info "year: $year"
Write-Info "live_refresh: $live_refresh"
Write-Info "timeout_seconds: $timeout_seconds"
Write-Info "send_slack_message: $send_slack_message"
if ($debug) {
Write-Info "session: $session"
Write-Info "webhook: $webhook"
}
Write-Info ""
#endregion
#region Functions
function Format-TimeSpan($Duration){
# http://stackoverflow.com/questions/61431951/ddg#61432373
$Day = switch ($Duration.Days) {
0 { $null; break }
1 { "{0} Day," -f $Duration.Days; break }
Default {"{0} Days," -f $Duration.Days}
}
$Hour = switch ($Duration.Hours) {
#0 { $null; break }
1 { "{0} Hour," -f $Duration.Hours; break }
Default { "{0} Hours," -f $Duration.Hours }
}
$Minute = switch ($Duration.Minutes) {
#0 { $null; break }
1 { "{0} Minute," -f $Duration.Minutes; break }
Default { "{0} Minutes," -f $Duration.Minutes }
}
$Second = switch ($Duration.Seconds) {
#0 { $null; break }
1 { "{0} Second" -f $Duration.Seconds; break }
Default { "{0} Seconds" -f $Duration.Seconds }
}
return "$Day $Hour $Minute $Second"
}
function Add-Property($Object, $Name, $Value) {
$Object | Add-Member -MemberType NoteProperty -Name $Name -Value $Value -Force
}
function Get-ObjectKeys($Object) {
return $Object.PSObject.Properties.name
}
function Get-ObjectValues($Object) {
return $Object.PSObject.Properties.value
}
function Get-PaddedLength($Values) {
return $Values | ForEach-Object { "$_".Length } | Sort-Object -Descending | Select-Object -First 1
}
function Get-Participant($Participants, $Id) {
return $Participants | Where-Object { $_.id -eq $Id } | Select-Object -First 1
}
function Get-Leaderboard($CallApi, $Year, $Session, $LeaderboardId) {
if ($CallApi) {
Write-Info "Refreshing data..."
$url = "https://adventofcode.com/$Year/leaderboard/private/view/$LeaderboardId.json"
$response = Invoke-WebRequest -Method Get -Uri $url -UserAgent "PowerShell Slack-Integration ($email) (https://github.com/maartengo/adventofcode_slack_bot)" -Headers @{ "Cookie" = "session=$Session" }
Set-Content -Value $response.Content -Path .\result.json -Encoding utf8BOM
}
else {
Write-Info "Using cached data..."
}
return Get-Content .\result.json -Encoding utf8BOM | ConvertFrom-Json
}
function Get-ScoreBoard() {
$scoreboardMessage = "Current Scores:`n```````n"
foreach ($participant in $participants) {
$index = $participant.position
$paddedIndex = "$index".PadRight($indexPadding)
$paddedName = $participant.name.PadRight($namePadding)
$paddedScore = "$($participant.local_score)".PadRight($scorePadding)
if ($null -ne $specialScores."$index") {
$paddedIndex = $specialScores."$index".PadRight($indexPadding)
}
$scoreboardMessage += "$paddedIndex) $paddedName $paddedScore ⭐ x $($participant.stars)`n"
}
$scoreboardMessage += "``````"
return $scoreboardMessage
}
function Send-SlackMessage($message) {
Write-Info "Sending slack message:"
Write-Info $message
if ($send_slack_message) {
$body = @{ "text" = $message } | ConvertTo-Json
$response = Invoke-WebRequest -Method Post -Uri $webhook -ContentType "application/json; charset=UTF-8" -Body $body
if ($response.StatusCode -ne 200) {
Write-Error "Failed to send slack message:"
$response
}
}
}
#endregion
$codeblock = "``````"
$live = [bool]::Parse($live_refresh)
$timeout = [int]::Parse($timeout_seconds)
$timeout = [math]::max(900, $timeout) # 15 minutes, the minimum refresh rate
$leaderboardTimes = @('5:00', '11:01') # post at 5:45 and 12:00 CET
# $leaderboardTimes = @('11:01')
$timeoutInMinutes = [math]::round($timeout / 60)
# Wait until AOC starts
$utcNow = (Get-Date).ToUniversalTime()
if($year -eq $utcNow.Year -and $utcNow.Month -ne 12) {
$startOfAOC = (Get-Date -Date "$year-12-01 05:00:00Z").ToUniversalTime()
$secondsUntilDecember = ($startOfAOC - $utcNow).TotalSeconds
Write-Info "Sleeping $(Format-TimeSpan ($startOfAOC - $utcNow)) until 1 December $year..."
while($secondsUntilDecember -gt 0) {
Start-Sleep -Seconds ([math]::min($secondsUntilDecember, 2147483))
$secondsUntilDecember = $secondsUntilDecember - 2147483
}
}
$minutesToWait = $timeoutInMinutes - ((Get-Date).minute % $timeoutInMinutes)
if ($timeoutInMinutes -gt $minutesToWait) {
Write-Info "Waiting $minutesToWait minutes so we start at a rounded number of minutes"
Start-Sleep ($minutesToWait * 60 - (Get-Date).second)
Write-Info "Continue..."
}
[datetime]$epoch = '1970-01-01 00:00:00'
$specialScores = @{
"1" = "🥇"
"2" = "🥈"
"3" = "🥉"
}
$init = $true
while ($true) {
$previous = @{
content = $content
participants = $participants
members = $members
date = $content.date
}
$content = Get-Leaderboard -CallApi $live -Year $year -Session $session -LeaderboardId $leaderboard
$members = Get-ObjectValues $content.members | Sort-Object -Descending -Property local_score,last_star_ts
$participants = $members | Where-Object { $_.local_score -gt 0 }
$indexPadding = [math]::max(2, "$($participants.Length)".Length)
$namePadding = Get-PaddedLength $participants.name
$scorePadding = Get-PaddedLength $participants.local_score
$i = 1
foreach ($member in $members) {
Add-Property -Object $member -Name "position" -Value $i
$i++
}
Add-Property -Object $content -Name "date" -Value (Get-Date)
Set-Content -Value ($content | ConvertTo-Json -Depth 20) -Path .\result.json -Encoding utf8BOM
if ($init) {
$init = $false
$previous = @{
content = $content
participants = $participants
members = $members
date = $content.date
}
}
if ($send_leaderboard_update) {
$now = Get-Date
$lastStarGained = $participants | Sort-Object -Property last_star_ts -Descending | Select-Object -First 1 -ExpandProperty last_star_ts
if($null -eq $lastStarGained) {
$lastStarGained = 0
}
$lastStarGainedTime = $epoch.AddSeconds($lastStarGained).ToLocalTime()
$times = $leaderboardTimes | Foreach-Object { [datetime]::parse($_).ToLocalTime() } | Sort-Object
for($i = 0; $i -lt $times.Length; $i++) {
$leaderBoardRefresh = $times[$i]
if ($now -le $leaderBoardRefresh -and ($now.AddSeconds($timeout) -ge $leaderBoardRefresh)) {
$previousTime = $times[($i - 1) % $times.Length]
if($previousTime -gt $now) { $previousTime.AddDays(-1) }
if($lastStarGainedTime -gt $previousTime) {
Send-SlackMessage (Get-ScoreBoard)
}
break
}
}
}
# $gainedStars = @(@{ name = "", day = 1, part = 1, time = 10:32 })
$gainedStars = @()
if ($debug) {
# $previous.participants = $null
}
foreach ($participant in $participants) {
$previousParticipant = Get-Participant -Participants ($previous.participants) -Id $participant.id
if ($null -ne $previousParticipant -and ($participant.stars - $previousParticipant.stars) -eq 0) {
continue;
}
foreach ($day in (Get-ObjectKeys $participant.completion_day_level)) {
$previousDay = $null
if ($null -ne $previousParticipant) {
$previousDay = $previousParticipant.completion_day_level.$day
}
foreach ($part in (Get-ObjectKeys $participant.completion_day_level.$day)) {
if ($null -eq $previousDay -or $null -eq $previousDay.$part) {
$gainedStars += @{
name = $participant.name
day = $day
part = $part
time = $participant.completion_day_level.$day.$part.get_star_ts
}
}
}
}
}
if ($gainedStars.Length -gt 0) {
$message = [System.Collections.ArrayList]::new()
$gainedStars | Sort-Object -Property time | ForEach-Object {
$gained = $_
$paddedName = $gained.name.PadRight($namePadding)
$paddedDay = "$($gained.day)".PadRight(2)
$paddedPart = $gained.part -eq "1" ? "1️⃣" : "2️⃣"
$paddedTime = "{0:HH:mm:ss}" -f $epoch.AddSeconds($gained.time).ToLocalTime()
$message.Add("$paddedName won a ⭐ for day $paddedDay part $paddedPart at $paddedTime 🎉")
}
# Messages can only be 55 lines long
for ($i = 0; $i * 50 -lt $message.Count; $i++) {
$text = $message | Select-Object -Skip ($i * 50) -First 50
$text = $text -join "`n"
$text = "$codeblock`n$text`n$codeblock"
Send-SlackMessage $text
Start-Sleep -Seconds 0.5
}
}
[System.gc]::Collect()
$interval = ($timeout - (Get-Date).Second % $timeout)
Write-Info "Sleeping for $timeout seconds... next check at $("{0:yyyy/MM/dd} {0:HH:mm:ss}" -f (Get-Date).AddSeconds($interval).ToLocalTime())"
Start-Sleep -Seconds $interval
}