调整代码
This commit is contained in:
@@ -3,6 +3,7 @@ from gewechat.call_back_message.message import WxMessage, MessageType, AppMessag
|
||||
import logging
|
||||
|
||||
from robot import Robot
|
||||
from utils.json_converter import json_to_object
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -24,12 +25,12 @@ async def callback(request: Request):
|
||||
# 获取原始JSON数据
|
||||
json_data = await request.json()
|
||||
logger.info(f"收到回调消息: {json_data}")
|
||||
|
||||
|
||||
# 检查是否为测试消息
|
||||
if 'testMsg' in json_data and json_data.get('testMsg') == '验证回调地址是否可用':
|
||||
logger.info("收到回调地址验证消息,返回成功")
|
||||
return {"code": 0, "message": "success"}
|
||||
|
||||
|
||||
# 创建消息对象
|
||||
msg = WxMessage.from_json(json_data)
|
||||
|
||||
@@ -67,12 +68,7 @@ async def handle_add_message(msg: WxMessage):
|
||||
|
||||
|
||||
async def handle_mod_contacts(msg: WxMessage):
|
||||
"""处理联系人变更"""
|
||||
logger.info(f"联系人信息变更: {msg.raw_data}")
|
||||
# 获取对应的Robot实例并刷新联系人
|
||||
# robot = robot_instances.get(msg.appid)
|
||||
# if robot:
|
||||
# robot.refresh_contacts()
|
||||
|
||||
|
||||
async def handle_del_contacts(msg: WxMessage):
|
||||
|
||||
@@ -56,5 +56,24 @@ def set_call_back():
|
||||
|
||||
print(response.text)
|
||||
|
||||
def send_file():
|
||||
|
||||
url = "/message/postFile"
|
||||
|
||||
payload = json.dumps({
|
||||
"appId": app_id,
|
||||
"toWxid": "52418238895@chatroom",
|
||||
"fileName": "favicon.ico",
|
||||
"fileUrl": "http://192.168.2.210:8888/static/favicon.ico"
|
||||
})
|
||||
headers = {
|
||||
'X-GEWE-TOKEN': 'cb43f52db27e4a56bb6ec7da54373582',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
|
||||
response = requests.request("POST", base_url+url, headers=headers, data=payload)
|
||||
|
||||
print(response.text)
|
||||
|
||||
if __name__ == '__main__':
|
||||
set_call_back()
|
||||
send_file()
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import typing
|
||||
|
||||
|
||||
class GeweResponse:
|
||||
"""
|
||||
通用的 Gewechat 响应处理类
|
||||
"""
|
||||
|
||||
def __init__(self, resp: dict):
|
||||
"""
|
||||
:param resp: gewechat接口返回的原始字典
|
||||
"""
|
||||
self.raw = resp
|
||||
self.ret = resp.get("ret")
|
||||
self.msg = resp.get("msg")
|
||||
self.data = resp.get("data")
|
||||
|
||||
def is_success(self) -> bool:
|
||||
"""
|
||||
判断请求是否成功
|
||||
:return: True/False
|
||||
"""
|
||||
return self.ret == 200
|
||||
|
||||
def get_msg(self) -> str:
|
||||
"""
|
||||
获取返回消息
|
||||
:return: 消息字符串
|
||||
"""
|
||||
return self.msg or ""
|
||||
|
||||
def get_data(self) -> typing.Any:
|
||||
"""
|
||||
获取返回的数据对象,类型可能为dict、list、str等
|
||||
:return: data字段内容
|
||||
"""
|
||||
return self.data
|
||||
|
||||
def get_data_as_list(self) -> list:
|
||||
"""
|
||||
获取data字段为list类型(若不是则返回空列表)
|
||||
"""
|
||||
if isinstance(self.data, list):
|
||||
return self.data
|
||||
return []
|
||||
|
||||
def get_data_as_dict(self) -> dict:
|
||||
"""
|
||||
获取data字段为dict类型(若不是则返回空字典)
|
||||
"""
|
||||
if isinstance(self.data, dict):
|
||||
return self.data
|
||||
return {}
|
||||
|
||||
def __repr__(self):
|
||||
return f"<GeweResponse ret={self.ret} msg={self.msg} data_type={type(self.data).__name__}>"
|
||||
Reference in New Issue
Block a user