需要开启正向ws,参考教程如下
https://napneko.github.io/zh-CN/guide/integration
onebot-11 api接口:https://github.com/botuniverse/onebot-11/blob/master/api/public.md#send_like-%E5%8F%91%E9%80%81%E5%A5%BD%E5%8F%8B%E8%B5%9E
nc接口:https://napneko.github.io/zh-CN/develop/api
nc把前面都差不多实现了 自己看看接口
代码如下:
import websocket
import json
def on_message(ws, message):
try:
# 解析 JSON 消息
json_message = json.loads(message)
# 打印接收到的消息
print(f"Received message: {json_message}")
# 判断是否包含点赞信息
if json_message.get("post_type") == "message":
raw_message = json_message.get("raw_message", "")
if "点赞" in raw_message:
user_id = json_message.get("sender", {}).get("user_id")
if user_id:
# 调用点赞 API
send_like(user_id)
except json.JSONDecodeError as e:
print(f"Failed to decode JSON message: {e}")
def send_like(user_id):
# 构建点赞的 API 请求数据
payload = {
"action": "send_like",
"params": {
"user_id": user_id,
"times": 10
},
}
ws.send(json.dumps(payload))
def on_error(ws, error):
print(f"An error occurred: {error}")
def on_close(ws):
print("Connection closed")
def on_open(ws):
ws.send("Hello, WebSocket!")
ws_url = "ws://192.168.11.25:3001" # 替换为你的 WebSocket 服务器 URI
ws = websocket.WebSocketApp(
ws_url,
on_message=on_message,
on_error=on_error,
on_close=on_close
)
ws.on_open = on_open
ws.run_forever()