From 0a029fcc235d40ccbb7124f4e394325b8103ce22 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Fri, 14 Mar 2025 08:56:02 +1100 Subject: [PATCH] Add Python client --- client-python/README.md | 13 ++++++++ client-python/client.py | 68 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 client-python/README.md create mode 100644 client-python/client.py diff --git a/client-python/README.md b/client-python/README.md new file mode 100644 index 0000000..a21768c --- /dev/null +++ b/client-python/README.md @@ -0,0 +1,13 @@ +## Chookchat Python Client + +This is an example client for Chookchat, written in Python. It is very simplistic. It connects to the Websocket, and prints recieved messages. + +This would be a good baseline for a GUI client with tkinter, or a bot for Chookchat linking it to other services. + +Not much else to say. + +### Running + +First, `pip3 install websocket-client rel`. If you need, create a virtual environment for Python. + +Then, run with `python3 client.py` diff --git a/client-python/client.py b/client-python/client.py new file mode 100644 index 0000000..826ba68 --- /dev/null +++ b/client-python/client.py @@ -0,0 +1,68 @@ +import websocket +import json +import _thread +import time +import rel +from google import genai + +username = "(insert username here)" +token = "(insert token here)" + +def joinRoom(roomName): + ws.send(json.dumps({ + "type": "joinRoom", + "username": username, + "token": token, + "room": roomName, + "content": "" + })) + +def sendMessage(content): + ws.send(json.dumps({ + "type": "message", + "username": username, + "token": token, + "content": content + })) + +def sendTyping(content): + ws.send(json.dumps({ + "type": "typing", + "username": username, + "token": token, + "content": content + })) + +def on_message(ws, message): + print("Message received: " + message) + if message == "ping": + ws.send("pong") + +def on_error(ws, error): + print("Error:", error) + +def on_close(ws, close_status_code, close_msg): + print(f"Connection closed: {close_status_code} - {close_msg}") + +def on_open(ws): + print("Opening connection to Chookchat...") + joinRoom("general") + ws.send(json.dumps({ + "type": "connect", + "username": username, + "token": token, + "content": username + " joined the room!" + })) + +if __name__ == "__main__": + websocket.enableTrace(True) + ws = websocket.WebSocketApp("wss://bobcompass.online/api/websocket", + on_open=on_open, + on_message=on_message, + on_error=on_error, + on_close=on_close + ) + + ws.run_forever(dispatcher=rel, reconnect=5, ping_interval=30, ping_timeout=10) + rel.signal(2, rel.abort) + rel.dispatch()