Exemplo n.º 1
0
    def _record_message(self, request, message, user_profile,
                        from_weixin_user):
        try:
            mp_user = WeixinMpUser.objects.get(owner_id=user_profile.user_id)
        except:
            if settings.DUMP_DEBUG_MSG:
                from core.exceptionutil import full_stack
                print '========== start monitored exception =========='
                print full_stack()
                print 'no mp_user for webapp_id ', user_profile.webapp_id
                print '========== finish monitored exception =========='
            #没有注册mp user,人工客服不可用,直接返回

            return

        sender_icon = self._get_weixin_user_head_icon(
            from_weixin_user.username)
        if hasattr(message, 'content'):
            content = message.content
        else:
            content = ''
        mediaId, picUrl = '', ''
        if message.msgType == WeixinMessageTypes.TEXT:
            content = message.content
        elif message.msgType == WeixinMessageTypes.VOICE:
            mediaId = message.mediaId
        elif message.msgType == WeixinMessageTypes.IMAGE:
            mediaId = message.mediaId
            picUrl = message.picUrl

        params = {
            'sender_username': from_weixin_user.username,
            'sender_nickname': from_weixin_user.username,
            'sender_fake_id': '',
            'sender_icon': sender_icon,
            'receiver_username': mp_user.username,
            'receiver_nickname': mp_user.username,
            'receiver_fake_id': '',
            'receiver_icon': '[not used]',
            'content': content,
            'mpuser': mp_user,
            'mode': settings.MODE,
            'weixin_created_at': message.createTime,
            'message_type': message.msgType,
            'msg_id': message.msgId,
            'pic_url': picUrl,
            'media_id': mediaId
        }
        if 'sender_fake_id' in request.POST:
            params['sender_fake_id'] = request.POST['sender_fake_id']

        return message_util.record_message(params)
Exemplo n.º 2
0
def get_or_create_messge(context, from_weixin_user):
    request, message, user_profile, member, response_rule, from_weixin_user, is_from_simulator = _get_info_from_context(
        context)

    try:
        mp_user = WeixinMpUser.objects.get(owner_id=user_profile.user_id)
    except:
        if settings.DUMP_DEBUG_MSG:
            from core.exceptionutil import full_stack
            print '========== start monitored exception =========='
            print full_stack()
            print 'no mp_user for webapp_id ', user_profile.webapp_id
            print '========== finish monitored exception =========='
        #没有注册mp user,人工客服不可用,直接返回
        return

    sender_icon = _get_weixin_user_head_icon(from_weixin_user.username)
    if message.has_key("content"):  #:hasattr(message, 'content'):
        content = message["content"]
    else:
        content = ''
    mediaId, picUrl = '', ''
    if message["msgType"] == WeixinMessageTypes.TEXT:
        content = message["content"]
    elif message["msgType"] == WeixinMessageTypes.VOICE:
        mediaId = message["mediaId"]
    elif message["msgType"] == WeixinMessageTypes.IMAGE:
        mediaId = message["mediaId"]
        picUrl = message["picUrl"]

    params = {
        'sender_username': from_weixin_user.username,
        'sender_nickname': from_weixin_user.username,
        'sender_fake_id': '',
        'sender_icon': sender_icon,
        'receiver_username': mp_user.username,
        'receiver_nickname': mp_user.username,
        'receiver_fake_id': '',
        'receiver_icon': '[not used]',
        'content': content,
        'mpuser': mp_user,
        'mode': settings.MODE,
        'weixin_created_at': message["createTime"],
        'message_type': message["msgType"],
        'msg_id': message["msgId"],
        'pic_url': picUrl,
        'media_id': mediaId
    }
    if 'sender_fake_id' in request["POST"]:
        params['sender_fake_id'] = request["POST"]['sender_fake_id']

    return message_util.record_message(params)
Exemplo n.º 3
0
def get_or_create_messge(context, from_weixin_user):
    request, message, user_profile, member, response_rule, from_weixin_user, is_from_simulator = _get_info_from_context(
        context)

    try:
        mp_user = WeixinMpUser.objects.get(owner_id=user_profile.user_id)
    except:
        if settings.DUMP_DEBUG_MSG:
            from core.exceptionutil import full_stack
            print '========== start monitored exception =========='
            print full_stack()
            print 'no mp_user for webapp_id ', user_profile.webapp_id
            print '========== finish monitored exception =========='
        #没有注册mp user,人工客服不可用,直接返回
        return

    sender_icon = _get_weixin_user_head_icon(from_weixin_user.username)
    if message.has_key("content"):  #:hasattr(message, 'content'):
        content = message["content"]
    else:
        content = ''
    mediaId, picUrl = '', ''
    if message["msgType"] == WeixinMessageTypes.TEXT:
        content = message["content"]
    elif message["msgType"] == WeixinMessageTypes.VOICE:
        mediaId = message["mediaId"]
    elif message["msgType"] == WeixinMessageTypes.IMAGE:
        mediaId = message["mediaId"]
        picUrl = message["picUrl"]
        try:
            import time
            if picUrl.find('mmbiz.qpic.cn') > -1:
                nonce_str = ''.join(
                    random.sample(string.ascii_letters + string.digits, 6))
                picUrlName = "%s%s" % (nonce_str, str(int(time.time() * 1000)))
                picUrl = upyun_util.upload_weixin_url_to_upyun(
                    picUrl, picUrlName)
        except:
            print 'error at picUrl to upyun'

    #判断是否公众号信息: added by slzhu
    is_from_mp_user = False
    if response_rule:
        is_from_mp_user = True

    is_un_read_msg = True
    if response_rule and response_rule.has_key(
            'type') and response_rule['type'] == 1:
        is_un_read_msg = False

    params = {
        'sender_username': from_weixin_user.username,
        'sender_nickname': from_weixin_user.username,
        'sender_fake_id': '',
        'sender_icon': sender_icon,
        'receiver_username': mp_user.username,
        'receiver_nickname': mp_user.username,
        'receiver_fake_id': '',
        'receiver_icon': '[not used]',
        'content': content,
        'mpuser': mp_user,
        'mode': settings.MODE,
        'weixin_created_at': message["createTime"],
        'message_type': message["msgType"],
        'msg_id': message["msgId"],
        'pic_url': picUrl,
        'media_id': mediaId,
        'is_from_mp_user': is_from_mp_user,
        'is_un_read_msg': is_un_read_msg
    }
    # if 'sender_fake_id' in request["POST"]:
    # 	params['sender_fake_id'] = request["POST"]['sender_fake_id']
    #return message_util.record_message(params)
    return message_util.record_message(params)