twitchext

package module
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2020 License: MIT Imports: 12 Imported by: 0

README

go-twitch-ext

GoDoc Build Status Go Report Card codecov GitHub Release

A library to help with the development of an EBS for a Twitch Extension

For any help please consult FAQ section

Supported Endpoints & Features

Features:

Twitch JWT

  • Twitch Claims structure supported
  • Sign Twitch claims into JWT Tokens
  • Verify Client/EBS Created Twitch JWT tokens into claims obj

API Endpoint:

This package supports the following Twitch Extension API endpoints

  • Get Live Channels with Extension Activated
  • Create Extension Secret
  • Get Extension Secret
  • Revoke Extension Secrets
  • Set Extension Required Configuration
  • Set Extension Configuration Segment
  • Get Extension Channel Configuration
  • Get Extension Configuration Segment
  • Send Extension PubSub Message
  • Send Extension Chat Message

Installing

go get github.com/jackmcguire1/go-twitch-ext

Example

package main

import (
	"log"
	"os"

	twitch "github.com/jackmcguire1/go-twitch-ext"
)

var twitchPkg *twitch.Twitch

func init() {
	twitchPkg = twitch.NewClient(
		os.Getenv("OWNER_ID"),
		os.Getenv("CLIENT_ID"),
		os.Getenv("EXT_SECRET"),
		os.Getenv("EXT_VERSION"),
		os.Getenv("EXT_CONFIG_VER"),
	)
}

func main() {
	claims := twitchPkg.CreateClaims(
		"35851594",
		twitch.BroadcasterRole,
		twitch.FormBroadcastSendPubSubPermissions(),
	)
	token, err := twitchPkg.JWTSign(claims)
	if err != nil {
		log.Fatal(err)
	}

	claims, err = twitchPkg.JWTVerify(token)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(claims)
}


Development

To develop go-twitch-ext or interact with its source code in any meaningful way, be sure you have the following installed:

Prerequisites

You will need to activate Modules for your version of Go, generally by invoking go with the support GO111MODULE=on environment variable set.

Twitch Extension Configuration

From your Twitch Extension Dashboard you can get the following:

  • Client ID
  • Base64 Secret
  • Extension Version
  • Extension (Broadcaster/Developer) Config Version - OPTIONAL
Owner ID

To get the owner ID, you will need to first create a temporary access token via https://twitchtokengenerator.com/
Next fetch your ownerID from a simple CURL command against the Twitch /users endpoint.
You'll also need the client ID from https://twitchtokengenerator.com/ as part of the query.

curl -H "Client-ID: <client id>" -H "Authorization: Bearer <access token>" -X GET "https://api.twitch.tv/helix/users?login=<twitch-login-name>"

your ownerID is the value of 'id'

{
	"data": [{
		"id": "35851594",
		"login": "crazyjack12",
		"display_name": "crazyjack12",
		"type": "",
		"broadcaster_type": "",
		"description": "Do What Thou Wilt",
		"profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/ea0bf955-255a-4eca-ad80-460b88162910-profile_image-300x300.png",
		"offline_image_url": "",
		"view_count": 1702
	}]
}

Create your own extension!

Get started and create your extension today!.

FAQ & SUPPORT

For any questions or suggestions please join the 'go-twitch-ext' channel on Discord!

License

The source code for go-twitch-ext is released under the MIT License.

Donations

All donations are appreciated!

Donate

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllConfigurationsResponse

type AllConfigurationsResponse struct {
	Configurations map[string]*Configuration `json:"configurations"`
	ResponseCommon
}

AllConfigurationsResponse contains all possible segment configurations for a broadcaster

type Channel

type Channel struct {
	Game     string `json:"game"`
	ID       string `json:"id"`
	Username string `json:"username"`
	Title    string `json:"title"`
	Viewers  string `json:"view_count"`
}

Channel A struct representative of an individual channel returned by getLiveChannelsWithExtensionEnabled

type Configuration

type Configuration struct {
	Segment *Segment `json:"segment"`
	Record  *Record  `json:"record"`
}

Configuration Contains SegmentType and record information

type ConfigurationResponse

type ConfigurationResponse struct {
	Configuration *Configuration `json:"configurations"`
	ResponseCommon
}

ConfigurationResponse contains data for the queried segment configuration of type global, broadcaster or developer

type ExtensionEnabledChannels

type ExtensionEnabledChannels struct {
	ResponseCommon
	Channels []*Channel `json:"channels"`
	Bookmark string     `json:"cursor"`
}

ExtensionEnabledChannels Response type of the getLiveChannelsWithExtensionEnabled

type Options

type Options struct {
	Client *http.Client
}

Options optional parameters for the twitch ext client for configuration

type PubSubPermissions

type PubSubPermissions struct {
	Send   []PublishType `json:"send,omitempty"`
	Listen []PublishType `json:"listen,omitempty"`
}

PubSubPermissions publish permissions used within JWT claims

func FormBroadcastSendPubSubPermissions

func FormBroadcastSendPubSubPermissions() *PubSubPermissions

FormBroadcastSendPubSubPermissions create the pubsub permissions for publishing a broadcast message type

func FormGenericPubSubPermissions

func FormGenericPubSubPermissions() *PubSubPermissions

FormGenericPubSubPermissions create the pubsub permissions for publishing to message for any target type

func FormGlobalSendPubSubPermissions

func FormGlobalSendPubSubPermissions() *PubSubPermissions

FormGlobalSendPubSubPermissions create the pubsub permissions for publishing a global targeted message

func FormWhisperSendPubSubPermissions

func FormWhisperSendPubSubPermissions(opaqueId string) *PubSubPermissions

FormWhisperSendPubSubPermissions create the pubsub permissions for publishing a whisper message type

type PublishType

type PublishType string

PublishType The Pub/Sub broadcast type

const (
	GenericPublish   PublishType = "*"
	BroadcastPublish PublishType = "broadcast"
	GlobalPublish    PublishType = "global"
)

Types of Pub/Sub Permissions or targets

type Record

type Record struct {
	Version string `json:"version"`
	Content string `json:"content"`
}

Record contains information about the version and raw content stored within the SegmentType configuation

type ResponseCommon

type ResponseCommon struct {
	Headers http.Header
}

ResponseCommon ...

func (*ResponseCommon) GetExtSendChatMessageRateLimit

func (rc *ResponseCommon) GetExtSendChatMessageRateLimit() int

GetExtSendChatMessageRateLimit returns the "Ratelimit-Ratelimit-Ratelimiterextensionchatmessages-Limit" header as an int.

func (*ResponseCommon) GetExtSendChatMessageRateLimitRemaining

func (rc *ResponseCommon) GetExtSendChatMessageRateLimitRemaining() int

GetExtSendChatMessageRateLimitRemaining returns the "Ratelimit-Ratelimit-Ratelimiterextensionchatmessages-Remaining" header as an int.

func (*ResponseCommon) GetExtSetConfigurationRateLimit

func (rc *ResponseCommon) GetExtSetConfigurationRateLimit() int

GetExtSetConfigurationRateLimit returns the "Ratelimit-Ratelimiterextensionsetconfiguration-Limit" header as an int

func (*ResponseCommon) GetExtSetConfigurationRateLimitRemaining

func (rc *ResponseCommon) GetExtSetConfigurationRateLimitRemaining() int

GetExtSetConfigurationRateLimitRemaining returns the "Ratelimit-Ratelimiterextensionsetconfiguration-Remaining" header as an int.

func (*ResponseCommon) GetPubSubChannelRateLimit

func (rc *ResponseCommon) GetPubSubChannelRateLimit() int

GetPubSubChannelRateLimit returns the "Ratelimit-Ratelimitermessagesbychannel-Limit" header as an int for the pubsub messages to channel

func (*ResponseCommon) GetPubSubChannelRateLimitRemaining

func (rc *ResponseCommon) GetPubSubChannelRateLimitRemaining() int

GetPubSubChannelRateLimitRemaining returns the "Ratelimit-Ratelimitermessagesbychannel-Remaining" header as an int

type RoleType

type RoleType string

RoleType The user role type

const (
	BroadcasterRole RoleType = "broadcaster"
	ExternalRole    RoleType = "external"
	ModeratorRole   RoleType = "moderator"
	ViewerRole      RoleType = "viewer"
)

Types of user roles used within the JWT Claims

type Secret

type Secret struct {
	Active  string `json:"active"`
	Content string `json:"content"`
	Expires string `json:"expires"`
}

Secret information about a generated secret

type SecretsResponse

type SecretsResponse struct {
	Version int       `json:"format_version"`
	Secrets []*Secret `json:"secrets"`
	ResponseCommon
}

SecretsResponse response structure received when generating or querying for generated secrets

type Segment

type Segment struct {
	Segment   string `json:"segment_type"`
	ChannelID string `json:"channel_id,omitempty"`
}

Segment contains information about the type and twitch channel the configuration is for.

type SegmentType

type SegmentType string

SegmentType A segment configuration type

const (
	BroadcasterSegment SegmentType = "broadcaster"
	DeveloperSegment   SegmentType = "developer"
	GlobalSegment      SegmentType = "global"
)

Types of segments datastores for the configuration service

type Twitch

type Twitch struct {
	OwnerID       string
	Secret        string
	ClientID      string
	Version       string
	ConfigVersion string
	// contains filtered or unexported fields
}

Twitch package struct

func NewClient

func NewClient(
	ownerID string,
	clientID string,
	secret string,
	extVersion string,
	configVersion string,
	opts ...*Options,
) (
	twitch *Twitch,
)

NewClient create reference to twitch-ext package https://dev.twitch.tv/docs/extensions/reference/#endpoints

func (*Twitch) CreateClaims

func (t *Twitch) CreateClaims(
	channelID string,
	role RoleType,
	permissions *PubSubPermissions,
) (
	claims *TwitchJWTClaims,
)

CreateClaims will construct a claims suitable for generating a JWT token, containing necessary information required by the Twitch API. @param channelID if this value is empty it will default to 'all' @param role if this value is empty it will default to 'external'

func (*Twitch) CreateExtensionSecret

func (t *Twitch) CreateExtensionSecret(
	delay int,
) (
	resp *SecretsResponse,
	err error,
)

CreateExtensionSecret create a new twitch secret for your twitch extension. The delay period, between the generation of the new secret and its use by Twitch, is specified by delay. https://dev.twitch.tv/docs/extensions/reference/#create-extension-secret

func (*Twitch) GetAllChannelConfigurations

func (t *Twitch) GetAllChannelConfigurations(channelID string) (resp *AllConfigurationsResponse, err error)

GetAllChannelConfigurations retrieves channel specific configuration returns map containing both broadcaster and developer segments. https://dev.twitch.tv/docs/extensions/reference/#get-extension-channel-configuration

func (*Twitch) GetBroadcasterSegment

func (t *Twitch) GetBroadcasterSegment(channelID string) (*ConfigurationResponse, error)

GetBroadcasterSegment retrieves channel specific Broadcaster SegmentType configuration

func (*Twitch) GetDeveloperSegment

func (t *Twitch) GetDeveloperSegment(channelID string) (*ConfigurationResponse, error)

GetDeveloperSegment retrieves channel specific developer SegmentType configuration

func (*Twitch) GetExtensionSecrets

func (t *Twitch) GetExtensionSecrets() (
	resp *SecretsResponse,
	err error,
)

GetExtensionSecrets returns a list of the extension secrets https://dev.twitch.tv/docs/extensions/reference/#get-extension-secret

func (*Twitch) GetGlobalSegment

func (t *Twitch) GetGlobalSegment() (*ConfigurationResponse, error)

GetGlobalSegment retrieves global extension SegmentType configuration

func (*Twitch) GetLiveChannelsWithExtensionEnabled

func (t *Twitch) GetLiveChannelsWithExtensionEnabled(
	extensionId string,
	bookmark string,
) (
	channels *ExtensionEnabledChannels,
	err error,
)

GetLiveChannelsWithExtensionEnabled Retrieve all live twitch channels which have the extension enabled. https://dev.twitch.tv/docs/extensions/reference/#get-live-channels-with-extension-activated

func (*Twitch) JWTSign

func (t *Twitch) JWTSign(claims *TwitchJWTClaims) (tokenString string, err error)

JWTSign Sign the a JWT Claim to produce a base64 token.

func (*Twitch) JWTVerify

func (t *Twitch) JWTVerify(token string) (claims *TwitchJWTClaims, err error)

JWTVerify validates a extension client side twitch base64 token and converts it into a twitch claim type, containing relevant information.

func (*Twitch) PublishChannelNotification

func (t *Twitch) PublishChannelNotification(channelID string, i interface{}) (res *ResponseCommon, err error)

PublishChannelNotification publish a notification to a specific channel with the twitch extension enabled.

func (*Twitch) PublishGlobalNotification

func (t *Twitch) PublishGlobalNotification(i interface{}) (res *ResponseCommon, err error)

PublishGlobalNotification publish a notification to all channels with the twitch extension enabled. https://dev.twitch.tv/docs/extensions/reference/#send-extension-pubsub-message

func (*Twitch) PublishWhisperNotification

func (t *Twitch) PublishWhisperNotification(channelID string, opaqueId string, i interface{}) (res *ResponseCommon, err error)

PublishWhisperNotification publish a notification to a specific user viewing the twitch extension.

func (*Twitch) RevokeExtensionSecrets

func (t *Twitch) RevokeExtensionSecrets() (res *ResponseCommon, err error)

RevokeExtensionSecrets all the secrets to the extension. https://dev.twitch.tv/docs/extensions/reference/#revoke-extension-secrets

func (*Twitch) SendTwitchChatMessage

func (t *Twitch) SendTwitchChatMessage(channelID string, message string) (res *ResponseCommon, err error)

SendTwitchChatMessage publish message to twitch chat of a specific channel ID. - Twitch extension must have this permission - The maximum message size is 280 character - There is a limit of 12 messages per minute, per channel. https://dev.twitch.tv/docs/extensions/reference/#send-extension-chat-message

func (*Twitch) SetBroadcasterSegment

func (t *Twitch) SetBroadcasterSegment(data interface{}, channelID string) (res *ResponseCommon, err error)

SetBroadcasterSegment sets channel specific broadcaster SegmentType configuration

func (*Twitch) SetDeveloperSegment

func (t *Twitch) SetDeveloperSegment(data interface{}, channelID string) (res *ResponseCommon, err error)

SetDeveloperSegment sets channel specific developer SegmentType configuration

func (*Twitch) SetExtensionRequired

func (t *Twitch) SetExtensionRequired(channelID string) (res *ResponseCommon, err error)

SetExtensionRequired is used to indicate that a channel has updated their configration, matching that of the latest twitch extension configuration version configured. https://dev.twitch.tv/docs/extensions/reference/#set-extension-required-configuration

func (*Twitch) SetGlobalSegment

func (t *Twitch) SetGlobalSegment(data interface{}) (res *ResponseCommon, err error)

SetGlobalSegment sets global extension configuration

type TwitchJWTClaims

type TwitchJWTClaims struct {
	OpaqueUserID string             `json:"opaque_user_id,omitempty"`
	UserID       string             `json:"user_id"`
	ChannelID    string             `json:"channel_id,omitempty"`
	Role         RoleType           `json:"role"`
	Unlinked     bool               `json:"is_unlinked,omitempty"`
	Permissions  *PubSubPermissions `json:"pubsub_perms"`
	jwt.StandardClaims
}

TwitchJWTClaims contains information containing twitch specific JWT information.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL