You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 satır
1.6KB

  1. from flask_restful import Resource, reqparse
  2. from pydantic import BaseModel, ValidationError
  3. from flask import jsonify,request,json
  4. from common import redis_helper,utils
  5. from wechat import gewe_chat
  6. from dataclasses import dataclass, asdict
  7. from typing import List
  8. import json
  9. class GetWxchatConfigResource(Resource):
  10. def __init__(self):
  11. self.parser = reqparse.RequestParser()
  12. self.wxchat = gewe_chat.wxchat
  13. def post(self):
  14. req = request.get_json()
  15. wxid = req.get("wxid")
  16. config=self.wxchat.get_wxchat_config_from_cache(wxid)
  17. return jsonify(config)
  18. @dataclass
  19. class AgentConfig(BaseModel):
  20. chatroomIdWhiteList: List[str] = []
  21. agentTokenId: str
  22. agentEnabled: bool
  23. addContactsFromChatroomIdWhiteList: List[str] = []
  24. chatWaitingMsgEnabled: bool
  25. class SaveWxchatConfigResource(Resource):
  26. def __init__(self):
  27. self.parser = reqparse.RequestParser()
  28. self.wxchat = gewe_chat.wxchat
  29. def post(self):
  30. req = request.get_json()
  31. wxid = req.get("wxid")
  32. data = req.get("data")
  33. # hash_key="__AI_OPS_WX__:WXCHAT_CONFIG"
  34. # redis_helper.redis_helper.update_hash_field(hash_key, wxid, json.dumps(data,ensure_ascii=False))
  35. try:
  36. # 使用 Pydantic 严格校验数据类型和结构
  37. validated_config = AgentConfig.model_validate(data)
  38. except ValidationError as e:
  39. response=jsonify({'code': 407, 'message': e.errors().__str__()})
  40. response.status_code = 407
  41. return response
  42. self.wxchat.save_wxchat_config(wxid, data)
  43. return jsonify({'wxid': wxid, 'config': data})