Ejemplo n.º 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 ''
Ejemplo n.º 2
0
    def parse_msg(self, msg):
        print msg.type
        print msg
        if msg.type == "event":
            content = ""
            if msg._data.get("Event", "") == "subscribe":
                content = "1、每日标准上班时间 10:00到19:00;" + \
                    "2、打卡时间10:03之前,之后记为迟到;" + \
                    "3、每月3次以内(含3次)迟到10:30之内,不做处理," + \
                    "超出3次,从第四次开始累计罚款20 40 80 160 平方递增," + \
                    "迟到超过30分钟按照事假2小时处理;" + \
                    "4、每人每月带薪病事假1天,不可跨月累计," + \
                    "超出部分病假发放50%工资(1天以上的需要医院假条)" + \
                    ",事假不发工资;" + \
                    "5、全勤奖,每月无事假病假,及任何迟到显现可获得全勤奖," + \
                    "奖品每月更换,平均价值300元;" + \
                    "6、加班,每日加班超过22:00(需要管理人员在加班单上签字)," + \
                    "第二天可11:00到岗,周末及节假日加班可累计倒休,抵扣事假,病假。"

            reply = TextReply(message=msg)
            reply.content = content

        elif msg.type == "text":
            reply = TextReply(message=msg)
            reply.content = msg._data.get("Content")

        elif msg.type == "image":
            reply = ImageReply(message=msg)
            reply.media_id = msg._data.get("MediaId")

        elif msg.type == "voice":
            reply = VoiceReply(message=msg)
            reply.media_id = msg._data.get("MediaId")
            pass
        else:
            return None

        re_xml = reply.render()
        return re_xml
Ejemplo n.º 3
0
def weixin_handler():
    # 从config文件中获取
    token = os.getenv('WECHAT_TOKEN', config.WECHAT_TOKEN)
    encodingAESKey = os.getenv('WECHAT_ENCODING_AES_KEY',
                               config.WECHAT_ENCODING_AES_KEY)
    appId = os.getenv('WECHAT_APP_ID', config.WECHAT_APP_ID)

    # 从请求中获取
    signature = flask.request.args.get("signature")
    timestamp = flask.request.args.get("timestamp")
    nonce = flask.request.args.get("nonce")
    echostr = flask.request.args.get("echostr")
    # encrypt_type = flask.request.args.get("encrypt_type")
    msg_signature = flask.request.args.get("msg_signature")
    '''
    print('signature:', signature)
    print('timestamp: ', timestamp)
    print('nonce:', nonce)
    print('echo_str:', echostr)
    print('encrypt_type:', encrypt_type)
    print('msg_signature:', msg_signature)
    '''

    crypto = WeChatCrypto(token, encodingAESKey, appId)
    try:
        check_signature(token, signature, timestamp, nonce)  # 签名验证
    except InvalidSignatureException:
        flask.abort(403)  # 校验token失败,证明这条消息不是微信服务器发送过来的

    if flask.request.method == "GET":  # 如果时明文模式 直接返回echoster
        return echostr
    elif flask.request.method == "POST":  # 如果时加密模式 先对传入的数据解密
        try:
            msg = crypto.decrypt_message(flask.request.data, msg_signature,
                                         timestamp, nonce)
            print('Descypted message: \n%s' % msg)  # 输出数据内容
        except (InvalidSignatureException, InvalidAppIdException):
            flask.abort(404)
        msg = parse_message(msg)  # 解析xml
        if msg.type == 'text':
            res = get_robot_reply(msg.content)
            reply = TextReply(message=msg)
            reply.content = '%s' % (res)
        elif msg.type == "image":  # 图片回复
            reply = ImageReply(message=msg)
            reply.media_id = msg.media_id
        else:
            reply = TextReply(content="暂时不支持此种类型的回复哦~", message=msg)
        print('Enscypted message: \n%s' % reply)
        # 返回加密信息
        return crypto.encrypt_message(reply.render(), nonce, timestamp)
Ejemplo n.º 4
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)