Skip to content

Commit

Permalink
twitch/eventsub: add stream.online and stream.offline payload
Browse files Browse the repository at this point in the history
For #58.
  • Loading branch information
zephyrtronium committed Sep 6, 2024
1 parent 06f344e commit 586a585
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 0 deletions.
18 changes: 18 additions & 0 deletions twitch/eventsub/stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package eventsub

// StreamOnline is the payload for a stream.online or stream.offline notification.
type Stream struct {
// ID is the stream ID.
// Only present in stream.online messages.
ID string `json:"id"`
// Broadcaster is the broadcaster's user ID.
Broadcaster string `json:"broadcaster_user_id"`
// BroadcasterLogin is the broadcaster's user login.
BroadcasterLogin string `json:"broadcaster_user_login"`
// BoradcasterName is the broadcaster's display name.
BroadcasterName string `json:"broadcaster_user_name"`
// Type is the stream type, usually "live".
Type string `json:"type"`
// Started is the time at which the stream started in RFC3339Nano format.
Started string `json:"started_at"`
}
45 changes: 45 additions & 0 deletions twitch/eventsub/stream_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package eventsub_test

import (
"testing"

"github.com/go-json-experiment/json"
"github.com/google/go-cmp/cmp"
"github.com/zephyrtronium/robot/twitch/eventsub"
)

func TestStream(t *testing.T) {
t.Run("online", func(t *testing.T) {
evt := Testdata("stream.online.json")
var got eventsub.Stream
if err := json.Unmarshal([]byte(evt.Event), &got); err != nil {
t.Errorf("couldn't unmarshal payload as stream: %v", err)
}
want := eventsub.Stream{
ID: "9001",
Broadcaster: "1337",
BroadcasterLogin: "cool_user",
BroadcasterName: "Cool_User",
Type: "live",
Started: "2020-10-11T10:11:12.123Z",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong stream (+got/-want):\n%s", diff)
}
})
t.Run("offline", func(t *testing.T) {
evt := Testdata("stream.offline.json")
var got eventsub.Stream
if err := json.Unmarshal([]byte(evt.Event), &got); err != nil {
t.Errorf("couldn't unmarshal payload as stream: %v", err)
}
want := eventsub.Stream{
Broadcaster: "1337",
BroadcasterLogin: "cool_user",
BroadcasterName: "Cool_User",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong stream (+got/-want):\n%s", diff)
}
})
}
22 changes: 22 additions & 0 deletions twitch/eventsub/testdata/stream.offline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"subscription": {
"id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
"type": "stream.offline",
"version": "1",
"status": "enabled",
"cost": 0,
"condition": {
"broadcaster_user_id": "1337"
},
"created_at": "2019-11-16T10:11:12.634234626Z",
"transport": {
"method": "webhook",
"callback": "https://example.com/webhooks/callback"
}
},
"event": {
"broadcaster_user_id": "1337",
"broadcaster_user_login": "cool_user",
"broadcaster_user_name": "Cool_User"
}
}
25 changes: 25 additions & 0 deletions twitch/eventsub/testdata/stream.online.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"subscription": {
"id": "f1c2a387-161a-49f9-a165-0f21d7a4e1c4",
"type": "stream.online",
"version": "1",
"status": "enabled",
"cost": 0,
"condition": {
"broadcaster_user_id": "1337"
},
"transport": {
"method": "webhook",
"callback": "https://example.com/webhooks/callback"
},
"created_at": "2019-11-16T10:11:12.634234626Z"
},
"event": {
"id": "9001",
"broadcaster_user_id": "1337",
"broadcaster_user_login": "cool_user",
"broadcaster_user_name": "Cool_User",
"type": "live",
"started_at": "2020-10-11T10:11:12.123Z"
}
}
24 changes: 24 additions & 0 deletions twitch/eventsub/testdata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package eventsub_test

import (
"embed"

"github.com/go-json-experiment/json"

"github.com/zephyrtronium/robot/twitch/eventsub"
)

//go:embed testdata
var testdata embed.FS

func Testdata(name string) *eventsub.Event {
b, err := testdata.ReadFile("testdata/" + name)
if err != nil {
panic(err)
}
var r eventsub.Event
if err := json.Unmarshal(b, &r); err != nil {
panic(err)
}
return &r
}

0 comments on commit 586a585

Please sign in to comment.