-
Notifications
You must be signed in to change notification settings - Fork 3
/
eventsub.go
72 lines (60 loc) · 2.69 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
package helixclient
import (
"time"
"github.com/gempir/gempbot/internal/log"
"github.com/nicklaw5/helix/v2"
)
func (c *HelixClient) CreateEventSubSubscription(userID string, webHookUrl string, subType string) (*helix.EventSubSubscriptionsResponse, error) {
c.Client.SetAppAccessToken(c.AppAccessToken.AccessToken)
c.Client.SetUserAccessToken("")
// Twitch doesn't need a user token here, always an app token eventhough the user has to authenticate beforehand.
// Internally they check if the app token has authenticated users
response, err := c.Client.CreateEventSubSubscription(
&helix.EventSubSubscription{
Condition: helix.EventSubCondition{BroadcasterUserID: userID},
Transport: helix.EventSubTransport{Method: "webhook", Callback: webHookUrl, Secret: c.eventSubSecret},
Type: subType,
Version: "1",
},
)
return response, err
}
func (c *HelixClient) CreateRewardEventSubSubscription(userID, webHookUrl, subType, rewardID string, retry bool) (*helix.EventSubSubscriptionsResponse, error) {
c.Client.SetAppAccessToken(c.AppAccessToken.AccessToken)
c.Client.SetUserAccessToken("")
// Twitch doesn't need a user token here, always an app token eventhough the user has to authenticate beforehand.
// Internally they check if the app token has authenticated users
response, err := c.Client.CreateEventSubSubscription(
&helix.EventSubSubscription{
Condition: helix.EventSubCondition{BroadcasterUserID: userID, RewardID: rewardID},
Transport: helix.EventSubTransport{Method: "webhook", Callback: webHookUrl, Secret: c.eventSubSecret},
Type: subType,
Version: "1",
},
)
return response, err
}
func (c *HelixClient) GetAllSubscriptions(eventType string) []helix.EventSubSubscription {
subs := []helix.EventSubSubscription{}
resp, err := c.Client.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{Type: eventType})
if err != nil {
log.Error(err)
}
subs = append(subs, resp.Data.EventSubSubscriptions...)
for resp.Data.Pagination.Cursor != "" {
log.Infof("Getting next subscriptions after %s", resp.Data.Pagination.Cursor)
time.Sleep(time.Second * 5)
resp, err = c.Client.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{Type: eventType, After: resp.Data.Pagination.Cursor})
if err != nil {
log.Error(err)
}
subs = append(subs, resp.Data.EventSubSubscriptions...)
}
return subs
}
func (c *HelixClient) RemoveEventSubSubscription(id string) (*helix.RemoveEventSubSubscriptionParamsResponse, error) {
return c.Client.RemoveEventSubSubscription(id)
}
func (c *HelixClient) GetEventSubSubscriptions(params *helix.EventSubSubscriptionsParams) (*helix.EventSubSubscriptionsResponse, error) {
return c.Client.GetEventSubSubscriptions(params)
}