Documentation
¶
Overview ¶
Package twitchwh is a library for interacting with Twitch EventSub over the Webhook transport. It allows you to assign event handlers to specific events.
To get started, create a new client using the New function. Then, assign an event handler using the On<EventType> fields. Finally, setup the HTTP handler for your application using the Handler function.
Index ¶
- type Client
- func (c *Client) AddSubscription(Type string, version string, condition Condition) error
- func (c *Client) GetSubscriptions() (subscriptions []Subscription, err error)
- func (c *Client) GetSubscriptionsByStatus(status string) (subscriptions []Subscription, err error)
- func (c *Client) GetSubscriptionsByType(Type string) (subscriptions []Subscription, err error)
- func (c *Client) Handler(w http.ResponseWriter, r *http.Request)
- func (c *Client) On(event string, handler func(json.RawMessage))
- func (c *Client) RemoveSubscription(id string) error
- func (c *Client) RemoveSubscriptionByType(Type string, condition Condition) error
- type ClientConfig
- type Condition
- type DuplicateSubscriptionError
- type InternalError
- type Subscription
- type SubscriptionNotFoundError
- type UnauthorizedError
- type UnhandledStatusError
- type VerificationTimeoutError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// Fired whenever a subscription is revoked.
// Check Subscription.Status for the reason.
OnRevocation func(Subscription)
// contains filtered or unexported fields
}
func (*Client) AddSubscription ¶
AddSubscription attemps to create a new subscription based on the type, version, and condition. You can find all subscription types, versions, and conditions at: EventSub subscription types. It will block until Twitch sends the verification request, or timeout after 10 seconds.
!! AddSubscription should only be called AFTER twitchwh.Client.Handler is set up accordingly. !!
// Setup the HTTP event handler
http.HandleFunc("/eventsub", client.Handler)
go http.ListenAndServe(":8080", nil)
_ := client.AddSubscription("stream.online", "1", twitchwh.Condition{
BroadcasterUserID: "215185844",
})
func (*Client) GetSubscriptions ¶
func (c *Client) GetSubscriptions() (subscriptions []Subscription, err error)
GetSubscriptions retrieves all subscriptions, including revoked ones. Automatically handles pagination.
Returns subscriptions and an error (if any).
func (*Client) GetSubscriptionsByStatus ¶
func (c *Client) GetSubscriptionsByStatus(status string) (subscriptions []Subscription, err error)
Get all subscriptions with the provided status. For a list of all status types see: https://dev.twitch.tv/docs/api/reference/#get-eventsub-subscriptions . Automatically handles pagination.
Returns subscriptions and an error (if any).
func (*Client) GetSubscriptionsByType ¶
func (c *Client) GetSubscriptionsByType(Type string) (subscriptions []Subscription, err error)
Get all subscriptions that match the provided type (eg. "stream.online"). Automatically handles pagination.
Returns subscriptions and an error (if any).
func (*Client) Handler ¶
func (c *Client) Handler(w http.ResponseWriter, r *http.Request)
Handler is the HTTP handler for requests from Twitch. It is up to you to assign this handler to the correct path according to your setup
client, _ := twitchwh.New(twitchwh.ClientConfig{
// ...
WebhookURL: "https://mydomain.com/eventsub",
})
http.HandleFunc("/eventsub", client.Handler)
http.ListenAndServe(":443", nil)
This example assumes https://mydomain.com is pointing to the Go app.
func (*Client) On ¶ added in v0.1.0
func (c *Client) On(event string, handler func(json.RawMessage))
Assign a handler to a particular event type. The handler takes a json.RawMessage that contains the event body. For a list of event types, see [https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/].
func (*Client) RemoveSubscription ¶
RemoveSubscription attempts to remove a subscription based on the ID. Returns SubscriptionNotFoundError if the subscription does not exist.
func (*Client) RemoveSubscriptionByType ¶ added in v0.0.2
RemoveSubscriptionByType attempts to remove a subscription based on the type and condition.
If no subscriptions are found, it will return nil.
Note: This will remove ALL subscriptions that match the provided type and condition.
type ClientConfig ¶
type ClientConfig struct {
// Client ID of your Twitch application
ClientID string
// Client Secret generated for your Twitch application. !! THIS IS NOT YOUR WEBHOOK SECRET !!
ClientSecret string
// Webhook secret used to verify events. This should be a random string between 10-100 characters
WebhookSecret string
// Full EventSub URL path, eg: https://mydomain.com/eventsub
WebhookURL string
// Log output
Debug bool
}
ClientConfig is used to configure a new Client
type Condition ¶
type Condition struct {
// broadcaster_user_id
BroadcasterUserID string `json:"broadcaster_user_id,omitempty"`
// moderator_user_id
ModeratorUserID string `json:"moderator_user_id,omitempty"`
// user_id
UserID string `json:"user_id,omitempty"`
// from_broadcaster_id
FromBroadcasterUserID string `json:"from_broadcaster_user_id,omitempty"`
// to_broadcaster_id
ToBroadcasterUserID string `json:"to_broadcaster_user_id,omitempty"`
// reward_id
//
// This should be int/string depending on subscription type
RewardID any `json:"reward_id,omitempty"`
// client_id
ClientID string `json:"client_id,omitempty"`
// extension_client_id
ExtensionClientID string `json:"extension_client_id,omitempty"`
// conduit_id
ConduitID string `json:"conduit_id,omitempty"`
// organization_id
OrganizationID string `json:"organization_id,omitempty"`
// category_id
CategoryID string `json:"category_id,omitempty"`
// campaign_id
CampaignID string `json:"campaign_id,omitempty"`
}
Condition for subscription. Empty values will be omitted. Fill out the options applicable to your subscription type
type DuplicateSubscriptionError ¶ added in v0.0.3
Attempted to add a subscription with a type and condition that already exists.
The Condition and Type fields are included in the error.
func (*DuplicateSubscriptionError) Error ¶ added in v0.0.3
func (e *DuplicateSubscriptionError) Error() string
type InternalError ¶ added in v0.0.3
type InternalError struct {
// Original error that caused this error, if any.
OriginalError error
// contains filtered or unexported fields
}
Returned for misc errors, like network or serialization errors for example.
func (*InternalError) Error ¶ added in v0.0.3
func (e *InternalError) Error() string
type Subscription ¶
type Subscription struct {
ID string `json:"id"`
Status string `json:"status"`
Type string `json:"type"`
Version string `json:"version"`
Cost int `json:"cost"`
// PLEASE NOTE that this will DEFAULT all unused conditions. Check the Type and get the correct condition for that type.
Condition Condition `json:"condition"`
Transport struct {
Method string `json:"method"`
Callback string `json:"callback"`
} `json:"transport"`
CreatedAt time.Time `json:"created_at"`
}
type SubscriptionNotFoundError ¶ added in v0.0.3
type SubscriptionNotFoundError struct{}
Could not find a subscription with the specified parameters.
func (*SubscriptionNotFoundError) Error ¶ added in v0.0.3
func (e *SubscriptionNotFoundError) Error() string
type UnauthorizedError ¶ added in v0.0.3
type UnauthorizedError struct{}
Helix returned an authorization error. This usually means the token, Client-ID, or client secret are invalid.
func (*UnauthorizedError) Error ¶ added in v0.0.3
func (e *UnauthorizedError) Error() string
type UnhandledStatusError ¶ added in v0.0.3
Helix returned an unexpected HTTP status code that is not handled by TwitchWH.
func (*UnhandledStatusError) Error ¶ added in v0.0.3
func (e *UnhandledStatusError) Error() string
type VerificationTimeoutError ¶ added in v0.0.3
type VerificationTimeoutError struct {
Subscription Subscription
}
Returned whenever AddSubscription times out waiting for verification confirmation.
func (*VerificationTimeoutError) Error ¶ added in v0.0.3
func (e *VerificationTimeoutError) Error() string