twitchpubsub

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2020 License: MIT Imports: 9 Imported by: 0

README

go-twitch-pubsub

Twitch PubSub library for Go

Getting Started

package main

import (
	"fmt"

	"github.com/pajlada/go-twitch-pubsub"
)

func main() {
	pubsubClient := twitchpubsub.NewClient(twitchpubsub.DefaultHost)

	userID := "82008718"
	channelID := "11148817"

	// OAuth token for userID with chat_login (or chat:read?) scope
	userToken := "jdgfkhjkdfhgdfjg"

	// Listen to a topic
	pubsubClient.Listen(twitchpubsub.ModerationActionTopic(userID, channelID), userToken)

	// Specify what callback is called when that topic receives a message
	pubsubClient.OnModerationAction(func(channelID string, event *twitchpubsub.ModerationAction) {
		fmt.Println(event.CreatedBy, event.ModerationAction, "on", event.TargetUserID)
	})

	go pubsubClient.Start()

	c := make(chan bool)
	<-c
}

Documentation

Index

Constants

View Source
const (
	TypeListen = "LISTEN"
)

Variables

View Source
var (
	// ErrNotConnected is returned if an action is attempted to be performed on a Client when it is not connected
	ErrNotConnected = errors.New("go-twitch-pubsub: Not connected")

	// ErrDisconnectedByUser is returned from Connect after the user calls Disconnect()
	ErrDisconnectedByUser = errors.New("go-twitch-pubsub: Disconnected by user")

	// DefaultHost is the default host to connect to Twitch's pubsub servers
	DefaultHost = "wss://pubsub-edge.twitch.tv"
)

Functions

func BitsEventTopic

func BitsEventTopic(channelID string) string

BitsEventTopic returns a properly formatted bits event topic string with the given channel ID argument

func ModerationActionTopic

func ModerationActionTopic(userID, channelID string) string

ModerationActionTopic returns a properly formatted moderation action topic string with the given user and channel ID arguments

func PointsActionTopic

func PointsActionTopic(channelID string) string

ModerationActionTopic returns a properly formatted moderation action topic string with the given user and channel ID arguments

func PointsEventTopic

func PointsEventTopic(channelID string) string

BitsEventTopic returns a properly formatted bits event topic string with the given channel ID argument

Types

type Base

type Base struct {
	Type string `json:"type"`
}

Base TODO: Refactor

type BaseData

type BaseData struct {
	Topic string `json:"topic"`
	// Message is an escaped json string
	Message string `json:"message"`
}

BaseData TODO: Refactor

type BitsEvent

type BitsEvent struct {
	UserName         string    `json:"user_name"`
	ChannelName      string    `json:"channel_name"`
	UserID           string    `json:"user_id"`
	ChannelID        string    `json:"channel_id"`
	Time             time.Time `json:"time"`
	ChatMessage      string    `json:"chat_message"`
	BitsUsed         int       `json:"bits_used"`
	TotalBitsUsed    int       `json:"total_bits_used"`
	Context          string    `json:"context"`
	BadgeEntitlement struct {
		NewVersion      int `json:"new_version"`
		PreviousVersion int `json:"previous_version"`
	} `json:"badge_entitlement"`
}

BitsEvent describes an incoming "Bit" action coming from Twitch's PubSub servers

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the client that connects to Twitch's pubsub servers

func NewClient

func NewClient(host string) *Client

NewClient creates a client struct and fills it in with some default values

func (*Client) Disconnect

func (c *Client) Disconnect()

Disconnect disconnects from Twitch's pubsub servers and leave the client in an idle state

func (*Client) Listen

func (c *Client) Listen(topicName string, authToken string)

Listen sends a message to Twitch's pubsub servers telling them we're interested in a specific topic Some topics require authentication, and for those you will need to pass a valid authentication token

func (*Client) OnBitsEvent

func (c *Client) OnBitsEvent(callback func(channelID string, data *BitsEvent))

OnBitsEvent attaches the given callback to the bits event

func (*Client) OnModerationAction

func (c *Client) OnModerationAction(callback func(channelID string, data *ModerationAction))

OnModerationAction attaches the given callback to the moderation action event

func (*Client) OnPointsAction

func (c *Client) OnPointsAction(callback func(channelID string, data *PointsAction))

func (*Client) OnPointsEvent

func (c *Client) OnPointsEvent(callback func(channelID string, data *PointsEvent))

func (*Client) SetConnectionLimit

func (c *Client) SetConnectionLimit(connectionLimit int)

func (*Client) SetTopicLimit

func (c *Client) SetTopicLimit(topicLimit int)

func (*Client) Start

func (c *Client) Start() error

Connect starts attempting to connect to the pubsub host

type InnerData

type InnerData struct {
	Data        json.RawMessage `json:"data"`
	Version     string          `json:"version"`
	MessageType string          `json:"message_type"`
	MessageID   string          `json:"message_id"`
}

InnerData TODO: Refactor

type Listen

type Listen struct {
	Base

	Nonce string `json:"nonce,omitempty"`

	Data ListenData `json:"data"`
}

type ListenData

type ListenData struct {
	Topics    []string `json:"topics"`
	AuthToken string   `json:"auth_token,omitempty"`
}

type Message

type Message struct {
	Base

	Data BaseData `json:"data"`
}

Message TODO: Refactor

type ModerationAction

type ModerationAction struct {
	Type             string   `json:"type"`
	ModerationAction string   `json:"moderation_action"`
	Arguments        []string `json:"args"`
	CreatedBy        string   `json:"created_by"`
	CreatedByUserID  string   `json:"created_by_user_id"`
	MsgID            string   `json:"msg_id"`
	TargetUserID     string   `json:"target_user_id"`
}

ModerationAction describes an incoming "Moderation" action coming from Twitch's PubSub servers

type PointsAction

type PointsAction struct {
	Timestamp   time.Time `json:"timestamp"`
	Redemption  string    `json:"redemption"`
	User_input  string    `json:"user_input"`
	Status      string    `json:"status"`
	Channel_id  string    `json:"channel_id"`
	Redeemed_at string    `json:"redeemed_at"`
	Reward      string    `json"reward"`
}

ModerationAction describes an incoming "Moderation" action coming from Twitch's PubSub servers

type PointsEvent

type PointsEvent struct {
	Channel_id  string `json:"channel_id"`
	Redeemed_at string `json:"redeemed_at"`
	Reward      string `json"reward"`
	Id          string `json:"id"`
	User        string `json:"user"`
}

BitsEvent describes an incoming "Bit" action coming from Twitch's PubSub servers

type ResponseMessage

type ResponseMessage struct {
	Base

	Error string `json:"error"`
	Nonce string `json:"nonce"`
}

ResponseMessage TODO: Refactor

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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