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.

88 lines
3.8KB

  1. import time
  2. import web
  3. from channel.wechatmp.wechatmp_message import WeChatMPMessage
  4. from bridge.context import *
  5. from bridge.reply import *
  6. from channel.wechatmp.common import *
  7. from channel.wechatmp.wechatmp_channel import WechatMPChannel
  8. from wechatpy import parse_message
  9. from common.log import logger
  10. from config import conf
  11. from wechatpy.replies import create_reply
  12. # This class is instantiated once per query
  13. class Query:
  14. def GET(self):
  15. return verify_server(web.input())
  16. def POST(self):
  17. # Make sure to return the instance that first created, @singleton will do that.
  18. try:
  19. args = web.input()
  20. verify_server(args)
  21. channel = WechatMPChannel()
  22. message = web.data()
  23. encrypt_func = lambda x: x
  24. if args.get("encrypt_type") == "aes":
  25. logger.debug("[wechatmp] Receive encrypted post data:\n" + message.decode("utf-8"))
  26. if not channel.crypto:
  27. raise Exception("Crypto not initialized, Please set wechatmp_aes_key in config.json")
  28. message = channel.crypto.decrypt_message(message, args.msg_signature, args.timestamp, args.nonce)
  29. encrypt_func = lambda x: channel.crypto.encrypt_message(x, args.nonce, args.timestamp)
  30. else:
  31. logger.debug("[wechatmp] Receive post data:\n" + message.decode("utf-8"))
  32. msg = parse_message(message)
  33. if msg.type in ["text", "voice", "image"]:
  34. wechatmp_msg = WeChatMPMessage(msg, client=channel.client)
  35. from_user = wechatmp_msg.from_user_id
  36. content = wechatmp_msg.content
  37. message_id = wechatmp_msg.msg_id
  38. logger.info(
  39. "[wechatmp] {}:{} Receive post query {} {}: {}".format(
  40. web.ctx.env.get("REMOTE_ADDR"),
  41. web.ctx.env.get("REMOTE_PORT"),
  42. from_user,
  43. message_id,
  44. content,
  45. )
  46. )
  47. if msg.type == "voice" and wechatmp_msg.ctype == ContextType.TEXT and conf().get("voice_reply_voice", False):
  48. context = channel._compose_context(
  49. wechatmp_msg.ctype, content, isgroup=False, desire_rtype=ReplyType.VOICE, msg=wechatmp_msg
  50. )
  51. else:
  52. context = channel._compose_context(
  53. wechatmp_msg.ctype, content, isgroup=False, msg=wechatmp_msg
  54. )
  55. if context:
  56. # set private openai_api_key
  57. # if from_user is not changed in itchat, this can be placed at chat_channel
  58. user_data = conf().get_user_data(from_user)
  59. context["openai_api_key"] = user_data.get(
  60. "openai_api_key"
  61. ) # None or user openai_api_key
  62. channel.produce(context)
  63. # The reply will be sent by channel.send() in another thread
  64. return "success"
  65. elif msg.type == "event":
  66. logger.info(
  67. "[wechatmp] Event {} from {}".format(
  68. msg.event, msg.source
  69. )
  70. )
  71. if msg.event in ["subscribe", "subscribe_scan"]:
  72. reply_text = subscribe_msg()
  73. replyPost = create_reply(reply_text, msg)
  74. return encrypt_func(replyPost.render())
  75. else:
  76. return "success"
  77. else:
  78. logger.info("暂且不处理")
  79. return "success"
  80. except Exception as exc:
  81. logger.exception(exc)
  82. return exc