diff --git a/bot/__init__.py b/bot/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bot/env.py b/bot/env.py new file mode 100644 index 0000000..4cc234d --- /dev/null +++ b/bot/env.py @@ -0,0 +1,12 @@ +import os + +try: + from dotenv import load_dotenv + + load_dotenv("../.env") +except ImportError: + print("dotenv not installed, skipping...") + + +WEBUI_URL = os.getenv("WEBUI_URL", "http://localhost:8080") +TOKEN = os.getenv("TOKEN", "") diff --git a/bot/examples/pingpong.py b/bot/examples/pingpong.py new file mode 100644 index 0000000..de82e21 --- /dev/null +++ b/bot/examples/pingpong.py @@ -0,0 +1,45 @@ +import socketio +from env import WEBUI_URL, TOKEN +from utils import send_message + +# Create a Socket.IO client instance +sio = socketio.Client(logger=True, engineio_logger=True) + + +# Event handlers +@sio.event +def connect(): + print("Connected!") + + +@sio.event +def disconnect(): + print("Disconnected from the server!") + + +def events(sio, user_id): + @sio.on("channel-events") + def channel_events(data): + if data["user"]["id"] == user_id: + # Ignore events from the bot itself + return + + print("Channel events:", data) + send_message(data["channel_id"], "Pong!") + + +try: + print(f"Connecting to {WEBUI_URL}...") + sio.connect(WEBUI_URL, socketio_path="/ws/socket.io", transports=["websocket"]) + print("Connection established!") +except Exception as e: + print(f"Failed to connect: {e}") + + +def join_callback(data): + events(sio, data["id"]) + + +# Authenticate with the server +sio.emit("user-join", {"auth": {"token": TOKEN}}, callback=join_callback) +sio.wait() diff --git a/bot/main.py b/bot/main.py new file mode 100644 index 0000000..de82e21 --- /dev/null +++ b/bot/main.py @@ -0,0 +1,45 @@ +import socketio +from env import WEBUI_URL, TOKEN +from utils import send_message + +# Create a Socket.IO client instance +sio = socketio.Client(logger=True, engineio_logger=True) + + +# Event handlers +@sio.event +def connect(): + print("Connected!") + + +@sio.event +def disconnect(): + print("Disconnected from the server!") + + +def events(sio, user_id): + @sio.on("channel-events") + def channel_events(data): + if data["user"]["id"] == user_id: + # Ignore events from the bot itself + return + + print("Channel events:", data) + send_message(data["channel_id"], "Pong!") + + +try: + print(f"Connecting to {WEBUI_URL}...") + sio.connect(WEBUI_URL, socketio_path="/ws/socket.io", transports=["websocket"]) + print("Connection established!") +except Exception as e: + print(f"Failed to connect: {e}") + + +def join_callback(data): + events(sio, data["id"]) + + +# Authenticate with the server +sio.emit("user-join", {"auth": {"token": TOKEN}}, callback=join_callback) +sio.wait() diff --git a/bot/utils.py b/bot/utils.py new file mode 100644 index 0000000..582bd36 --- /dev/null +++ b/bot/utils.py @@ -0,0 +1,14 @@ +import requests +from env import WEBUI_URL, TOKEN + + +def send_message(channel_id: str, message: str): + url = f"{WEBUI_URL}/api/v1/channels/{channel_id}/messages/post" + headers = {"Authorization": f"Bearer {TOKEN}"} + + data = {"content": message} + + response = requests.post(url, headers=headers, json=data) + response.raise_for_status() + + return response.json()