import asyncio
import json
import websockets

async def send_message(uri, message):
    '''
    使用 WebSockets 发送消息到 Go-CQHTTP
    
    Args:
        uri (str): Go-CQHTTP 的 WebSocket 地址
        message (str): 要发送的消息
    
    Returns:
        None
    '''
    async with websockets.connect(uri) as websocket:  # 连接 WebSocket 服务器
        await websocket.send(message)  # 发送消息
        response = await websocket.recv()  # 接收回复
        print(response)  # 打印回复

async def main():
    '''
    主函数,发送私聊消息
    '''
    uri = 'ws://localhost:6700'  # Go-CQHTTP 的 WebSocket 地址和端口号
    message = json.dumps({
        'action': 'send_private_msg',  # 发送私聊消息
        'params': {
            'user_id': 123456,  # 目标用户的 QQ 号
            'message': 'Hello, World!'  # 消息内容
        }
    })
    await send_message(uri, message)  # 调用发送消息函数

asyncio.get_event_loop().run_until_complete(main())  # 运行异步任务
Python 使用 WebSockets 库对接 Go-CQHTTP 示例代码

原文地址: https://www.cveoy.top/t/topic/m5LR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录