您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ServiceAccount.py 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import web
  2. import time
  3. import channel.wechatmp.reply as reply
  4. import channel.wechatmp.receive as receive
  5. from config import conf
  6. from common.log import logger
  7. from bridge.context import *
  8. from channel.wechatmp.common import *
  9. from channel.wechatmp.wechatmp_channel import WechatMPChannel
  10. # This class is instantiated once per query
  11. class Query():
  12. def GET(self):
  13. return verify_server(web.input())
  14. def POST(self):
  15. # Make sure to return the instance that first created, @singleton will do that.
  16. channel = WechatMPChannel()
  17. try:
  18. webData = web.data()
  19. # logger.debug("[wechatmp] Receive request:\n" + webData.decode("utf-8"))
  20. wechatmp_msg = receive.parse_xml(webData)
  21. if wechatmp_msg.msg_type == 'text':
  22. from_user = wechatmp_msg.from_user_id
  23. message = wechatmp_msg.content.decode("utf-8")
  24. message_id = wechatmp_msg.msg_id
  25. logger.info("[wechatmp] {}:{} Receive post query {} {}: {}".format(web.ctx.env.get('REMOTE_ADDR'), web.ctx.env.get('REMOTE_PORT'), from_user, message_id, message))
  26. context = channel._compose_context(ContextType.TEXT, message, isgroup=False, msg=wechatmp_msg)
  27. if context:
  28. # set private openai_api_key
  29. # if from_user is not changed in itchat, this can be placed at chat_channel
  30. user_data = conf().get_user_data(from_user)
  31. context['openai_api_key'] = user_data.get('openai_api_key') # None or user openai_api_key
  32. channel.produce(context)
  33. # The reply will be sent by channel.send() in another thread
  34. return "success"
  35. elif wechatmp_msg.msg_type == 'event':
  36. logger.info("[wechatmp] Event {} from {}".format(wechatmp_msg.Event, wechatmp_msg.from_user_id))
  37. content = subscribe_msg()
  38. replyMsg = reply.TextMsg(wechatmp_msg.from_user_id, wechatmp_msg.to_user_id, content)
  39. return replyMsg.send()
  40. else:
  41. logger.info("暂且不处理")
  42. return "success"
  43. except Exception as exc:
  44. logger.exception(exc)
  45. return exc