Beispiel #1
0
def index():
    # print request.args
    # print json.dumps(request.args)
    if request.method == 'GET':
        data = request.args
        signature = data.get('signature')
        timestamp = data.get('timestamp')
        nonce = data.get('nonce')
        echostr = data.get('echostr')
        token = current_app.config.get('TOKEN')
        try:
            check_signature(token, signature, timestamp, nonce)
        except InvalidSignatureException:
            return 'invalid signature'
        return echostr
    else:
        xml = request.data
        print xml
        msg = parse_message(xml)
        if msg.type == 'text':
            print msg.content
            reply =  TextReply(message=msg)
            reply.content = u'reply 测试'
            xml_reply = reply.render()
            return xml_reply
        elif msg.type == 'image':
            reply = ImageReply(message=msg)
            reply.media_id = msg.media_id
            xml_reply = reply.render()
            return xml_reply
        elif msg.type == 'voice':
            # reply = VoiceReply(message=msg)
            # reply.media_id = msg.media_id
            reply = TextReply(message=msg)
            reply.content = msg.recognition
            xml_reply = reply.render()
            return xml_reply
        elif msg.type == 'video':
            reply = VideoReply(message=msg)
            reply.media_id = msg.media_id
            reply.title = u'你的video'
            reply.description = u'wo 爱倪呀'
            xml_reply = reply.render()
            return xml_reply
            pass
        elif msg.type == 'location':
            pass
        elif msg.type == 'link':
            pass
        elif msg.type == 'shortvideo':
            reply = VideoReply(message=msg)
            reply.media_id = msg.thumb_media_id
            reply.title = u'你的video'
            reply.description = u'wo 爱倪呀'
            xml_reply = reply.render()
            return xml_reply
        else:
            return ''
Beispiel #2
0
    def test_video_reply_properties(self):
        from wechatpy.replies import VideoReply

        reply = VideoReply()
        reply.media_id = '123456'
        reply.title = 'test'

        self.assertEqual('123456', reply.media_id)
        self.assertEqual('test', reply.title)
Beispiel #3
0
    def test_video_reply_properties(self):
        from wechatpy.replies import VideoReply

        reply = VideoReply()
        reply.media_id = '123456'
        reply.title = 'test'

        self.assertEqual('123456', reply.media_id)
        self.assertEqual('test', reply.title)
Beispiel #4
0
    def test_video_reply_properties(self):
        from wechatpy.replies import VideoReply

        reply = VideoReply()
        reply.media_id = "123456"
        reply.title = "test"
        self.assertEqual("123456", reply.media_id)
        self.assertEqual("test", reply.title)

        reply = VideoReply(media_id="123456", title="test")
        self.assertEqual("123456", reply.media_id)
        self.assertEqual("test", reply.title)
Beispiel #5
0
    def post(self, request):
        xml = request.body
        msg = parse_message(xml)
        if msg.type == 'text':
            # 获取文本内容
            try:
                content = msg.content
                # print(res)
                # print(json.dump(res))
                reply = TextReply(content=content, message=msg)
                r_xml = reply.render()
                # 获取唯一标记用户的openid,下文介绍获取用户信息会用到
                openid = msg.source
                # print(openid)
                return HttpResponse(r_xml)
            except Exception as e:
                # 自行处理
                return HttpResponse('success')
            # return HttpResponse('')
        elif msg.type == 'image':
            print(msg.image, msg.media_id)
            reply = ArticlesReply(message=msg)

            reply.add_article({
                'title': 'Welcome tom my channel!',
                'description': 'Finally, we meet you here,\nYou must know you are very important for us!',
                'image': msg.image,
                'url': 'https://baidu.com'
            })
            r_xml = reply.render()
            return HttpResponse(r_xml)
        elif msg.type == 'voice':
            print(msg.format, msg.recognition)
            reply = VoiceReply(media_id=msg.media_id, message=msg)
            return HttpResponse(reply.render())
        elif msg.type == 'video':
            print(msg)
            print(msg.media_id, msg.thumb_media_id)
            reply = VideoReply(media_id=msg.media_id, title='Test video', description='Description', message=msg)

            return HttpResponse(reply.render())
        elif msg.type == 'location':
            print(msg.location_x, msg.location_y, msg.scale, msg.label, msg.location)
            return HttpResponse('Success')

        elif msg.type == 'event':
            # print(msg.type)
            if msg.event == 'subscribe':
                try:
                    reply = TextReply(content='Welcome to my channel', message=msg)
                    r_xml = reply.render()
                    return HttpResponse(r_xml)
                except:
                    return HttpResponse('Success')
            elif msg.event == 'unsubscribe':
                try:
                    reply = TextReply(content='See you again!', message=msg)
                    r_xml = reply.render()
                    return HttpResponse(r_xml)
                except:
                    return HttpResponse('Success')
            elif msg.event == 'subscribe_scan':
                try:
                    print(msg.scene_id, msg.ticket)
                    reply = TextReply(content='See you again!', message=msg)
                    r_xml = reply.render()
                    return HttpResponse(r_xml)
                except:
                    return HttpResponse('Success')
            elif msg.event == 'scan':
                try:
                    print(msg.scene_id, msg.ticket)
                    reply = TextReply(content='See you again!', message=msg)
                    r_xml = reply.render()
                    return HttpResponse(r_xml)
                except:
                    return HttpResponse('Success')
            elif msg.event == 'location':
                try:
                    print(msg.latitude, msg.longitude, msg.precision)
                    reply = TextReply(content='See you again!', message=msg)
                    r_xml = reply.render()
                    return HttpResponse(r_xml)
                except:
                    return HttpResponse('Success')
            elif msg.event == 'click':
                try:
                    print(msg.key)
                    reply = TextReply(content=msg.key, message=msg)
                    r_xml = reply.render()
                    return HttpResponse(r_xml)
                except:
                    return HttpResponse('Success')
            elif msg.event == 'view':
                try:
                    print(f'{msg.url}\n=========')
                    reply = TextReply(content=msg.url, message=msg)
                    r_xml = reply.render()
                    return HttpResponse(r_xml)
                except:
                    return HttpResponse('Success')
Beispiel #6
0
def handle_wx(request):
    if request.method == 'GET':
        signature = request.GET.get('signature', '')
        timestamp = request.GET.get('timestamp', '')
        nonce = request.GET.get('nonce', '')
        echo_str = request.GET.get('echostr', '')
        try:
            check_signature(token, signature, timestamp, nonce)
        except InvalidSignatureException:
            echo_str = '错误的请求'
        response = HttpResponse(echo_str)
        # client = WeChatClient("wx058a1e6adf42dede", "d4631adb81c598c1f9acddfb4bc2ba10")
        # menu = client.menu.get()
        # print(menu)
        return response
    elif request.method == 'POST':
        xml = request.body
        msg = parse_message(xml)
        openid = msg.source
        try:
            if msg.type == 'text':
                reply = TextReply(content=msg.content, message=msg)
                print(msg.content)
                return HttpResponse(reply.render())
            elif msg.type == 'image':
                print(msg.media_id)
                reply = ImageReply(media_id=msg.media_id, message=msg)
                return HttpResponse(reply.render())
            elif msg.type == 'voice':
                print(msg.media_id)
                reply = VoiceReply(media_id=msg.media_id, message=msg)
                return HttpResponse(reply.render())
            elif msg.type == 'video':
                print(msg)
                reply = VideoReply(media_id=msg.media_id,
                                   thumb_media_id=msg.thumb_media_id,
                                   title='title',
                                   description='description',
                                   message=msg)
                print(reply)
                print(msg.media_id)
                return HttpResponse(reply.render())
            elif msg.type == 'music':
                print(msg.thumb_media_id, msg.title, msg.description)
                reply = MusicReply(thumb_media_id=msg.thumb_media_id,
                                   title='title',
                                   description='description',
                                   music_url=msg.music_url,
                                   hq_music_url=msg.hq_music_url,
                                   message=msg)
                return HttpResponse(reply.render())
            # elif msg.type == 'news':
            #         reply = ArticlesReply(message=msg, articles=[
            #             {
            #                 'title': u'标题1',
            #                 'description': u'描述1',
            #                 'url': u'http://www.qq.com',
            #             },
            #             {
            #                 'title': u'标题2',
            #                 'description': u'描述2',
            #                 'url': u'http://www.qq.com',
            #                 'image': 'http://img.qq.com/1.png',
            #             },
            #         ])
            #         # 继续添加
            #         reply.add_article({
            #             'title': u'标题3',
            #             'description': u'描述3',
            #             'url': u'http://www.qq.com',
            #         })
            #         r_xml = reply.render()
            #         return HttpResponse(r_xml)
            elif msg.type == 'event':
                push = ScanCodeWaitMsgEvent(msg)
                #获取二维码信息,字符串
                content = msg.scan_result
                print(content)
                # 如何处理,自行处理,回复一段文本或者图文
                reply = TextReply(content="Someting", message=msg)
                r_xml = reply.render()
                return HttpResponse(r_xml)
            #pass
        except Exception as e:
            print("Exception:", e)
            return "success"
    return HttpResponse('ok')
Beispiel #7
0
def wechatHome(request):
    # get signature, timestamp and nonce
    signature = request.GET.get('signature')
    timestamp = request.GET.get('timestamp')
    nonce = request.GET.get('nonce')
    encrypt_type = request.GET.get('encrypt_type')
    msg_signature = request.GET.get('msg_signature')

    try:
        check_signature(TOKEN, signature, timestamp, nonce)
        if request.method == 'GET':
            response = request.GET.get('echostr')
            return HttpResponse(response, content_type="application/json")

# POST request
        if encrypt_type != None:
            # encryption mode
            print("request is encrypt")
            from wechatpy.crypto import WeChatCrypto
            crypto = WeChatCrypto(TOKEN, AES_KEY, APPID)
            try:
                # now msg is xml
                msg = crypto.decrypt_message(request.body, msg_signature,
                                             timestamp, nonce)
            except (InvalidSignatureException, InvalidAppIdException):
                HttpResponseBadRequest("decrypt message error")
        else:
            msg = request.body
        # plaintext mode
        print("before parse_message")
        pprint.pprint(msg)
        msg = wechatpy.parse_message(msg)
        print("after parse_message")
        pprint.pprint(msg)
        if msg.type == 'text':  # text message
            reply = TextReply(message=msg)
            reply.content = 'text reply'
        elif msg.type == 'image':
            reply = ImageReply(message=msg)
            reply.media_id = msg.media_id
        elif msg.type == 'voice':
            reply = VoiceReply(message=msg)
            reply.media_id = 'voice media id'
        elif msg.type == 'video':
            reply = VideoReply(message=msg)
            reply.media_id = 'video media id'
            reply.title = 'video title'
            reply.description = 'video description'
        elif msg.type == 'music':
            reply = MusicReply(message=msg)
            reply.thumb_media_id = 'thumb media id'
            reply.title = 'music title'
            reply.description = 'music description'
            reply.music_url = 'music url'
            reply.hq_music_url = 'hq music url'
        elif msg.type == 'news':
            reply = ArticlesReply(message=msg,
                                  articles=[
                                      {
                                          'title': u'标题1',
                                          'description': u'描述1',
                                          'url': u'http://www.qq.com',
                                      },
                                  ])
        else:
            reply = create_reply('Sorry, can not handle this for now', msg)
        return HttpResponse(reply.render(), content_type="application/json")
    except InvalidSignatureException as e:
        print("check_signature failed")
        HttpResponseBadRequest(e)