Ejemplo n.º 1
0
def addContact():
    '''
    Add a contact
    {
      "sender_id": "4",
      "displayName": "Mohit",
      "phoneNum": "8427434777",
      "countryPhoneCode":"91"  
    }
    '''
    auth_header = request.headers.get('Authorization')
    sub = jwtUtil.parse_decode_auth_token(auth_header, SECRETKEY)

    req_json = request.get_json()
    returnStatus = {"status": "SUCCESS"}
    session = Session()
    try:
        contactJson = req_json['contact']
        sender_id = contactJson['sender_id']
        sender_name = contactJson['sender_name']
        con_displayName = contactJson['display_name']
        con_phone_Num = contactJson['phone_number']
        country_phone_code = contactJson['country_phone_code']

        if (sender_name == sub):
            contact = Users(display_name=con_displayName,
                            phone_number=con_phone_Num,
                            country_code=country_phone_code,
                            contact_id=sender_id)
            session.add(contact)
            session.commit()
            session.refresh(contact)
            #         schema = UserSchema(many = True)
            #         contactList = schema.dump(contact)
            returnStatus.update({
                "user_id":
                contact.user_id,
                "display_name":
                contact.display_name,
                "phone_number":
                contact.phone_number,
                "country_phone_code":
                contact.country_phone_code
            })
            print(contact.user_id)
        else:
            returnStatus = {"status": "FAILURE"}
    except Exception as e:
        print(e)
        returnStatus = {
            "status":
            "Some problem while adding contact, please check for duplicity"
        }
    finally:
        #returnStatus = "Contact {0} added success fully".format(contact.user_id)
        session.close()
    return json.dumps(returnStatus)
Ejemplo n.º 2
0
def register():
    '''
    Add a User
    {
      
      "display_name": "Mohit",
      "phone_number": "8427434777",
      "country_phone_code":"91",
      callback_url : "hdjkfhkdjsf",
      telesign_customer_id : "ghdkjafhjd",
      telesign_api_key : "jsdhfjkhsdfkj",
      password : "******"
          }
    '''
    req_json = request.get_json()
    returnStatus = "status: User created successfully"
    session = Session()
    try:
        userJson = req_json['contact']
        user_displayName = userJson['display_name']
        phone_Num = userJson['phone_number']
        country_phone_code = userJson['country_phone_code']
        callback_url = userJson['callback_url']
        telesign_customer_id = userJson['telesign_customer_id']
        telesign_api_key = userJson['telesign_api_key']
        password = userJson['password']

        newUser = Users(display_name=user_displayName,
                        phone_number=phone_Num,
                        country_code=country_phone_code,
                        callback_url=callback_url,
                        ts_cust_id=telesign_customer_id,
                        ts_api_key=telesign_api_key,
                        password=password)
        session.add(newUser)
        session.commit()
    except Exception as e:
        print(e)
        returnStatus = "status: Some problem while registering user, please check for duplicity"
    finally:
        returnStatus = "status: Registration successfull"
        #returnStatus = "Contact {0} added success fully".format(contact.user_id)
        session.close()
    return json.dumps(returnStatus)
Ejemplo n.º 3
0
def sendMessage():
    '''
    example post request:
        {
          "currentUser_displayName": "Manmohan",
          "receiver_displayName": "Bala",
          "sender_phoneNum": "9023051078",
          "receiver_phoneNum": "7009600580",
          "receiver_countryPhoneCode":"91",
          "message":"Be grateful to the great Thanos."  
        }
    '''
    req_json = request.get_json()
    ''' 1    
    sender_displayName = req_json['currentUser_displayName']
    receiver_displayName = req_json['receiver_displayName']
    sender_phoneNum = req_json['sender_phoneNum']
    '''
    sender_id = req_json['sender_id']
    receiver_id = req_json['receiver_id']
    receiver_countryPhoneCode = req_json['receiver_countryPhoneCode']
    receiver_phoneNum = req_json['receiver_phoneNum']
    message = req_json['message']
    '''
    1. Hit the user table get customerId API key
    2. send the sms
    3. save message in chat table with sender and receiver
    4. optional: do a getrequest to check whether user has received the message    
    '''
    # fetching from the database
    session = Session()
    ''' 1
        user_objects = session.query( Users.telesign_customer_id, Users.telesign_api_key, Users.user_id ).\
        filter(Users.display_name.like(sender_displayName) & Users.phone_number.like(sender_phoneNum)).all()
    '''
    user_objects = session.query( Users.telesign_customer_id, Users.telesign_api_key, Users.user_id ).\
    filter(Users.user_id.like(sender_id)).all()

    APIkey = user_objects[0].telesign_api_key
    customerID = user_objects[0].telesign_customer_id
    ''' 2
     sender_id = user_objects[0].user_id
    '''
    '''
    3
    fetch receiver_id_fk by querying user table 
    Save message data 
    
    receiver_objects = session.query( Users.user_id ).\
    filter(Users.display_name.like(receiver_displayName) & Users.phone_number.like(receiver_phoneNum)).all()
    receiver_id = receiver_objects[0].user_id
    '''

    iMessage = Chat(message=message,
                    sender_id_fk=sender_id,
                    receiver_id_fk=receiver_id)

    session.add(iMessage)
    session.commit()

    # serializing as JSON
    session.close()
    #customerID = '0E09D1B5-86CB-4E89-828E-CA61C850BA06'
    #APIkey = 'M4hajYKMhoXrFnXsg84yMm+FeeSU8Jjoi3JMZVOiMysXXpPvnBv+pxNTss++zrobMxGSc8lcN8ib4elemPPBHw=='
    url = 'https://rest-ww.telesign.com/v1/messaging'
    #phoneNumber = '919023051078'
    #message = 'Hi I want to order pepperoni pizza'
    messagetype = 'ARN'

    data = {
        'phone_number': receiver_countryPhoneCode + receiver_phoneNum,
        'message': message,
        'message_type': messagetype
    }

    encode = (customerID + ':' + APIkey).encode('utf-8')
    b64encoded = base64.urlsafe_b64encode(encode).decode('ascii')
    header = {'Authorization': 'Basic ' + b64encoded}

    response = post(url, data=data, headers=header)
    jsonResponse = json.loads(response.content)
    print(jsonify(jsonResponse))
    '''
    json to be returned having
    Message-sent: yes
    Timestamp : dd-mm-yy hh:mm
    Error: only if there is some error otherwise empty
    
    In DB save:
    message
    from phone
    To phone
    Timestamp    
    '''
    return jsonify(jsonResponse)