MCPcopy
hub / github.com/zhayujie/CowAgent / POST

Method POST

channel/wechatmp/active_reply.py:21–75  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

19 return verify_server(web.input())
20
21 def POST(self):
22 # Make sure to return the instance that first created, @singleton will do that.
23 try:
24 args = web.input()
25 verify_server(args)
26 channel = WechatMPChannel()
27 message = web.data()
28 encrypt_func = lambda x: x
29 if args.get("encrypt_type") == "aes":
30 logger.debug("[wechatmp] Receive encrypted post data:\n" + message.decode("utf-8"))
31 if not channel.crypto:
32 raise Exception("Crypto not initialized, Please set wechatmp_aes_key in config.json")
33 message = channel.crypto.decrypt_message(message, args.msg_signature, args.timestamp, args.nonce)
34 encrypt_func = lambda x: channel.crypto.encrypt_message(x, args.nonce, args.timestamp)
35 else:
36 logger.debug("[wechatmp] Receive post data:\n" + message.decode("utf-8"))
37 msg = parse_message(message)
38 if msg.type in ["text", "voice", "image"]:
39 wechatmp_msg = WeChatMPMessage(msg, client=channel.client)
40 from_user = wechatmp_msg.from_user_id
41 content = wechatmp_msg.content
42 message_id = wechatmp_msg.msg_id
43
44 logger.info(
45 "[wechatmp] {}:{} Receive post query {} {}: {}".format(
46 web.ctx.env.get("REMOTE_ADDR"),
47 web.ctx.env.get("REMOTE_PORT"),
48 from_user,
49 message_id,
50 content,
51 )
52 )
53 if msg.type == "voice" and wechatmp_msg.ctype == ContextType.TEXT and conf().get("voice_reply_voice", False):
54 context = channel._compose_context(wechatmp_msg.ctype, content, isgroup=False, desire_rtype=ReplyType.VOICE, msg=wechatmp_msg)
55 else:
56 context = channel._compose_context(wechatmp_msg.ctype, content, isgroup=False, msg=wechatmp_msg)
57 if context:
58 channel.produce(context)
59 # The reply will be sent by channel.send() in another thread
60 return "success"
61 elif msg.type == "event":
62 logger.info("[wechatmp] Event {} from {}".format(msg.event, msg.source))
63 if msg.event in ["subscribe", "subscribe_scan"]:
64 reply_text = subscribe_msg()
65 if reply_text:
66 replyPost = create_reply(reply_text, msg)
67 return encrypt_func(replyPost.render())
68 else:
69 return "success"
70 else:
71 logger.info("暂且不处理")
72 return "success"
73 except Exception as exc:
74 logger.exception(exc)
75 return exc

Callers

nothing calls this directly

Calls 9

WechatMPChannelClass · 0.90
WeChatMPMessageClass · 0.90
confFunction · 0.90
subscribe_msgFunction · 0.90
verify_serverFunction · 0.85
decodeMethod · 0.80
produceMethod · 0.80
getMethod · 0.45
_compose_contextMethod · 0.45

Tested by

no test coverage detected