### Get File Path Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/file-service.md Generates the storage path for a file based on its hash and filename. It creates subdirectories using the first two characters of the hash and preserves the original file extension. The directory is created if it doesn't exist. ```python def _get_file_path(self, file_hash: str, filename: str) -> str: # Use the first two characters of the hash as the subdirectory name sub_dir = file_hash[:2] # Preserve the original file extension _, extension = os.path.splitext(filename) # Construct the full file path file_path = os.path.join(self.base_dir, sub_dir, f"{file_hash}{extension}") # Create the directory if it doesn't exist os.makedirs(os.path.dirname(file_path), exist_ok=True) return file_path ``` -------------------------------- ### Get Session List Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/wechat-service.md Retrieves a list of current chat sessions. The response includes session details like name, ID, and type (friend or group). ```python from app.services.wechat_service import WeChatService service = WeChatService() response = await service.get_session() if response.success: sessions = response.data["items"] for session in sessions: print(f"会话: {session['name']}") ``` -------------------------------- ### WeChatService Class Initialization Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/wechat-service.md Demonstrates the singleton pattern implementation for the WeChatService class, ensuring a single instance and automatic startup of the operation executor. ```python class WeChatService: def __new__(cls): if cls._instance is None: cls._instance = super(WeChatService, cls).__new__(cls) return cls._instance def __init__(self): if not self._initialized: self._queue = OperationQueue(max_concurrent=1) self._executor = UIOperationExecutor() self._executor.start() self._initialized = True ``` -------------------------------- ### chat_with Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/wechat-service.md Switches the current view to a specified chat window. Supports exact matching for the chat object. ```APIDOC ## POST /v1/wechat/chatwith ### Description Switches the current view to a specified chat window. Supports exact matching for the chat object. ### Method POST ### Endpoint /v1/wechat/chatwith ### Parameters #### Query Parameters - **who** (str) - Required - The nickname of the chat object. - **exact** (bool) - Optional - Whether to perform an exact match. Defaults to False. - **wxname** (str | None) - Optional - The WeChat account name. ### Request Example ```json { "who": "Zhang San" } ``` ### Response #### Success Response (200) - **success** (bool) - Indicates if the operation was successful. - **message** (str) - A message describing the result of the operation. - **data** (dict | None) - Additional data, if any. #### Response Example ```json { "success": true, "message": "Switched to chat with Zhang San.", "data": null } ``` ``` -------------------------------- ### Full Upload and Send Flow Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/file-service.md Demonstrates the complete process of uploading a file, retrieving its information, sending it via WeChat, listing files, and optionally deleting a file. ```python from fastapi import UploadFile, File from app.services.file_service import FileService from app.services.wechat_service import WeChatService # 1. Upload the file file_service = FileService() result = await file_service.upload_file(file) file_id = result.file_id # 2. Get file information file_info = file_service.get_file(file_id) print(f"File uploaded: {file_info.filename}") # 3. Send file via WeChat wechat_service = WeChatService() response = await wechat_service.send_file( file_id=file_id, who="Receiver" ) # 4. View file list total, files = file_service.list_files() # 5. Delete file (optional) file_service.delete_file(file_id) ``` -------------------------------- ### send_file Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/wechat-service.md Sends an uploaded file to a specified chat window. Requires a file ID obtained from an upload interface. ```APIDOC ## POST /v1/wechat/sendfile ### Description Sends an uploaded file to a specified chat window. Requires a file ID obtained from an upload interface. ### Method POST ### Endpoint /v1/wechat/sendfile ### Parameters #### Query Parameters - **file_id** (str) - Required - The ID of the file to send (obtained from the upload interface). - **who** (str | None) - Optional - The nickname of the recipient. - **exact** (bool) - Optional - Whether to perform an exact match for the recipient. Defaults to False. - **wxname** (str | None) - Optional - The WeChat account name. ### Request Example ```json { "file_id": "abc123def456...", "who": "Zhang San" } ``` ### Response #### Success Response (200) - **success** (bool) - Indicates if the operation was successful. - **message** (str) - A message describing the result of the operation. - **data** (dict | None) - Additional data, if any. #### Response Example ```json { "success": true, "message": "File sent successfully.", "data": null } ``` ``` -------------------------------- ### Send File Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/wechat-service.md Sends a previously uploaded file to a specified chat. Requires a file ID obtained from an upload interface. ```python from app.services.wechat_service import WeChatService service = WeChatService() # 发送文件 response = await service.send_file( file_id="abc123def456...", who="张三" ) ``` -------------------------------- ### get_session Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/wechat-service.md Retrieves a list of current chat sessions. Can be filtered by WeChat account name. ```APIDOC ## POST /v1/wechat/getsession ### Description Retrieves a list of current chat sessions. Can be filtered by WeChat account name. ### Method POST ### Endpoint /v1/wechat/getsession ### Parameters #### Query Parameters - **wxname** (str | None) - Optional - The WeChat account name. ### Request Example ```json { "wxname": "my_wechat_account" } ``` ### Response #### Success Response (200) - **success** (bool) - Indicates if the operation was successful. - **message** (str) - A message describing the result of the operation. - **data** (dict | None) - A dictionary containing session information: - **total** (int) - The total number of sessions. - **items** (list) - A list of session objects, each with: - **name** (str) - The name of the session. - **id** (str) - The ID of the session. - **type** (str) - The type of session ('friend' or 'group'). #### Response Example ```json { "success": true, "message": "Sessions retrieved successfully.", "data": { "total": 2, "items": [ { "name": "Zhang San", "id": "user123", "type": "friend" }, { "name": "My Group", "id": "group456", "type": "group" } ] } } ``` ``` -------------------------------- ### Switch Chat Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/wechat-service.md Switches the active chat window to a specified contact or group. Supports exact matching for the recipient. ```python from app.services.wechat_service import WeChatService service = WeChatService() # 切换到某个好友的聊天窗口 response = await service.chat_with(who="张三") ``` -------------------------------- ### Validate File Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/file-service.md Validates the size and MIME type of an uploaded file against configured limits. Resets the file pointer after validation. ```python async def _validate_file(self, file: UploadFile) -> None: # Check file size against the maximum allowed size if file.file.seek(0, 2) > self.max_size: raise HTTPException(status_code=400, detail="文件大小超过限制") # Check MIME type against the allowed types if file.content_type not in self.allowed_types: raise HTTPException(status_code=400, detail="不支持的文件类型") # Reset file pointer to the beginning of the file file.file.seek(0) ``` -------------------------------- ### send_message Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/wechat-service.md Sends a text message to a specified chat window. Supports clearing the input box, mentioning users, and exact recipient matching. ```APIDOC ## POST /v1/wechat/send ### Description Sends a text message to a specified chat window. Supports clearing the input box, mentioning users, and exact recipient matching. ### Method POST ### Endpoint /v1/wechat/send ### Parameters #### Query Parameters - **msg** (str) - Required - The content of the message. - **who** (str | None) - Optional - The nickname of the recipient. - **clear** (bool) - Optional - Whether to clear the input box after sending. Defaults to True. - **at** (str | list | None) - Optional - User(s) to mention. Can be a string or a list. - **exact** (bool) - Optional - Whether to perform an exact match for the recipient. Defaults to False. - **wxname** (str | None) - Optional - The WeChat account name. ### Request Example ```json { "msg": "Hello, this is a test message", "who": "File Transfer Helper" } ``` ### Response #### Success Response (200) - **success** (bool) - Indicates if the operation was successful. - **message** (str) - A message describing the result of the operation. - **data** (dict | None) - Additional data, if any. #### Response Example ```json { "success": true, "message": "Message sent successfully.", "data": null } ``` ``` -------------------------------- ### Send Text Message Source: https://github.com/cluic/wxauto-restful-api/blob/main/_autodocs/api-reference/wechat-service.md Sends a text message to a specified chat. Supports optional parameters for clearing the input box, mentioning users, and exact recipient matching. ```python from app.services.wechat_service import WeChatService service = WeChatService() # 发送简单消息 response = await service.send_message( msg="你好,这是一条测试消息", who="文件传输助手" ) # 发送带 @ 的消息 response = await service.send_message( msg="大家好", who="我的群聊", at=["张三", "李四"] ) # 不清空编辑框 response = await service.send_message( msg="继续输入", who="某个好友", clear=False ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.