Ejemplo n.º 1
0
def login():
    #First recheck if user is a valid google user
    try:
        token = request.form.get("idtoken")
        client_id = environ.get("GOOGLE_CLIENT_ID")
        infoid = id_token.verify_oauth2_token(token, google.auth.transport.requests.Request(), client_id)
        if infoid['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
            raise ValueError('Wrong issuer.')
        userid = infoid['sub']
        Users = users()
        #print(Users)
        user = Users.get_by("email",request.form.get("email"))
        #Check if user is administrator, if not just force logout
        if user is not None and (user["role"] == 'administrator'):
            #Creates the session
            session["user"] = { "username": user["name"], "email": user["email"] }
            return "session_created"
        else:
            #Destroy session in the server
            session.pop("user",None)
            #Destroy session in the client
            return "destroy_session"
    except ValueError:
        return "destroy_session"
        pass
Ejemplo n.º 2
0
def answer():
    global nexmo_client
    global Conferences
    #get the incoming call number
    phone = request.args.get('from')
    #Identify if the number is a valid customer, agent or supervisor
    Users = users()
    anonymus = Users.get_by(field='phone', value=phone)
    if anonymus is None:
        print({"error": "Invalid participant"})
        return jsonify({"error": "Invalid participant"}), 400
    elif anonymus["role"] == 'customer':
        #Is a customer
        customer = anonymus
        #Check for available agents and supervisors
        agents = Users.filterby({'status':'active','role':'agent'}, single=False)
        #select random agent
        agent = None
        if agents is not None:
            agent = random.choice(agents)
        if agent is None:
            #Return custom NCCO
            print("return speech NCCO")
            return jsonify([
                {
                    "action": "talk",
                    "text": "Thanks for calling to the nexmo conference. In this moment all agents are busy. Please try later"
                }
            ])
        else:
            #Change status
            Users.update({"status":"busy"}, agent["id"])
            #Create the conference in function of the agent
            Conferences[agent["phone"]] = conference()
            Conferences[agent["phone"]].add_participant({'number':agent["phone"], 'role':agent["role"]})
            #Get the customer leg
            customer_leg = request.args.get('uuid')
            Conferences[agent["phone"]].add_participant({'number':customer["phone"], 'role':customer["role"], 'leg': customer_leg})
            #Call the agent with the agent ncco and return the customer NCCO to the customer
            response = nexmo_client.create_call(
                {
                    "to": [{
                        "type": "phone",
                        "number": agent["phone"]
                    }],
                    "from": {
                        "type": "phone",
                        "number": environ.get('NEXMO_NUMBER')
                    },
                    "ncco": Conferences[agent["phone"]].agent_ncco(),
                    "eventUrl": [
                        "{url_root}/webhooks/events".format(url_root=environ.get("SITE_URL"))
                    ]
                }
            )
            print("Return customer ncco")
            return jsonify(Conferences[agent["phone"]].customer_ncco())
    else:
        print("Simple answer")
        return "Answer Response", 200
Ejemplo n.º 3
0
def role_change():
    name = "{0} / {1}".format(request.form.get("first_name"), request.form.get("last_name"))
    role = request.form.get("role")
    phone = request.form.get("phone")
    email = request.form.get("email")
    status = request.form.get("status")
    id = request.form.get("id")
    Users = users()
    #if request.method == "POST":
    if request.form.get("_method") == "PUT":
        Users.update({"name": name, "role": role, "phone": phone, "email": email, "status": status}, id)
    else:
        Users.add(User(name = name, role = role, phone = phone, email = email, status = status))
        
    return redirect(url_for('roles'))
Ejemplo n.º 4
0
def events():
    global Conferences
    req = request.get_json()
    if(req is None):
        req = dict(request.args)
    print(req)
    if "status" in req:
        #If answered and phone is from agent. Then Check the conference for agent, call the supervisor number if available 
        #And send him the supervisor ncco
        if req["status"] == "answered":
            phone = req["to"]
            #Check if agent phone - for this a conference must exist for the agent.
            if phone in Conferences:
                #We check for supervisor if available
                Users = users()
                supervisor = Users.filterby({'status':'active','role':'supervisor'})
                if supervisor is not None:
                    #Make supervisor reservation
                    Users.update({"status":"busy"}, supervisor["id"])
                    #Add supervisor to conference
                    Conferences[phone].add_participant({'number':supervisor["phone"], 'role':supervisor["role"]})
                    #Add Agent Leg to agent participant
                    agent_leg = req['uuid']
                    Conferences[phone].set_leg(agent_leg, phone)
                    #Make call to supervidor to connect him to conference
                    response = nexmo_client.create_call(
                        {
                            "to": [{
                                "type": "phone",
                                "number": supervisor["phone"]
                            }],
                            "from": {
                                "type": "phone",
                                "number": environ.get('NEXMO_NUMBER')
                            },
                            "ncco": Conferences[phone].supervisor_ncco(),
                            "eventUrl": [
                                "{url_root}/webhooks/events".format(url_root=environ.get("SITE_URL"))
                            ]
                        }
                    )
        elif req["status"] in ["unanswered","completed","rejected"]:
            phone = req["to"]
            Users = users()
            user = Users.filterby({'phone':phone})
            if user is not None:
                if user["role"] in ["supervisor","agent"]:
                    Users.update({"status":"active"}, user["id"])
                    if req["status"] == "completed":
                        if user["role"] == "agent":
                            #If conversation is completed and role is agent. Then save the conference to database
                            conf = conferences()
                            conf.add(Conference(name = Conferences[phone].name, date = req["timestamp"], audio='', conference_uuid=req["conversation_uuid"] ))
                            conf_item = conf.get_by_name(Conferences[phone].name)
                            parts = Conferences[phone].get_participants()
                            for part in parts:
                                user_item = Users.filterby({'phone': part["number"] })
                                leg = part["leg"] if "leg" in part else ""
                                conf.add_participant(Participant(conference_id=conf_item["id"], user_id=user_item["id"], user_leg=leg))
                            #Restart the conference value 
                            Conferences[phone] = None
    if "recording_url" in req:
        conf = conferences()
        conf_item = conf.get_by("conference_uuid", req["conversation_uuid"], single=True)
        conf.update({"audio": req["recording_url"]},conf_item["id"])

    return "Events received", 200
Ejemplo n.º 5
0
def role_delete(key):
    Users = users()
    Users.delete(key)
    return "deleted"
Ejemplo n.º 6
0
def role(key = None):
    if key is None:
        return render_template('user.html', action="add")
    else:
        Users = users()
        return render_template('user.html', action="edit", role = Users.get_by("id",key))
Ejemplo n.º 7
0
def roles():
    Users = users()
    return render_template('user.html', action="list", users = Users.get_all())
Ejemplo n.º 8
0
parser = argparse.ArgumentParser()
subparser = parser.add_subparsers()

parser_admin = subparser.add_parser('create')
parser_admin.add_argument('role', nargs='?', help='Admin creation')
parser_admin.set_defaults(parser='create')

args = parser.parse_args()

if vars(args) == {}:
    parser.print_help()
    sys.exit()

if args.parser == 'create':
    if(args.role == 'admin'):
        name = input('Insert the name: ')
        role = 'administrator'
        email = input('Insert the email: ')
        phone = input('Insert the phone: ')
        status = "active"
        #We use app context, Because we are using flask sqlalchemy and not sqlalchemy individually
        #But if we put from both doesn't bad just happends
        #if environ.get("DATABASE_ENGINE") == "sqlalchemy":
        with app.app_context():
            Users = users()
            Users.add(User(name=name, role=role, email=email, phone=phone, status=status))
    elif(args.role == 'user'):
        print('Hi: user')
    else:
        print('Invalid role')