forked from nicklaw5/helix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventsub.go
593 lines (529 loc) · 26.6 KB
/
eventsub.go
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
package helix
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"net/url"
"strings"
)
// EventSub Types for Parsing Requests / Responses
// Represents a subscription
type EventSubSubscription struct {
ID string `json:"id"`
Type string `json:"type"`
Version string `json:"version"`
Status string `json:"status"`
Condition EventSubCondition `json:"condition"`
Transport EventSubTransport `json:"transport"`
CreatedAt Time `json:"created_at"`
Cost int `json:"cost"`
}
// Conditions for a subscription, not all are necessary and some only apply to some subscription types, see https://dev.twitch.tv/docs/eventsub/eventsub-reference
type EventSubCondition struct {
BroadcasterUserID string `json:"broadcaster_user_id"`
FromBroadcasterUserID string `json:"from_broadcaster_user_id"`
ToBroadcasterUserID string `json:"to_broadcaster_user_id"`
RewardID string `json:"reward_id"`
ClientID string `json:"client_id"`
ExtensionClientID string `json:"extension_client_id"`
UserID string `json:"user_id"`
}
// Transport for the subscription, currently the only supported Method is "webhook". Secret must be between 10 and 100 characters
type EventSubTransport struct {
Method string `json:"method"`
Callback string `json:"callback"`
Secret string `json:"secret"`
}
// Twitch Response for getting all current subscriptions
type ManyEventSubSubscriptions struct {
TotalCost int `json:"total_cost"`
MaxTotalCost int `json:"max_total_cost"`
EventSubSubscriptions []EventSubSubscription `json:"data"`
Pagination Pagination `json:"pagination"`
}
// Response for getting all current subscriptions
type EventSubSubscriptionsResponse struct {
ResponseCommon
Data ManyEventSubSubscriptions
}
// Parameter for filtering subscriptions, currently only the status is filterable
type EventSubSubscriptionsParams struct {
Status string `query:"status"`
Type string `query:"type"`
After string `query:"after"`
}
// Parameter for removing a subscription.
type RemoveEventSubSubscriptionParams struct {
ID string `query:"id"`
}
// Response for removing a subscription
type RemoveEventSubSubscriptionParamsResponse struct {
ResponseCommon
}
// EventSub helper Variables for Types and Status
const (
EventSubStatusEnabled = "enabled"
EventSubStatusPending = "webhook_callback_verification_pending"
EventSubStatusFailed = "webhook_callback_verification_failed"
EventSubStatusNotificationFailuresExceeded = "notification_failures_exceeded"
EventSubStatusAuthorizationRevoked = "authorization_revoked"
EventSubStatusUserRemoved = "user_removed"
EventSubTypeChannelUpdate = "channel.update"
EventSubTypeChannelFollow = "channel.follow"
EventSubTypeChannelSubscription = "channel.subscribe"
EventSubTypeChannelSubscriptionEnd = "channel.subscription.end"
EventSubTypeChannelSubscriptionGift = "channel.subscription.gift"
EventSubTypeChannelSubscriptionMessage = "channel.subscription.message"
EventSubTypeChannelCheer = "channel.cheer"
EventSubTypeChannelRaid = "channel.raid"
EventSubTypeChannelBan = "channel.ban"
EventSubTypeChannelUnban = "channel.unban"
EventSubTypeModeratorAdd = "channel.moderator.add"
EventSubTypeModeratorRemove = "channel.moderator.remove"
EventSubTypeChannelPointsCustomRewardAdd = "channel.channel_points_custom_reward.add"
EventSubTypeChannelPointsCustomRewardUpdate = "channel.channel_points_custom_reward.update"
EventSubTypeChannelPointsCustomRewardRemove = "channel.channel_points_custom_reward.remove"
EventSubTypeChannelPointsCustomRewardRedemptionAdd = "channel.channel_points_custom_reward_redemption.add"
EventSubTypeChannelPointsCustomRewardRedemptionUpdate = "channel.channel_points_custom_reward_redemption.update"
EventSubTypeChannelPollBegin = "channel.poll.begin"
EventSubTypeChannelPollProgress = "channel.poll.progress"
EventSubTypeChannelPollEnd = "channel.poll.end"
EventSubTypeChannelPredictionBegin = "channel.prediction.begin"
EventSubTypeChannelPredictionProgress = "channel.prediction.progress"
EventSubTypeChannelPredictionLock = "channel.prediction.lock"
EventSubTypeChannelPredictionEnd = "channel.prediction.end"
EventSubExtensionBitsTransactionCreate = "extension.bits_transaction.create"
EventSubTypeHypeTrainBegin = "channel.hype_train.begin"
EventSubTypeHypeTrainProgress = "channel.hype_train.progress"
EventSubTypeHypeTrainEnd = "channel.hype_train.end"
EventSubTypeStreamOnline = "stream.online"
EventSubTypeStreamOffline = "stream.offline"
EventSubTypeUserAuthorizationRevoke = "user.authorization.revoke"
EventSubTypeUserUpdate = "user.update"
)
// Event Notification Responses
// Data for a channel ban notification
type EventSubChannelBanEvent struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
ModeratorUserID string `json:"moderator_user_id"`
ModeratorUserLogin string `json:"moderator_user_login"`
ModeratorUserName string `json:"moderator_user_name"`
Reason string `json:"reason"`
EndsAt Time `json:"ends_at"`
IsPermanent bool `json:"is_permanent"`
}
// Data for a channel subscribe notification
type EventSubChannelSubscribeEvent struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Tier string `json:"tier"`
IsGift bool `json:"is_gift"`
}
// EventSubChannelSubscriptionGiftEvent
type EventSubChannelSubscriptionGiftEvent struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Total int `json:"total"`
Tier string `json:"tier"`
CumulativeTotal int `json:"cumulative_total"`
IsAnonymous bool `json:"is_anonymous"`
}
// EventSubChannelSubscriptionMessageEvent
type EventSubChannelSubscriptionMessageEvent struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Tier string `json:"tier"`
Message EventSubMessage `json:"message"`
CumulativeTotal int `json:"cumulative_total"`
StreakMonths int `json:"streak_months"`
DurationMonths int `json:"duration_months"`
}
// Data for a channel cheer notification
type EventSubChannelCheerEvent struct {
IsAnonymous bool `json:"is_anonymous"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Message string `json:"message"`
Bits int `json:"bits"`
}
// Data for a channel update notification
type EventSubChannelUpdateEvent struct {
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Title string `json:"title"`
Language string `json:"language"`
CategoryID string `json:"category_id"`
CategoryName string `json:"category_name"`
IsMature bool `json:"is_mature"`
}
// Data for a channel unban notification
type EventSubChannelUnbanEvent struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
ModeratorUserID string `json:"moderator_user_id"`
ModeratorUserLogin string `json:"moderator_user_login"`
ModeratorUserName string `json:"moderator_user_name"`
}
// Data for a channel follow notification
type EventSubChannelFollowEvent struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
}
// Data for a channel moderator add notification, it's the same as the channel follow notification
type EventSubModeratorAddEvent = EventSubChannelFollowEvent
// Data for a channel moderator remove notification, it's the same as the channel follow notification
type EventSubModeratorRemoveEvent = EventSubChannelFollowEvent
// Data for a channel raid notification
type EventSubChannelRaidEvent struct {
FromBroadcasterUserID string `json:"from_broadcaster_user_id"`
FromBroadcasterUserLogin string `json:"from_broadcaster_user_login"`
FromBroadcasterUserName string `json:"from_broadcaster_user_name"`
ToBroadcasterUserID string `json:"to_broadcaster_user_id"`
ToBroadcasterUserLogin string `json:"to_broadcaster_user_login"`
ToBroadcasterUserName string `json:"to_broadcaster_user_name"`
Viewers int `json:"viewers"`
}
// Data for a channel poll begin event
type EventSubChannelPollBeginEvent struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Title string `json:"title"`
Choices []PollChoice `json:"choices"`
BitsVoting EventSubBitVoting `json:"bits_voting"`
ChannelPointsVoting EventSubChannelPointsVoting `json:"channel_points_voting"`
StartedAt Time `json:"started_at"`
EndsAt Time `json:"ends_at"`
}
// Data for a channel poll progress event, it's the same as the channel poll begin event
type EventSubChannelPollProgressEvent = EventSubChannelPollBeginEvent
// Data for a channel poll end event
type EventSubChannelPollEndEvent struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Title string `json:"title"`
Choices []PollChoice `json:"choices"`
BitsVoting EventSubBitVoting `json:"bits_voting"`
ChannelPointsVoting EventSubChannelPointsVoting `json:"channel_points_voting"`
Status string `json:"status"`
StartedAt Time `json:"started_at"`
EndedAt Time `json:"ended_at"`
}
type EventSubBitVoting struct {
IsEnabled bool `json:"is_enabled"`
AmountPerVote int `json:"amount_per_vote"`
}
type EventSubChannelPointsVoting = EventSubBitVoting
// Data for a channel points custom reward notification
type EventSubChannelPointsCustomRewardEvent struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
IsEnabled bool `json:"is_enabled"`
IsPaused bool `json:"is_paused"`
IsInStock bool `json:"is_in_stock"`
Title string `json:"title"`
Cost int `json:"cost"`
Prompt string `json:"prompt"`
IsUserInputRequired bool `json:"is_user_input_required"`
ShouldRedemptionsSkipRequestQueue bool `json:"should_redemptions_skip_request_queue"`
MaxPerStream EventSubMaxPerStream `json:"max_per_stream"`
MaxPerUserPerStream EventSubMaxPerStream `json:"max_per_user_per_stream"`
BackgroundColor string `json:"background_color"`
Image EventSubImage `json:"image"`
DefaultImage EventSubImage `json:"default_image"`
GlobalCooldown EventSubGlobalCooldown `json:"global_cooldown"`
CooldownExpiresAt Time `json:"cooldown_expires_at"`
RedemptionsRedeemedCurrentStream int `json:"redemptions_redeemed_current_stream"`
}
// Data for a channel points custom reward redemption notification
type EventSubChannelPointsCustomRewardRedemptionEvent struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
UserInput string `json:"user_input"`
Status string `json:"status"`
Reward EventSubReward `json:"reward"`
RedeemedAt Time `json:"redeemed_at"`
}
// Data for a channel prediction begin event
type EventSubChannelPredictionBeginEvent struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Title string `json:"title"`
Outcomes []EventSubOutcome `json:"outcomes"`
StartedAt Time `json:"started_at"`
LockedAt Time `json:"locked_at"`
}
// Data for a channel prediction progress event
type EventSubChannelPredictionProgressEvent = EventSubChannelPredictionBeginEvent
// Data for a channel prediction lock event
type EventSubChannelPredictionLockEvent struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Title string `json:"title"`
WinningOutcomeID string `json:"winning_outcome_id"`
Outcomes []EventSubOutcome `json:"outcomes"`
Status string `json:"status"`
StartedAt Time `json:"started_at"`
LockedAt Time `json:"locked_at"`
}
// Data for a channel prediction end event
type EventSubChannelPredictionEndEvent struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Title string `json:"title"`
WinningOutcomeID string `json:"winning_outcome_id"`
Outcomes []EventSubOutcome `json:"outcomes"`
Status string `json:"status"`
StartedAt Time `json:"started_at"`
EndedAt Time `json:"eneded_at"`
}
// Data for an extension bits transaction creation
type EventSubExtensionBitsTransactionCreateEvent struct {
ExtensionClientID string `json:"extension_client_id"`
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
Product EventSubProduct `json:"product"`
}
// Data for a hype train begin notification
type EventSubHypeTrainBeginEvent struct {
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Total int `json:"total"`
Progress int `json:"progress"`
Goal int `json:"goal"`
TopContributions []EventSubContribution `json:"top_contributions"`
LastContribution EventSubContribution `json:"last_contribution"`
StartedAt Time `json:"started_at"`
ExpiresAt Time `json:"expires_at"`
}
// Data for a hype train progress notification
type EventSubHypeTrainProgressEvent struct {
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Level int `json:"level"`
Total int `json:"total"`
Progress int `json:"progress"`
Goal int `json:"goal"`
TopContributions []EventSubContribution `json:"top_contributions"`
LastContribution EventSubContribution `json:"last_contribution"`
StartedAt Time `json:"started_at"`
ExpiresAt Time `json:"expires_at"`
}
// Data for a hype train end notification
type EventSubHypeTrainEndEvent struct {
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Level int `json:"level"`
Total int `json:"total"`
TopContributions []EventSubContribution `json:"top_contributions"`
StartedAt Time `json:"started_at"`
ExpiresAt Time `json:"expires_at"`
CooldownEndsAt Time `json:"cooldown_ends_at"`
}
// Data for a stream online notification
type EventSubStreamOnlineEvent struct {
ID string `json:"id"`
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
Type string `json:"type"`
StartedAt Time `json:"started_at"`
}
// Data for a stream offline notification
type EventSubStreamOfflineEvent struct {
BroadcasterUserID string `json:"broadcaster_user_id"`
BroadcasterUserLogin string `json:"broadcaster_user_login"`
BroadcasterUserName string `json:"broadcaster_user_name"`
}
// Data for an user authentication revoke notification, this means the user has revoked the access token and if you need to comply with gdpr you need to delete your user data belonging to the user.
type EventSubUserAuthenticationRevokeEvent struct {
ClientID string `json:"client_id"`
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
}
// Data for an user update notification
type EventSubUserUpdateEvent struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
Email string `json:"email"`
Description string `json:"description"`
}
// This belongs to a custom reward and defines it's cooldown
type EventSubGlobalCooldown struct {
IsEnabled bool `json:"is_enabled"`
Seconds int `json:"seconds"`
}
// This also belongs to a custom reward and defines the image urls
type EventSubImage struct {
Url1x string `json:"url_1x"`
Url2x string `json:"url_2x"`
Url4x string `json:"url_4x"`
}
// This belongs to a hype train and defines a user contribution
type EventSubContribution struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
Type string `json:"type"`
Total int64 `json:"total"`
}
// This belong to an outcome and defines user reward
type EventSubTopPredictor struct {
UserID string `json:"user_id"`
UserLogin string `json:"user_login"`
UserName string `json:"user_name"`
ChannelPointWon string `json:"channel_points_won"`
ChannelPointsUsed int `json:"channel_points_used"`
}
// This belongs to a custom reward and defines if it is limited per stream
type EventSubMaxPerStream struct {
IsEnabled bool `json:"is_enabled"`
Value int `json:"value"`
}
// This belong to a channel prediction and defines the outcomes
type EventSubOutcome struct {
ID string `json:"id"`
Title string `json:"title"`
Color string `json:"color"`
Users int `json:"users"`
ChannelPoints int `json:"channel_points"`
TopPredictors []EventSubTopPredictor `json:"top_predictors"`
}
type EventSubProduct struct {
Name string `json:"name"`
Bits int `json:"bots"`
Sku string `json:"sku"`
InDevelopment bool `json:"in_development"`
}
// This belongs to a reward redemption and defines the reward redeemed
type EventSubReward struct {
ID string `json:"id"`
Title string `json:"title"`
Cost int `json:"cost"`
Prompt string `json:"prompt"`
}
// EventSubMessage
type EventSubMessage struct {
Text string `json:"text"`
Emotes []EventSubEmote `json:"emotes"`
}
// EventSubEmote
type EventSubEmote struct {
Begin int `json:"begin"`
End int `json:"end"`
ID string `json:"id"`
}
// Get all EventSub Subscriptions
func (c *Client) GetEventSubSubscriptions(params *EventSubSubscriptionsParams) (*EventSubSubscriptionsResponse, error) {
resp, err := c.get("/eventsub/subscriptions", &ManyEventSubSubscriptions{}, params)
if err != nil {
return nil, err
}
eventSubs := &EventSubSubscriptionsResponse{}
resp.HydrateResponseCommon(&eventSubs.ResponseCommon)
eventSubs.Data.TotalCost = resp.Data.(*ManyEventSubSubscriptions).TotalCost
eventSubs.Data.MaxTotalCost = resp.Data.(*ManyEventSubSubscriptions).MaxTotalCost
eventSubs.Data.EventSubSubscriptions = resp.Data.(*ManyEventSubSubscriptions).EventSubSubscriptions
eventSubs.Data.Pagination = resp.Data.(*ManyEventSubSubscriptions).Pagination
return eventSubs, nil
}
// Remove an EventSub Subscription
func (c *Client) RemoveEventSubSubscription(id string) (*RemoveEventSubSubscriptionParamsResponse, error) {
resp, err := c.delete("/eventsub/subscriptions", nil, &RemoveEventSubSubscriptionParams{ID: id})
if err != nil {
return nil, err
}
eventsub := &RemoveEventSubSubscriptionParamsResponse{}
resp.HydrateResponseCommon(&eventsub.ResponseCommon)
return eventsub, nil
}
// Creates an EventSub subscription
func (c *Client) CreateEventSubSubscription(payload *EventSubSubscription) (*EventSubSubscriptionsResponse, error) {
if payload.Transport.Method == "webhook" && !strings.HasPrefix(payload.Transport.Callback, "https://") {
return nil, fmt.Errorf("error: callback must use https")
}
if payload.Transport.Secret != "" && (len(payload.Transport.Secret) < 10 || len(payload.Transport.Secret) > 100) {
return nil, fmt.Errorf("error: secret must be between 10 and 100 characters")
}
callbackUrl, err := url.Parse(payload.Transport.Callback)
if err != nil {
return nil, err
}
if callbackUrl.Port() != "" && callbackUrl.Port() != "443" {
return nil, fmt.Errorf("error: callback must use port 443")
}
resp, err := c.postAsJSON("/eventsub/subscriptions", &ManyEventSubSubscriptions{}, payload)
if err != nil {
return nil, err
}
eventsub := &EventSubSubscriptionsResponse{}
resp.HydrateResponseCommon(&eventsub.ResponseCommon)
eventsub.Data = *resp.Data.(*ManyEventSubSubscriptions)
return eventsub, nil
}
// Verifys that a notification came from twitch using the a signature and the secret used when creating the subscription
func VerifyEventSubNotification(secret string, header http.Header, message string) bool {
hmacMessage := []byte(fmt.Sprintf("%s%s%s", header.Get("Twitch-Eventsub-Message-Id"), header.Get("Twitch-Eventsub-Message-Timestamp"), message))
mac := hmac.New(sha256.New, []byte(secret))
mac.Write(hmacMessage)
hmacsha256 := fmt.Sprintf("sha256=%s", hex.EncodeToString(mac.Sum(nil)))
return hmacsha256 == header.Get("Twitch-Eventsub-Message-Signature")
}