コード例 #1
0
ファイル: api.py プロジェクト: tfdetang/Wechat-Discuss
def wechat():
    signature = request.args.get('signature', '')
    timestamp = request.args.get('timestamp', '')
    nonce = request.args.get('nonce', '')
    encrypt_type = request.args.get('encrypt_type', 'raw')
    msg_signature = request.args.get('msg_signature', '')
    try:
        check_signature(TOKEN, signature, timestamp, nonce)
    except InvalidSignatureException:
        abort(403)
    if request.method == 'GET':
        echo_str = request.args.get('echostr', '')
        return echo_str

    # POST request
    if encrypt_type == 'raw':
        # plaintext mode
        msg = parse_message(request.data)
        replies.EmptyReply().render()
        reply = handler.handle_msg(msg, AppId, Secret)
        return reply.render()
    else:
        # encryption mode
        crypto = WeChatCrypto(TOKEN, EncodingAESKey, AppId)
        try:
            msg = crypto.decrypt_message(request.data, msg_signature,
                                         timestamp, nonce)
        except (InvalidSignatureException, InvalidAppIdException):
            abort(403)
        else:
            msg = parse_message(msg)
            reply = handler.handle_msg(msg, AppId, Secret)
            return crypto.encrypt_message(reply.render(), nonce, timestamp)
コード例 #2
0
    def test_send(self):
        """测试客服回复"""
        sender = "openid"
        message = messages.TextMessage(dict(
            FromUserName=sender,
            content="xyz"
        ))
        message = self._wrap_message(message)

        # 空消息转换
        empty_msg = replies.EmptyReply()
        empty_str = ""
        self.assertIsNone(Reply.reply2send(empty_msg)[0])
        self.assertIsNone(Reply.reply2send(empty_str)[0])

        client = self.app.client.message

        # 文本消息转换
        content = "test"
        msg_type = Reply.MsgType.TEXT
        reply = Reply(type=msg_type, content=content).reply(message)
        funcname, kwargs = Reply.reply2send(reply)
        self.assertTrue(hasattr(client, funcname))
        self.assertEqual(funcname, "send_text")
        self.assertEqual(reply.content, kwargs["content"])

        # 图片消息转换
        media_id = "media_id"
        msg_type = Reply.MsgType.IMAGE
        reply = Reply(type=msg_type, media_id=media_id).reply(message)
        funcname, kwargs = Reply.reply2send(reply)
        self.assertTrue(hasattr(client, funcname))
        self.assertEqual(funcname, "send_image")
        self.assertEqual(reply.media_id, kwargs["media_id"])

        # 声音消息转换
        msg_type = Reply.MsgType.VOICE
        reply = Reply(type=msg_type, media_id=media_id).reply(message)
        funcname, kwargs = Reply.reply2send(reply)
        self.assertTrue(hasattr(client, funcname))
        self.assertEqual(funcname, "send_voice")
        self.assertEqual(reply.media_id, kwargs["media_id"])

        # 视频消息转换
        title = "title"
        description = "desc"
        msg_type = Reply.MsgType.VIDEO
        reply = Reply(type=msg_type, media_id=media_id, title=title,
            description=description).reply(message)
        funcname, kwargs = Reply.reply2send(reply)
        self.assertTrue(hasattr(client, funcname))
        self.assertEqual(funcname, "send_video")
        self.assertEqual(reply.media_id, kwargs["media_id"])
        self.assertEqual(reply.title, kwargs["title"])
        self.assertEqual(reply.description, kwargs["description"])
        # 选填字段
        reply = Reply(type=msg_type, media_id=media_id).reply(message)
        funcname, kwargs = Reply.reply2send(reply)
        self.assertTrue(hasattr(client, funcname))
        self.assertEqual(funcname, "send_video")
        self.assertEqual(reply.media_id, kwargs["media_id"])
        self.assertIsNone(kwargs["title"])
        self.assertIsNone(kwargs["description"])

        # 音乐消息转换
        music_url = "music_url"
        hq_music_url = "hq_music_url"
        msg_type = Reply.MsgType.MUSIC
        reply = Reply(type=msg_type, thumb_media_id=media_id, title=title,
            description=description, music_url=music_url,
            hq_music_url=hq_music_url).reply(message)
        funcname, kwargs = Reply.reply2send(reply)
        self.assertTrue(hasattr(client, funcname))
        self.assertEqual(funcname, "send_music")
        self.assertEqual(reply.thumb_media_id, kwargs["thumb_media_id"])
        self.assertEqual(reply.music_url, kwargs["url"])
        self.assertEqual(reply.hq_music_url, kwargs["hq_url"])
        self.assertEqual(reply.title, kwargs["title"])
        self.assertEqual(reply.description, kwargs["description"])
        # 选填字段
        reply = Reply(type=msg_type, thumb_media_id=media_id).reply(message)
        funcname, kwargs = Reply.reply2send(reply)
        self.assertTrue(hasattr(client, funcname))
        self.assertEqual(funcname, "send_music")
        self.assertEqual(reply.thumb_media_id, kwargs["thumb_media_id"])
        self.assertIsNone(kwargs["url"])
        self.assertIsNone(kwargs["hq_url"])
        self.assertIsNone(kwargs["title"])
        self.assertIsNone(kwargs["description"])

        # 图文消息转换
        pass

        # 确认消息发送
        handler = self._create_handler(replies=dict(
            type=Reply.MsgType.TEXT,
            content=content
        ))

        def callback(url, request, response):
            data = json.loads(request.body.decode())
            self.assertEqual(data["touser"], sender)
            self.assertEqual(data["msgtype"], Reply.MsgType.TEXT)
            self.assertEqual(data["text"]["content"], content)

        with wechatapi_accesstoken(), wechatapi("/cgi-bin/message/custom/send", dict(
            errcode=0
        ), callback):
            handler.replies.all()[0].send(message)
コード例 #3
0
 def test_empty_reply_deserialize(self):
     reply = replies.EmptyReply()
     self._test_deserialize(reply)