diff --git a/twitch/eventsub/stream.go b/twitch/eventsub/stream.go new file mode 100644 index 0000000..55a9775 --- /dev/null +++ b/twitch/eventsub/stream.go @@ -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"` +} diff --git a/twitch/eventsub/stream_test.go b/twitch/eventsub/stream_test.go new file mode 100644 index 0000000..edad2b0 --- /dev/null +++ b/twitch/eventsub/stream_test.go @@ -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) + } + }) +} diff --git a/twitch/eventsub/testdata/stream.offline.json b/twitch/eventsub/testdata/stream.offline.json new file mode 100644 index 0000000..bc17c7c --- /dev/null +++ b/twitch/eventsub/testdata/stream.offline.json @@ -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" + } +} \ No newline at end of file diff --git a/twitch/eventsub/testdata/stream.online.json b/twitch/eventsub/testdata/stream.online.json new file mode 100644 index 0000000..55b14a5 --- /dev/null +++ b/twitch/eventsub/testdata/stream.online.json @@ -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" + } +} \ No newline at end of file diff --git a/twitch/eventsub/testdata_test.go b/twitch/eventsub/testdata_test.go new file mode 100644 index 0000000..13816f0 --- /dev/null +++ b/twitch/eventsub/testdata_test.go @@ -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 +}