mirror of
				https://github.com/open-webui/bot
				synced 2025-06-26 18:16:24 +00:00 
			
		
		
		
	init
This commit is contained in:
		
							parent
							
								
									9b31a0098a
								
							
						
					
					
						commit
						4cb3e243c9
					
				
							
								
								
									
										0
									
								
								bot/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								bot/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										12
									
								
								bot/env.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								bot/env.py
									
									
									
									
									
										Normal file
									
								
							| @ -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", "") | ||||
							
								
								
									
										45
									
								
								bot/examples/pingpong.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								bot/examples/pingpong.py
									
									
									
									
									
										Normal file
									
								
							| @ -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() | ||||
							
								
								
									
										45
									
								
								bot/main.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								bot/main.py
									
									
									
									
									
										Normal file
									
								
							| @ -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() | ||||
							
								
								
									
										14
									
								
								bot/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								bot/utils.py
									
									
									
									
									
										Normal file
									
								
							| @ -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() | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user