请给出对接go-cqhttp的Python3代码
以下是一个简单的Python3代码示例,用于与go-cqhttp进行对接:
import websocket
import json
def on_message(ws, message):
data = json.loads(message)
print(data)
def on_error(ws, error):
print(error)
def on_close(ws):
print("Websocket closed")
def on_open(ws):
# 发送验证消息
data = {
"action": "send_private_msg",
"params": {
"user_id": 123456,
"message": "Hello, world!"
}
}
ws.send(json.dumps(data))
if __name__ == "__main__":
# 连接到go-cqhttp的websocket接口
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:6700",
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.run_forever()
此示例使用Python的websocket库连接到go-cqhttp的websocket接口,并发送一条私聊消息。您可以根据需要修改on_open函数来发送不同类型的消息,并使用on_message函数处理返回的数据。
原文地址: https://www.cveoy.top/t/topic/bBkJ 著作权归作者所有。请勿转载和采集!