Twilio with Parse on Android to perform SMS refurbishment

I am trying to send sms check using Twilio Cloud Module in Parse. How should I do it. I don't have an exact guide or usage guide for Android. How to send parameters to the cloud function?

+3


source to share


1 answer


You can create two Cloud Function on Parse, one to send a verification code to the user and verify the user's phone number.

To get started with Cloud Parse features take a look at this tutorial https://www.parse.com/docs/js/guide#cloud_modules

After setting up Parse tool go to main.js file and add two functions

// Include the Twilio Cloud Module and initialize it
var twilio = require('twilio')('<Your Twilio Account Sid>', '<Your Twilio Auth Token>');

// Define the Cloud Functions

Parse.Cloud.define("sendVerificationCode", function(request, response) {
    var verificationCode = Math.floor(Math.random()*999999);
    var user = Parse.User.current();
    user.set("phoneVerificationCode", verificationCode);
    user.save();

    twilio.sendSms({
        From: "<Your Twilio phone number>",
        To: request.params.phoneNumber,
        Body: "Your verification code is " + verificationCode + "."
    }, function(err, responseData) { 
        if (err) {
            response.error(err);
        } else { 
            response.success("Success");
        }
    });
});

Parse.Cloud.define("verifyPhoneNumber", function(request, response) {
    var user = Parse.User.current();
    var verificationCode = user.get("phoneVerificationCode");
    if (verificationCode == request.params.phoneVerificationCode) {
        user.set("phoneNumber", request.params.phoneNumber);
        user.save();
        response.success("Success");
    } else {
        response.error("Invalid verification code.");
    }
});

      



After that, in your Android project, you can request a phone confirmation by following

HashMap<String, Object> params = new HashMap<String, Object>();
params.put("phoneNumber", phoneNumber);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground("sendVerificationCode", params, new FunctionCallback<JSONObject>() {
    public void done(JSONObject response, ParseException e) {
        progressBar.setVisibility(View.GONE);
        if (e == null) {
             Log.d("Response", "no exceptions! " + response.toString());
             // Code sent successfully you have to wait it or ask the user to enter the code for verification
        } else {
             Log.d("Response", "Exception: " + response.toString() + e);
             Toast.makeText(getApplicationContext(),  "Something wrong.  Please try again." + e, Toast.LENGTH_LONG).show(); 
        }
    }
});

      

Then check the code that is (obtained at) / (user entered)

HashMap<String, Object> params = new HashMap<String, Object>();
params.put("phoneNumber", phoneNumber);
params.put("phoneVerificationCode", code);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground("verifyPhoneNumber", params, new FunctionCallback<String>() {
    public void done(String response, ParseException e) {
        progressBar.setVisibility(View.GONE);
        if (e == null) {
            token = response;
            Log.d("Response", "no exceptions! " + response);
            ParseUser.becomeInBackground(token, new LogInCallback() {
                @Override
                public void done(ParseUser parseUser, ParseException e) {
                    if (e == null){
                        Log.d("Response", "no exceptions! ");
                        Intent i = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(i);
                        finish();
                    } else {
                        Log.d("Response", "Exception: " + e);
                        Toast.makeText(getApplicationContext(),"Something wrong.  Please try again." + e, Toast.LENGTH_LONG).show();
                    }
                }
            });
        } else {
            Log.d("Response", "Exception: " + response + e);
            Toast.makeText(getApplicationContext(), "Something wrong.  Please try again.", Toast.LENGTH_LONG).show();
        }
    }
});

      

+2


source







All Articles