(self)
| 21 | return verify_server(web.input()) |
| 22 | |
| 23 | def POST(self): |
| 24 | try: |
| 25 | args = web.input() |
| 26 | verify_server(args) |
| 27 | request_time = time.time() |
| 28 | channel = WechatMPChannel() |
| 29 | message = web.data() |
| 30 | encrypt_func = lambda x: x |
| 31 | if args.get("encrypt_type") == "aes": |
| 32 | logger.debug("[wechatmp] Receive encrypted post data:\n" + message.decode("utf-8")) |
| 33 | if not channel.crypto: |
| 34 | raise Exception("Crypto not initialized, Please set wechatmp_aes_key in config.json") |
| 35 | message = channel.crypto.decrypt_message(message, args.msg_signature, args.timestamp, args.nonce) |
| 36 | encrypt_func = lambda x: channel.crypto.encrypt_message(x, args.nonce, args.timestamp) |
| 37 | else: |
| 38 | logger.debug("[wechatmp] Receive post data:\n" + message.decode("utf-8")) |
| 39 | msg = parse_message(message) |
| 40 | if msg.type in ["text", "voice", "image"]: |
| 41 | wechatmp_msg = WeChatMPMessage(msg, client=channel.client) |
| 42 | from_user = wechatmp_msg.from_user_id |
| 43 | content = wechatmp_msg.content |
| 44 | message_id = wechatmp_msg.msg_id |
| 45 | |
| 46 | supported = True |
| 47 | if "【收到不支持的消息类型,暂无法显示】" in content: |
| 48 | supported = False # not supported, used to refresh |
| 49 | |
| 50 | # New request |
| 51 | if ( |
| 52 | channel.cache_dict.get(from_user) is None |
| 53 | and from_user not in channel.running |
| 54 | or content.startswith("#") |
| 55 | and message_id not in channel.request_cnt # insert the godcmd |
| 56 | ): |
| 57 | # The first query begin |
| 58 | if msg.type == "voice" and wechatmp_msg.ctype == ContextType.TEXT and conf().get("voice_reply_voice", False): |
| 59 | context = channel._compose_context(wechatmp_msg.ctype, content, isgroup=False, desire_rtype=ReplyType.VOICE, msg=wechatmp_msg) |
| 60 | else: |
| 61 | context = channel._compose_context(wechatmp_msg.ctype, content, isgroup=False, msg=wechatmp_msg) |
| 62 | logger.debug("[wechatmp] context: {} {} {}".format(context, wechatmp_msg, supported)) |
| 63 | |
| 64 | if supported and context: |
| 65 | channel.running.add(from_user) |
| 66 | channel.produce(context) |
| 67 | else: |
| 68 | trigger_prefix = conf().get("single_chat_prefix", [""])[0] |
| 69 | if trigger_prefix or not supported: |
| 70 | if trigger_prefix: |
| 71 | reply_text = textwrap.dedent( |
| 72 | f"""\ |
| 73 | 请输入'{trigger_prefix}'接你想说的话跟我说话。 |
| 74 | 例如: |
| 75 | {trigger_prefix}你好,很高兴见到你。""" |
| 76 | ) |
| 77 | else: |
| 78 | reply_text = textwrap.dedent( |
| 79 | """\ |
| 80 | 你好,很高兴见到你。 |
nothing calls this directly
no test coverage detected