GoLang API Client for Twitch API, EventSub, and WebSockets
- Go 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| .forgejo/workflows | ||
| .gitignore | ||
| events.go | ||
| go.mod | ||
| go.sum | ||
| helix.go | ||
| helix_chat.go | ||
| helix_eventsub.go | ||
| helix_stream.go | ||
| helix_types.go | ||
| helix_user.go | ||
| helix_video.go | ||
| http.go | ||
| LICENSE | ||
| oauth.go | ||
| README.md | ||
| subscription.go | ||
| subscription_types.go | ||
| twitch.go | ||
| twitch_test.go | ||
| webhook.go | ||
| webhook_test.go | ||
| websocket_api.go | ||
| websocket_event.go | ||
| websocket_types.go | ||
gotwitch
gotwitch is a Go client library for the Twitch API. It covers the Helix REST API and the EventSub websocket for building chat bots and stream tools.
Installation
go get vc.maxkaya.com/sdk/go-twitch
Quick Start
client := gotwitch.TwitchClient{
ClientID: os.Getenv("TWITCH_CLIENT_ID"), // Specific app Client ID; for app token
ClientSecret: os.Getenv("TWITCH_CLIENT_SECRET"), // Same as above
BotUsername: os.Getenv("TWITCH_BOT_USERNAME"),
BotAccessToken: os.Getenv("TWITCH_BOT_ACCESS_TOKEN"),
Channels: []string{"channelname"},
}
session, err := gotwitch.New(client)
if err != nil {
log.Fatal(err)
}
session.RegisterEventListener("channel.chat.message", func(e gotwitch.WebsocketMessage) {
fmt.Println(e.Payload.Event.Message.Text)
})
if err := session.Connect(); err != nil {
log.Fatal(err)
}
Websocket Subscriptions
Events arrive over the EventSub websocket after Connect establishes a session. Create a subscription for every channel in the TwitchClient Channels field.
session.CreateSubscription(gotwitch.SubscriptionTypeChannelChatMessage, gotwitch.Transport{Method: "websocket"})
Webhook Subscriptions
Events arrive as HTTP requests to a public callback URL. Webhook subscriptions need a callback URL reachable by Twitch over HTTPS and a shared secret.
session.CreateSubscription(gotwitch.SubscriptionTypeChannelFollow, gotwitch.Transport{Method: "webhook", Callback: "https://example.com/twitch", Secret: "s3cret"})
Serve the callback from your own HTTP server. NewWebhookHandler answers Twitch verification requests and only forwards signed notifications to your handler.
mux := http.NewServeMux()
mux.Handle("/twitch/events", gotwitch.NewWebhookHandler("s3cret", func(rw http.ResponseWriter, req *http.Request, message gotwitch.WebsocketMessage) {
fmt.Println(message.Metadata.SubscriptionType)
}))
http.ListenAndServe(":8080", mux)
Documentation
Full API reference with examples lives in the godoc comments. Browse with go doc gotwitch or pkg.go.dev.
License
MIT