Using Plivo and Parse

Parse is a Mobile Backend as a Service provider that eliminates the need to write extensive server code or maintain servers for your mobile app.

Send SMS

Using Parse with Plivo is a simple integration that involves calling a PFCloud function in Objective C (for iOS devices) and putting a matching function on Parse Cloud which receives the message data (sender, recipient, message body) from your app and passes it to Plivo to send the message.

Objective C Code

Here is the Objective C Code that would be used in your iOS app:

1
2
3
4
5
6
7
8
9
10
11
[PFCloud callFunctionInBackground:@"sendSmsPlivo"
    withParameters:@{ @"phonenumber" : phoneNumberString, @"messagebody" : alertMessage}
    block:^(id object, NSError *error) {
        if (myShowPopup){
          [[[UIAlertView alloc] initWithTitle:myPopupTitle
                                      message:myPopupMessage
                                     delegate:nil
                            cancelButtonTitle:@"Ok"
                            otherButtonTitles:nil, nil] show];
        }
    }];

 
The code above expects the following variables to be provided:

Variable Description
phoneNumberString The phone number of the recipient to which your app user will send a message
alertMessage The body of the text message to be sent
myPopupTitle The title of the popup to show the user when message has been sent
myPopupMessage The message body to show in the popup

Parse Cloud code

Below is the code to put on Parse Cloud which would accept the request from your iOS app above and pass the message data to Plivo to send the message.

Replace Plivo_AUTH_ID, Plivo_AUTH_TOKEN, and Plivo_Phone_Number with the AuthID, AuthToken, and a Phone number from your Plivo dashboard. The phone number is the number from which messages will be sent. If you don’t already have a number to use, you can buy a Plivo phone number.

Notice that your Plivo AuthID and AuthToken are defined in the code on Parse Cloud and thus are safe and inaccessible to your app’s users.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Parse.Cloud.define("sendSmsPlivo", function(request, response) {
  var auth_id = "Plivo_AUTH_ID";
  var auth_token = "Plivo_AUTH_TOKEN";
  var plivo_number = "Plivo_Phone_Number";

  Parse.Cloud.httpRequest({
    method: "POST",
    headers: {'Content-Type': 'application/json',},
    url: 'https://'+auth_id+':'+auth_token+'@api.plivo.com/v1/Account/'+auth_id+ '/Message/',
    body: {
      "src" : plivo_number,
      "dst" : request.params.phonenumber,
      "text" : request.params.messagebody
    },
    success: function(httpResponse) {
      console.log(httpResponse.text);
    },
    error: function(httpResponse) {
      console.error('Request failed with response code ' + httpResponse.status);
    }
  });
});

 

Make Call

Using Parse with Plivo is a simple integration that involves calling a PFCloud function in Objective C (for iOS devices) and putting a matching function on Parse Cloud which receives the message data (from number, to number, answer_url) from your app and passes it to Plivo to make the call.

Objective C Code

Here is the Objective C Code that would be used in your iOS app:

1
2
3
4
5
6
7
8
9
10
11
[PFCloud callFunctionInBackground:@"makeCallPlivo"
    withParameters:@{ @"phonenumber" : phoneNumberString }
    block:^(id object, NSError *error) {
        if (myShowPopup){
          [[[UIAlertView alloc] initWithTitle:myPopupTitle
                                      message:myPopupMessage
                                     delegate:nil
                            cancelButtonTitle:@"Ok"
                            otherButtonTitles:nil, nil] show];
        }
    }];

 
The code above expects the following variables to be provided:

Variable Description
phoneNumberString The phone number of the recipient to which your app user will make a calling
myPopupTitle The title of the popup to show the user when message has been sent
myPopupMessage The message body to show in the popup

Parse Cloud code

Below is the code to put on Parse Cloud which would accept the request from your iOS app above and pass the message data to Plivo to send the message.

Replace Plivo_AUTH_ID, Plivo_AUTH_TOKEN, and Plivo_Phone_Number with the AuthID, AuthToken, and a Phone number from your Plivo dashboard. The phone number is the number from which calls will be made. If you don’t already have a number to use, you can buy a Plivo phone number.

Notice that your Plivo AuthID and AuthToken are defined in the code on Parse Cloud and thus are safe and inaccessible to your app’s users.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Parse.Cloud.define("makeCallPlivo", function(request, response) {
  var auth_id = "Plivo_AUTH_ID";
  var auth_token = "Plivo_AUTH_TOKEN";
  var plivo_number = "Plivo_Phone_Number";

  Parse.Cloud.httpRequest({
    method: "POST",
    headers: {'Content-Type': 'application/json',},
    url: 'https://'+auth_id+':'+auth_token+'@api.plivo.com/v1/Account/'+auth_id+ '/Call/',
    body: {
      "from" : plivo_number,
      "to" : request.params.phonenumber,
      "answer_url" : "https://s3.amazonaws.com/static.plivo.com/answer.xml"
    },
    success: function(httpResponse) {
      console.log(httpResponse.text);
    },
    error: function(httpResponse) {
      console.error('Request failed with response code ' + httpResponse.status);
    }
  });
});

 

Is anything on this page unclear? Suggest edits and help us improve our documentation!