Exemplo n.º 1
0
def startPOST():
    buddyList = []
    respData = {}  # Stores all data that will be returned

    r.flushall()  # Flush all old keys

    for i in xrange(1,6):
        user = request.form["user%s" % i]
        pnum = request.form["pnum%s" % i]
        if user != "" and pnum != "":
            uid = r.incr("global:userid")  # Get a unique id for each user
            buddyList.append({
                "uid": uid,
                "user": user,
                "num": pnum,
                "verified": '0'  # By default, not verified
                })

            r.set("uid:%s:name" % uid, user)
            r.set("uid:%s:number" % uid, pnum)
            r.set("uid:%s:verified" % uid, "0")

            r.set("number:%s:uid" % pnum, uid)  # Reverse index for lookup via number

            r.rpush("global:group", uid)  # Storing uid in list (NOT set as order is important)

    respData["buddyList"] = buddyList

    # Search & rent a number
    code, response = p.search_numbers({"country_code": "1"})  # Fixed to US numbers

    if code != 200:  # Bad request to the api
        abort(500)

    if response["meta"]["total_count"] == 0:  # No more numbers left
        abort(500)

    number = response["objects"].pop()["number"]  # Extract first number from the list
    code, response = p.rent_number({"number": number, "app_id": app.config["PLIVO_APP_ID"]})  # rent the number

    if code != 201:  # Was unable to rent the number
        # TODO: retry for few more times?
        abort(500)

    r.set("global:number", number)  # Storing currently rented number
    respData["number"] = number

    # send verfication for all 5 users
    for uid in r.lrange("global:group", 0, -1):
        code, response = p.send_message({
                            "src": number,
                            "dst": r.get("uid:%s:number" % uid),
                            "text": "Hi, You have been invited to a group chat. Reply with YES to join. Ignore if not interested.",
                            "type": "sms"
                            })

        if code != 202:  # Bad request to api. message queuing failed
            abort(500)

    return render_template("start.html", respData=respData)
Exemplo n.º 2
0
def startGET():
    respData = {
        "number": r.get("global:number"),
        "buddyList": []
    }

    for uid in r.lrange("global:group", 0, -1):
        respData["buddyList"].append({
                "uid": uid,
                "user": r.get("uid:%s:name" % uid),
                "num": r.get("uid:%s:number" % uid),
                "verified": r.get("uid:%s:verified" % uid)
            })

    return render_template("start.html", respData=respData)
Exemplo n.º 3
0
def verfication():
    resp = {
        "status": "success",
        "buddyList": []
    }

    for uid in r.lrange("global:group", 0, -1):
        if r.get("uid:%s:verified" % uid) == "1":
            resp["buddyList"].append({
                    "uid": uid,
                    "verified": True
                })
        else:
            resp["buddyList"].append({
                    "uid": uid,
                    "verified": False
                })

    return jsonify(resp)
Exemplo n.º 4
0
def message():
    src = request.args.get('From', '')
    text = request.args.get('Text', '')
    srcUid = r.get("number:%s:uid" % src)

    if srcUid:
        if r.get("uid:%s:verified" % srcUid) == "1":
            # Number is verified, send the messsage to the group
            uids = set.difference(set(r.lrange("global:group", 0, -1)), set([srcUid]))  # finding a set difference to find all users except the sender
            for uid in uids:
                if r.get("uid:%s:verified" % uid) == "1":
                    code, response = p.send_message({
                            "src": r.get("global:number"),
                            "dst": r.get("uid:%s:number" % uid),
                            "text": "%s: %s" % (r.get("uid:%s:name" % srcUid), text),
                            "type": "sms"
                            })
        else:
            # Number is not verified, check if the incoming message is 'YES'
            if text.strip().upper() == "YES":
                r.set("uid:%s:verified" % srcUid, "1")

    return ""