Example #1
0
def wechat_handler():
    msg = request.wechat_msg
    _uuid = insert_message(msg)
    if msg.type == 'event' and msg.event == 'subscribe':
        reply = TextReply(content='感谢关注!', message=msg)
    elif msg.type == 'text':
        reply_type, reply_txt_or_article = rulebasedrobot(msg)
        if reply_type == 0:
            reply = TextReply(content=reply_txt_or_article, message=msg)
        elif reply_type == 1:
            reply = ArticlesReply(message=msg)
            reply.add_article(reply_txt_or_article)
    elif msg.type == 'image':
        reply = TextReply(content='别这样,你的图片会被我保存的。', message=msg)
    else:
        reply = TextReply(content='仅支持文本、图片消息。', message=msg)
    insert_response(reply, _uuid)
    return reply
Example #2
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')
Example #3
0
def connect():
    if request.method == 'GET':
        args = request.args
        resp_body = ""
        for k, v in args.items():
            print(f"{k}: {v}")
        print(args)
        try:
            check_signature_str = check_signature(token='001001001001',
                                                  signature=args['signature'],
                                                  timestamp=args['timestamp'],
                                                  nonce=args['nonce'])
            print(check_signature_str)
            resp_body = args["echostr"]
        except InvalidSignatureException:
            print("Error with check_signature")
            resp_body = "Error with check_signature"
            pass
        return resp_body
    elif request.method == 'POST':
        xml = request.stream.read()
        msg = parse_message(xml)
        print(msg)
        if msg.type == 'text':
            reply = TextReply(content=msg.content, message=msg)
        elif msg.type == 'image':
            reply = ImageReply(media_id=msg.media_id, message=msg)
            ocr_text = ''
            try:
                cred = credential.Credential(
                    os.environ.get("TENCENTCLOUD_SECRET_ID"),
                    os.environ.get("TENCENTCLOUD_SECRET_KEY"))
                httpProfile = HttpProfile()
                httpProfile.endpoint = "ocr.tencentcloudapi.com"

                clientProfile = ClientProfile()
                clientProfile.httpProfile = httpProfile
                client = ocr_client.OcrClient(cred, "ap-hongkong",
                                              clientProfile)

                ocrreq = models.GeneralFastOCRRequest()
                ocrreq.ImageUrl = msg.image  #发送过来的消息的url
                ocrres = client.GeneralFastOCR(ocrreq)
                print(ocrres.to_json_string())

                for x in json.loads(ocrres.to_json_string())["TextDetections"]:
                    print(x["DetectedText"])
                    ocr_text = ocr_text + x["DetectedText"]

            except TencentCloudSDKException as err:
                print(err)
            reply = ArticlesReply(message=msg)
            # simply use dict as article
            reply.add_article({
                'title': 'OCR Test',
                'description': ocr_text,
                'image': msg.image
                #'url': 'url'
            })
        else:
            reply = TextReply(content='Hello,大哥,目前只支持文本和图片', message=msg)
        xml = reply.render()
        print(reply)
        response = make_response(xml)
        response.content_type = 'application/xml'
        return response