Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

80 lignes
3.6KB

  1. import time
  2. import web
  3. from wechatpy import parse_message
  4. from wechatpy.replies import create_reply
  5. from bridge.context import *
  6. from bridge.reply import *
  7. from channel.wechatmp.common import *
  8. from channel.wechatmp.wechatmp_channel import WechatMPChannel
  9. from channel.wechatmp.wechatmp_message import WeChatMPMessage
  10. from common.log import logger
  11. from config import conf, subscribe_msg
  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(wechatmp_msg.ctype, content, isgroup=False, desire_rtype=ReplyType.VOICE, msg=wechatmp_msg)
  49. else:
  50. context = channel._compose_context(wechatmp_msg.ctype, content, isgroup=False, msg=wechatmp_msg)
  51. if context:
  52. # set private openai_api_key
  53. # if from_user is not changed in itchat, this can be placed at chat_channel
  54. user_data = conf().get_user_data(from_user)
  55. context["openai_api_key"] = user_data.get("openai_api_key") # None or user openai_api_key
  56. channel.produce(context)
  57. # The reply will be sent by channel.send() in another thread
  58. return "success"
  59. elif msg.type == "event":
  60. logger.info("[wechatmp] Event {} from {}".format(msg.event, msg.source))
  61. if msg.event in ["subscribe", "subscribe_scan"]:
  62. reply_text = subscribe_msg()
  63. if reply_text:
  64. replyPost = create_reply(reply_text, msg)
  65. return encrypt_func(replyPost.render())
  66. else:
  67. return "success"
  68. else:
  69. logger.info("暂且不处理")
  70. return "success"
  71. except Exception as exc:
  72. logger.exception(exc)
  73. return exc