active_reply.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. channel.produce(context)
  53. # The reply will be sent by channel.send() in another thread
  54. return "success"
  55. elif msg.type == "event":
  56. logger.info("[wechatmp] Event {} from {}".format(msg.event, msg.source))
  57. if msg.event in ["subscribe", "subscribe_scan"]:
  58. reply_text = subscribe_msg()
  59. if reply_text:
  60. replyPost = create_reply(reply_text, msg)
  61. return encrypt_func(replyPost.render())
  62. else:
  63. return "success"
  64. else:
  65. logger.info("暂且不处理")
  66. return "success"
  67. except Exception as exc:
  68. logger.exception(exc)
  69. return exc