A Web Socket / TCP Socket server for use inside Streamlabs Chatbot's scripting API.
- Use sample: Interactive Spinning Wheel
Here I will show you how to set it up so you can start developing your own interactions for Streamlabs Chatbot.
• Streamlabs Chatbot
- Download the latest version of the script
- Extract the folder in your Scripts folder
- In the top of your Python script, where all the imports are, include the following
import clr
import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
clr.AddReferenceToFileAndPath('InteractiveLimitless.dll')
from InteractiveLS import Interaction
- Then on your variables initialization part of the script, add the following line:
global InteractionInstance #Can be any name, this will hold our interaction instance
- Now lets define our own function which will be run when we get a message from the client:
def OnMessageReceived(sender,e):
Parent.SendStreamMessage(e.Message)
- Now, on the
init()
section of the script, add the following:
def Init():
global settings, InteractionInstance
path = os.path.dirname(__file__)
try:
with codecs.open(os.path.join(path, configFile), encoding='utf-8-sig', mode='r') as file:
settings = json.load(file, encoding='utf-8-sig')
except:
settings = {
"WebSocketPort": 15011,
"TCPSocketPort": 15010
}
InteractionInstance = Interaction.GetInstance()
InteractionInstance.MessageReceived += OnMessageReceived
#"ws" for Web sockets and "tcp" for TCP server
InteractionInstance.StartServer('ws',settings["WebSocketPort"])
return
Make sure to register your function adding it to the InteractionInstance.MessageReceived
event handler.
- To send data to the clients, we use the
SendMessage()
method as follows:
def Execute(data):
if data.IsChatMessage():
InteractionInstance.SendMessage(data.UserName)
*You can only send strings to the client. You could convert a python dictionary to json before sending it to the client and parsing it when the client responds.
- To finish, make sure to unload everything just as follows:
def Unload():
global InteractionInstance
InteractionInstance.Close()
InteractionInstance= None
return
def ReloadSettings(jsonData):
Unload()
Init()
return
- Now using your favorite Web Sockets / TCP client connect to the localhost IP
127.0.0.1
and the port specified in the script/config file
Get an instance of the class
instance = Interaction Interaction.GetInstance()
Add the function you want to run to this event handler
def myFunction(sender,e):
Parent.SendStreamMessage(e.Message)
Interaction.MessageReceived += myFunction
sender
- The object that called the evente
- The event arguments, where theMessage
property has the message passed to the server
Start the desired server with the desired port
"ws"
- To start a Web Socket server"tcp"
- To start a TCP socket server
int
- Port number to start the server on
Interaction.ServerStart("ws", 15011)
Send a message to all the clients connected to the server
interaction.SendMessage("Hi clients, its server")
Closes all servers and cleans up. Must be used in the Unload()
function of your script
Interaction.Close()
Plays a windows sound. Mostly used to debug
Interaction.Beep()