twitch

package module
v0.0.0-...-af41f20 Latest Latest
Warning

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

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

README

go-twitchchat-ws

Go library for connecting to Twitch chat via web sockets

Import

go get github.com/dobecad/go-twitchchat-ws

Usage

package main

import (
	"fmt"

	twitch "github.com/dobecad/go-twitchchat-ws"
)

func main() {
	opts := &twitch.ClientOpts{
		Tls:          true,
		Capabilities: []string{twitch.TagsCapability},
		Channels:     []string{"theprimeagen"},
	}

	// Anonymous client
	client := twitch.NewClient("justinfan321123", "59301", opts)
	if err := client.Connect(); err != nil {
		fmt.Println("Failed to connect: ", err)
		return
	}
	defer client.Disconnect()

	output := make(chan []byte)
	errors := make(chan error)
	go client.ReadMessages(output, errors)

	go func() {
		for err := range errors {
			fmt.Println("Error: ", err)
		}
	}()

	for msg := range output {
		fmt.Printf("Incoming message: %s", msg)
	}
}

Notes

The intention of this library is simply for maintaining a WS connection to multiple Twitch IRC channels for viewing the incoming messages. This library does NOT handle IRC message parsing.

Documentation

Index

Constants

View Source
const (
	TagsCapability       = "twitch.tv/tags"
	CommandsCapability   = "twitch.tv/commands"
	MembershipCapability = "twitch.tv/membership"
)

Variables

View Source
var (
	ErrNoConnection     = errors.New("no connection to websocket")
	ErrNoPingContent    = errors.New("no ping content")
	ErrChannelNotJoined = errors.New("client is not currently connected to given channel")
)

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(username string, accessToken string, opts *ClientOpts) *Client

Create a new client for connecting to Twitch's WS servers

func (*Client) Connect

func (ctx *Client) Connect() error

Establish an authenticated connection with Twitch WS server

func (*Client) Disconnect

func (ctx *Client) Disconnect() error

Disconnect from the Twitch WS

func (*Client) JoinChannel

func (c *Client) JoinChannel(channelName string) error

Join a new channel to recieve messages for

func (*Client) LeaveChannel

func (c *Client) LeaveChannel(channelName string) error

Leave a channel so that you no longer recieve that channels messages

func (*Client) ReadMessages

func (c *Client) ReadMessages(output chan<- []byte, error_out chan<- error)

Read chat messages from the channel.

This should be run as a goroutine.

Pass in an output and errors channel to ingest the incoming chat messages and errors

func (*Client) StopReadingMessages

func (c *Client) StopReadingMessages()

type ClientOpts

type ClientOpts struct {
	// Connect to Twitch TLS WS endpoint or non-TLS WS endpoint
	Tls bool

	// Specify which capabilities you want
	Capabilities []string

	// Specify channel names you want to join initially
	Channels []string
}

Jump to

Keyboard shortcuts

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