コード例 #1
0
def handle_group_message(msg):
    obj = json.loads(msg)
    if not obj.has_key("appid") or not obj.has_key("sender") or \
       not obj.has_key("receivers") or not obj.has_key("group_id"):
        logging.warning("invalid push msg:%s", msg)
        return

    logging.debug("group push msg:%s", msg)

    appid = obj["appid"]
    sender = obj["sender"]
    receivers = obj["receivers"]
    group_id = obj["group_id"]

    appname = get_title(appid)
    sender_name = user.get_user_name(rds, appid, sender)

    content = push_content(sender_name, obj["content"])

    extra = {}
    extra["sender"] = sender

    if group_id:
        extra["group_id"] = group_id

    for receiver in receivers:
        if group_id:
            quiet = user.get_user_notification_setting(rds, appid, receiver, group_id)
            if quiet:
                logging.info("uid:%d group id:%d is in quiet mode", receiver, group_id)
                continue

        push_message(appid, appname, receiver, content, extra)
コード例 #2
0
def handle_im_message(msg):
    obj = json.loads(msg)
    if not obj.has_key("appid") or \
       not obj.has_key("sender") or \
       not obj.has_key("receiver"):
        logging.warning("invalid push msg:%s", msg)
        return

    logging.debug("push msg:%s", msg)
    appid = obj["appid"]
    sender = obj["sender"]
    receiver = obj["receiver"]

    appname = get_title(appid)
    sender_name = user.get_user_name(rds, appid, sender)
    extra = {}
    extra["sender"] = sender

    do_not_disturb = user.get_user_do_not_disturb(rds, appid, receiver, sender)
    if do_not_disturb:
        logging.debug("uid:%s set do not disturb :%s", receiver, sender)
        return

    content_obj = json.loads(obj['content'])
    if content_obj.get('revoke'):
        collapse_id = content_obj.get('revoke').get('msgid')
        sender_name = sender_name if sender_name else ''
        content = "%s撤回了一条消息"%sender_name
    else:
        collapse_id = content_obj.get('uuid')
        content = push_content(sender_name, obj["content"])
    push_message(appid, appname, receiver, content, extra, collapse_id=collapse_id)
コード例 #3
0
def handle_voip_message(msg):
    obj = json.loads(msg)
    appid = obj["appid"]
    sender = obj["sender"]
    receiver = obj["receiver"]

    appname = get_title(appid)
    sender_name = user.get_user_name(rds, appid, sender)
    u = user.get_user(rds, appid, receiver)
    if u is None:
        logging.info("uid:%d nonexist", receiver)
        return
    #找出最近绑定的token
    ts = max(u.apns_timestamp, u.xg_timestamp, u.ng_timestamp, u.mi_timestamp, u.hw_timestamp, u.gcm_timestamp,
             u.ali_timestamp)

    if sender_name:
        sender_name = sender_name.decode("utf8")
        content = "%s:%s"%(sender_name, u"请求与你通话")
    else:
        content = u"你的朋友请求与你通话"

    if u.apns_device_token and u.apns_timestamp == ts:
        sound = "apns.caf"
        badge = 0
        ios_push(appid, u.apns_device_token, content, badge, sound, {})
    elif u.mi_device_token and u.mi_timestamp == ts:
        MiPush.push_message(appid, u.mi_device_token, content)
    elif u.hw_device_token and u.hw_timestamp == ts:
        HuaWeiPush.push_message(appid, u.hw_device_token, content)
    elif u.ali_device_token and u.ali_timestamp == ts:
        AliPush.push_message(appid, appname, u.hw_device_token, content)
    else:
        logging.info("uid:%d has't device token", receiver)
コード例 #4
0
def handle_customer_message_v2(msg):
    obj = json.loads(msg)
    if "appid" not in obj or "content" not in obj or \
       "sender_appid" not in obj or "sender" not in obj or \
       "receiver_appid" not in obj or "receiver" not in obj:
        logging.warning("invalid customer push msg:%s", msg)
        return

    logging.debug("customer push msg:%s", msg)

    appid = obj["appid"]
    sender_appid = obj["sender_appid"]
    sender = obj["sender"]
    receiver_appid = obj["receiver_appid"]
    receiver = obj["receiver"]
    raw_content = obj["content"]

    assert (appid == receiver_appid)

    extra = {}
    appname = get_title(appid)
    u = user.get_user(rds, appid, receiver)
    if u is None:
        logging.info("uid:%d nonexist", receiver)
        return

    if u.wx_openid:
        result = WXPush.push(appid, appname, u.wx_openid, raw_content)
        # errcode=45015,
        # errmsg=response out of time limit or subscription is canceled
        if result and result.get('errcode') == 45015:
            now = int(time.time())
            content_obj = {
                "wechat": {
                    "customer_appid": receiver_appid,
                    "customer_id": receiver,
                    "timestamp": now,
                    "notification": "微信会话超时"
                }
            }
            post_system_message(sender_appid, sender, json.dumps(content_obj))
    else:
        extra['xiaowei'] = {"new": 1}
        sender_name = user.get_user_name(rds, sender_appid, sender)
        content = push_content(sender_name, raw_content)
        push_customer_support_message(appid, appname, u, content, extra)
コード例 #5
0
def handle_im_message(msg):
    obj = json.loads(msg)
    if not obj.has_key("appid") or \
       not obj.has_key("sender") or \
       not obj.has_key("receiver"):
        logging.warning("invalid push msg:%s", msg)
        return

    logging.debug("push msg:%s", msg)
    appid = obj["appid"]
    sender = obj["sender"]
    receiver = obj["receiver"]

    appname = get_title(appid)
    sender_name = user.get_user_name(rds, appid, sender)
    content = push_content(sender_name, obj["content"])

    extra = {}
    extra["sender"] = sender
    push_message(appid, appname, receiver, content, extra)
コード例 #6
0
def handle_customer_message(msg):
    obj = json.loads(msg)
    if not obj.has_key("appid") or not obj.has_key("command") or \
       not obj.has_key("customer_appid") or not obj.has_key("customer") or \
       not obj.has_key("seller") or not obj.has_key("content") or \
       not obj.has_key("store") or not obj.has_key("receiver"):
        logging.warning("invalid customer push msg:%s", msg)
        return

    logging.debug("customer push msg:%s", msg)

    appid = obj["appid"]
    receiver = obj["receiver"]
    command = obj["command"]
    customer_appid = obj["customer_appid"]
    customer = obj["customer"]
    store = obj["store"]
    seller = obj["seller"]
    raw_content = obj["content"]

    appname = get_title(appid)

    extra = {}
    if command == MSG_CUSTOMER:
        sender_name = user.get_user_name(rds, customer_appid, customer)
        content = push_content(sender_name, raw_content)
        push_message(appid, appname, receiver, content, extra)
    elif command == MSG_CUSTOMER_SUPPORT:
        if appid == customer_appid and receiver == customer:
            #客服发给顾客
            u = user.get_user(rds, appid, receiver)
            if u is None:
                logging.info("uid:%d nonexist", receiver)
                return

            if u.wx_openid:
                result = WXPush.push(appid, appname, u.wx_openid, raw_content)
                #errcode=45015,
                #errmsg=response out of time limit or subscription is canceled
                if result and result.get('errcode') == 45015:
                    now = int(time.time())
                    content_obj = {
                        "wechat": {
                            "customer_appid": customer_appid,
                            "customer_id": customer,
                            "timestamp": now,
                            "notification": "微信会话超时"
                        }
                    }
                    post_system_message(config.KEFU_APPID, seller,
                                        json.dumps(content_obj))
            else:
                extra['store_id'] = store
                extra['xiaowei'] = {"new": 1}
                store = Store.get_store(rds, store)
                sender_name = store.name
                content = push_content(sender_name, raw_content)
                push_customer_support_message(appid, appname, u, content,
                                              extra)
        else:
            #群发到其它客服人员
            sender_name = user.get_user_name(rds, appid, seller)
            content = push_content(sender_name, raw_content)
            push_message(appid, appname, receiver, content, extra)
コード例 #7
0
def send_group_message(obj):
    appid = obj["appid"]
    sender = obj["sender"]
    receivers = obj["receivers"]
    group_id = obj["group_id"]

    appname = get_title(appid)
    sender_name = user.get_user_name(rds, appid, sender)

    content = push_content(sender_name, obj["content"])

    try:
        c = json.loads(obj["content"])
        collapse_id = c.get('uuid')
        if c.has_key('revoke'):
            collapse_id = c['revoke']['msgid']
            sender_name = sender_name if sender_name else ''
            content = "%s撤回了一条消息" % sender_name
        at = c.get('at', [])
        at_all = c.get('at_all', False)
    except ValueError:
        at = []
        at_all = False
        collapse_id = None

    extra = {}
    extra["sender"] = sender
    extra["group_id"] = group_id

    apns_users = []
    jp_users = []
    xg_users = []
    hw_users = []
    gcm_users = []
    mi_users = []
    ali_users = []

    # 群聊中被at的用户
    at_users = []
    for receiver in receivers:
        quiet = user.get_user_notification_setting(rds, appid, receiver,
                                                   group_id)
        if quiet:
            logging.info("uid:%d group id:%d do not disturb", receiver,
                         group_id)

        if quiet and receiver not in at:
            continue

        u = user.get_user(rds, appid, receiver)
        if u is None:
            logging.info("uid:%d nonexist", receiver)
            continue

        if (at_all or receiver in at) and sender_name:
            at_users.append(u)
            continue

        # 找出最近绑定的token
        ts = max(u.apns_timestamp, u.xg_timestamp, u.mi_timestamp,
                 u.hw_timestamp, u.gcm_timestamp, u.ali_timestamp,
                 u.jp_timestamp)

        if u.apns_device_token and u.apns_timestamp == ts:
            apns_users.append(u)
        elif u.xg_device_token and u.xg_timestamp == ts:
            xg_users.append(u)
        elif u.mi_device_token and u.mi_timestamp == ts:
            mi_users.append(u)
        elif u.hw_device_token and u.hw_timestamp == ts:
            hw_users.append(u)
        elif u.gcm_device_token and u.gcm_timestamp == ts:
            gcm_users.append(u)
        elif u.ali_device_token and u.ali_timestamp == ts:
            ali_users.append(u)
        elif u.jp_device_token and u.jp_timestamp == ts:
            jp_users.append(u)
        else:
            logging.info("uid:%d has't device token", receiver)

    for u in at_users:
        content = "%s在群聊中@了你" % sender_name
        push_message_u(appid, appname, u, content, extra)

    for u in xg_users:
        xg_push(appid, appname, u.xg_device_token, content, extra)

    for u in hw_users:
        HuaWeiPush.push(appid, appname, u.hw_device_token, content)

    for u in gcm_users:
        GCMPush.push(appid, appname, u.gcm_device_token, content)

    for u in ali_users:
        AliPush.push(appid, appname, u.ali_device_token, content)

    # ios apns
    notifications = []
    for u in apns_users:
        sound = 'default'
        payload = Payload(alert=content,
                          sound=sound,
                          badge=u.unread + 1,
                          custom=extra)
        token = u.apns_device_token
        n = Notification(token, payload, None)
        notifications.append(n)

    if notifications:
        IOSPush.push_group_batch(appid, notifications, collapse_id)

    for u in apns_users:
        user.set_user_unread(rds, appid, receiver, u.unread + 1)

    # 极光推送
    tokens = []
    for u in jp_users:
        tokens.append(u.jp_device_token)
    if tokens:
        JGPush.push(appid, appname, tokens, content)

    tokens = []
    for u in mi_users:
        tokens.append(u.mi_device_token)

    MiPush.push_batch(appid, appname, tokens, content)
コード例 #8
0
def handle_im_messages(msgs):
    msg_objs = []
    for msg in msgs:
        obj = json.loads(msg)
        if not obj.has_key("appid") or \
                not obj.has_key("sender") or \
                not obj.has_key("receiver"):
            logging.warning("invalid push msg:%s", msg)
            continue

        msg_objs.append(obj)
        logging.debug("push msg:%s", msg)

    apns_users = []
    jp_users = []
    xg_users = []
    hw_users = []
    gcm_users = []
    mi_users = []
    ali_users = []

    for obj in msg_objs:
        appid = obj["appid"]
        sender = obj["sender"]
        receiver = obj["receiver"]

        appname = get_title(appid)
        sender_name = user.get_user_name(rds, appid, sender)

        do_not_disturb = user.get_user_do_not_disturb(rds, appid, receiver,
                                                      sender)
        if do_not_disturb:
            logging.debug("uid:%s set do not disturb :%s", receiver, sender)
            continue

        u = user.get_user(rds, appid, receiver)
        if u is None:
            logging.info("uid:%d nonexist", receiver)
            continue

        content_obj = json.loads(obj['content'])
        if content_obj.get('revoke'):
            collapse_id = content_obj.get('revoke').get('msgid')
            sender_name = sender_name if sender_name else ''
            content = "%s撤回了一条消息" % sender_name
        else:
            collapse_id = content_obj.get('uuid')
            content = push_content(sender_name, obj["content"])

        # 找出最近绑定的token
        ts = max(u.apns_timestamp, u.xg_timestamp, u.mi_timestamp,
                 u.hw_timestamp, u.gcm_timestamp, u.ali_timestamp,
                 u.jp_timestamp)

        if u.apns_device_token and u.apns_timestamp == ts:
            apns_users.append((u, appname, content, collapse_id))
        elif u.xg_device_token and u.xg_timestamp == ts:
            xg_users.append((u, appname, content, collapse_id))
        elif u.mi_device_token and u.mi_timestamp == ts:
            mi_users.append((u, appname, content, collapse_id))
        elif u.hw_device_token and u.hw_timestamp == ts:
            hw_users.append((u, appname, content, collapse_id))
        elif u.gcm_device_token and u.gcm_timestamp == ts:
            gcm_users.append((u, appname, content, collapse_id))
        elif u.ali_device_token and u.ali_timestamp == ts:
            ali_users.append((u, appname, content, collapse_id))
        elif u.jp_device_token and u.jp_timestamp == ts:
            jp_users.append((u, appname, content, collapse_id))
        else:
            logging.info("uid:%d has't device token", receiver)

    for u, appname, content, _ in xg_users:
        xg_push(u.appid, appname, u.xg_device_token, content, {})

    for u, appname, content, _ in hw_users:
        HuaWeiPush.push(u.appid, appname, u.hw_device_token, content)

    for u, appname, content, _ in gcm_users:
        GCMPush.push(u.appid, appname, u.gcm_device_token, content)

    for u, appname, content, _ in ali_users:
        AliPush.push(u.appid, appname, u.ali_device_token, content)

    for u, appname, content, _ in jp_users:
        JGPush.push(u.appid, appname, u.jp_device_token, content)

    for u, appname, content, _ in mi_users:
        MiPush.push(u.appid, appname, u.mi_device_token, content)

    # ios apns
    notifications = []
    for u, appname, content, collapse_id in apns_users:
        sound = 'default'
        payload = Payload(alert=content, sound=sound, badge=u.unread + 1)
        token = u.apns_device_token
        n = Notification(token, payload, collapse_id)
        notifications.append(n)

    if notifications:
        IOSPush.push_peer_batch(u.appid, notifications)

    for u, appname, content, collapse_id in apns_users:
        user.set_user_unread(rds, u.appid, u.uid, u.unread + 1)