Twitch Chatbot
How to setup the Twitch chatbot in an Actor
Twitch channel name
It's possible streamer login to the bigscreen with a s6 account link to one twitch account but stream in a different account. For example, users have their own personal s6/twitch accounts but stream on STREAMSIX offical channel. This means that we cannot enforce the chatbot to read from twitch account link to bigscreen user's account. Instead, the bigscreen game should allow user to enter the twitch channel manually, then send the channel name to actor.
Setup (Golang)
To setup Twitch chatbot in actor (golang).
- Add twitch-client package in
go.mod
require (artifactory.timeplay.com/twitch-client v1.1.0-5)
- Twitch chatbot should only start when bigscreen user has started a Twitch stream. This means that the following info are required before chatbot is started:
- streamer's channel name
- bigscreen streamsix user ID
- session id
- In the actor, listen for twitch channel name from bigscreen.
import ("artifactory.timeplay.com/rmb-lib/pkg/config""artifactory.timeplay.com/twitch-client""artifactory.timeplay.com/util/v2/pkg/log"irc "github.com/gempir/go-twitch-irc/v3")func OnTwitchChannel(channel string, bigscreenID string, sessionID string) {c := config.Configcaller := user.AuthorizedUser{}setup := twitch.ChatSetupParam{Channel: channel,Cmds: []string{"!play"},}logParam := twitch.LoggingParam{BigscreenUserID: bigscreenID,SessionID: sessionID,License: c.License,Log: util.Log,}chatClient := twitch.StartAnonymousBot(caller, setup, logParam)}
The above setup auto parsing the chat message for anything that starts with strings in Cmds
. This is also auto sends event to data pipeline to track gameplay that occurs in the chat.
- To read chat message, register with the returned ChatClient object.
message
object contains most of the data from the chat, such as twitch user id, name, tags, and badges. If the message starts with a registered cmd, it will be parsed intotwitch.CmdData
.
chatClient.RegisterPrivateMessage(func(message irc.PrivateMessage, cmdData twitch.CmdData, s6UserID string) {log.Infoln(message.User.Name+": "+message.Message, message.User.ID, message.User.Name)})
- To listen for connection status
chatClient.RegisterOnConnect(func() {log.Infoln("connected")})chatClient.RegisterOnDisconnect(func(err error) {log.Errorln(err)})
Note that twitch-client package contains a TwitchClient. This is used to access Twitch API. This is not required to use the chatbot because Twitch chat implements IRC interface.
Setup (CSharp)
Requires
basic-rmb 1.3.0-2actor-sdk 1.3.0-2
Twitch chatbot is embedded in the actor-sdk. There is no additional package to install.
The Twitch chatbot is a wrapper that use TwitchLib underneath. You can access the TwitchLib directly to process chats. The wrapper provides event logging for any commands that player enter in the chat.
Below is sample code to start chatbot. It also setup Twitch pubsub and API access.
///// send these from bigscreen// bigscreen user's twitch channelvar channelName = "<channel>";// this is same as channel name, unless you have a dedicated chatbot accountvar chatbotUsername = "<username>";// bigscreen user's twitch access token, unless you're using a dedicated chatbot accountvar twitchAccessToken = "<token>";/////// twitch client id can usually be hardcoded. You can also get it from license servicevar twitchClientID = "<client_id>";TwitchManager.Start(Util.log, channelName, chatbotUsername, twitchAccessToken, twitchClientID);// listen for chatTwitchManager.Client.ChatClient.OnMessageReceived += Client_OnMessageReceived;// get user idvar user = await TwitchManager.Client.API.Helix.Users.GetUsersAsync(null, new List<string> { channelName }, twitchAccessToken);// listen for whisperTwitchManager.Client.PubSub.ListenToWhispers(user.Users[0].Id);TwitchManager.Client.PubSub.OnWhisper += PubSub_OnWhisper;// send topics once all listeners are setupTwitchManager.Client.PubSub.SendTopics(twitchAccessToken);