(EN) SMS Integration with other SMS services (providers) in OzApp plugin

·
9 min read

Desculpe-nos, mas este texto está apenas disponível em Inglês Americano y Russo.

Our clients often use requests to see if there is a possibility of alternative SMS integration instead the built-in Twilio and SMSC.
Therefore, in this post we will describe how to create a new way to send SMS.

We use Object-Oriented PHP with class to write the code we need. But experienced php developers can use the functional approach.
So, let’s create a custom class for sending sms and call it for example CustomSMSProvider. And add a few basic properties and methods to it, which we will write about below.
Code example:

class CustomSMSProvider {
public $provider = '';
public function getOptions($settings) {}
public function send($to, $message, $sendTime = null) {}
public function send_book($from,$to,$text,$id,$sendTime = 0) {}
public function send_otp($response, $to, $message) {}
public function init() {}
}

Now let’s analyze the properties and methods of our class.

public $provider – the value of the property is the value of the option – this is how the SMS will be sent (we can choose this method in the plugin settings). You can enter any value here, for example: as_custom_sms
public function getOptions($settings) {} – in this method, you need to describe the provider’s SMS settings. These settings will then be displayed on the SMS tab of the plugin settings.
this method filters the values ​​of all plugin options, so you need to be careful when writing your options. First, it always returns $settings. Second, the sms options are written to the $settings[‘sms’][‘options’] array which contains all the options on the sms tab.
Perhaps later we will describe how the array of all plugin options is formed, but now we will describe only what is needed for SMS.
Each plugin option is an array that contains the following options:

  • ‘title’ – string option name (required)
  • ‘description’ – string option description (optional)
  • ‘order’ – number option order (required)
  • ‘fields’ – array description of the settings associated with this option, contains an array of arrays with the following keys:
      • ‘name’ – string is the name of the option that we will substitute in get_option to get the value (required)
      • ‘value’ – mix the option value that get_option will return, (required)
      • ‘type’ – option types. we only use switch and input (required)
      • ‘multiple’ – bool – multiple choices (optional),

    ‘values’ – only for option type switch. array of arrays of values in the format [‘label’ => ”, ‘value’ => ”] (optional),

  • ‘fields’ – only for option type switch. array of arrays that will contain nested options that will be visible in the interface if switch is active

Example of code for adding options, using Twilio as an example:


public function getOptions($settings) {
$opt = [
'title' => 'Twilio', // sms method name
'description' => '',
'order' => 40,
// in fields, be sure to add an array of options of type switch
'fields' => [
[
'name' => 'oz_smsType', // here always oz_smsType
'value' => get_option('oz_smsType') == $this->provider, // here always this condition
'type' => 'switch', // here always switch
'multiple' => false, // here always false
// in values always these values
'values' => [
[
'label' => '',
'value' => $this->provider
]
],
// here we will describe our options. One option, one array. Using Twilio as an example, three options are needed: SID, Token, Account Phone Number or Alphanumeric Sender ID
'fields' => [
[
'title' => 'SID',
'description' => '',
'order' => 10,
'fields' => [
[
'name' => 'oz_twilio_sid', // You can come up with any option name. we will write oz_twilio_sid
'value' => get_option('oz_twilio_sid', ''),
'type' => 'input',
'multiple' => false,
'values' => [],
],
]
],
[
'title' => 'Token',
'description' => '',
'order' => 10,
'fields' => [
[
'name' => 'oz_twilio_token',
'value' => get_option('oz_twilio_token', ''),
'type' => 'input',
'multiple' => false,
'values' => [],
],
]
],
[
'title' => 'Account Phone Number or Alphanumeric Sender ID',
'description' => '',
'order' => 10,
'fields' => [
[
'name' => 'oz_twilio_sender',
'value' => get_option('oz_twilio_sender', ''),
'type' => 'input',
'multiple' => false,
'values' => [],
],
]
],
]
]
]
];
$settings['sms']['options'][] = $opt;
return $settings;
}

Method that will send SMS:

public function send($to, $message, $sendTime = null) {}
Method variables:
$to – recipient’s phone number
$message – message text
$sendTime – send time (optional)

You must describe the logic of this function yourself using the API of your SMS provider. If successful, the function should return a response from your SMS provider. In case of an error, an array with keys: error – true, text – error text

In our example, you will see an example of submitting code using Twilio:


public function send($to, $message, $sendTime = null) {
$sid = get_option('oz_twilio_sid');
$token = get_option('oz_twilio_token');
$auth = base64_encode($sid . ":" . $token);
$from = get_option('oz_twilio_sender');
$data = array(
"From" => $from,
"To" => $to,
"Body" => do_shortcode($message),
);
$url = "https://api.twilio.com/2010-04-01/Accounts/".$sid."/Messages.json";
$sms = wp_remote_post( $url, array(
'headers' => [ 'Authorization' => "Basic $auth" ],
'body' => $data,
) );
if (is_wp_error($sms)) {
$sms = [
'error' => true,
'text' => $sms-> get_error_message(),
];
}
else {
$sms = json_decode(wp_remote_retrieve_body($sms), true);
if ($sms['status'] && $sms['status'] == 'queued') {
return $sms;
}
else {
$sms = [
'error' => true,
'text' => $sms['message'],
'response' => $sms
];
}
}
return $sms;
}

Method of sending SMS at the time of creating a new record

public function send_book($from,$to,$text,$id,$sendTime = 0) {}
It must contain the send method. And optionally, you can describe further actions.
Method Variables:
$from – optional,
$to – recipient’s phone,
$text – message,
$id – appointment id,
$sendTime – optional

Code example:


public function send_book($from,$to,$text,$id,$sendTime = 0) {
$this-> send($to, $text);
}

Method of sending SMS code, before booking, to confirm the phone number

public function send_otp($response, $to, $message) {}
Must return an array with key success and value true or send method error.

Code example:


public function send_otp($response, $to, $message) {
$response = $this-> send($to, $message);
if (isset($response['error']) && $response['error']) {
return $response;
}
else {
return [
'success' => true,
];
}
}

The method in which method call hooks are written

public function init() {}
Here you need to bind to the SMS sending hooks that our plugin uses its own sending method. almost always this code will be like in the example:

Code example:


public function init() {
add_filter('book_oz_plugin_settings', [$this, 'getOptions']);
add_filter('book_oz_otp_send_'.$this-> provider, [$this,'send_otp'], 10, 3);
add_action('book_sendingSMS_'.$this-> provider, [$this,'send_book'],10,5);
add_action('book_sendingSMSRemind_emp_'.$this-> provider, [$this, 'send'], 10,3);
}

The class itself can be saved in a separate file or added directly to your theme’s functions.php.
After that, you need to create an instance of the class and call the init method
Sample code for functions.php (class in a separate file in the same folder as functions.php):

require_once(dirname(__FILE__).'/CustomSMSProvider.php');
$customsms = new CustomSMSProvider();
$customsms->init();

That’s all. If everything is done correctly, then in the plugin settings in the SMS tab you will see the settings for your new method and receive SMS in this way when booking.
Example of this code you can find at GitHub

Back to blog