Custom SMS gateway

From GroupScript documentation

Jump to: navigation, search

Since version 4.0 site visitors have the ability to receive the coupon code via SMS. Messages are sent via EZTexting.com API- so you need an account there. If you wish to use another API, you have to create a custom SMS gateway.

In order to create new SMS gateway, you must follow these steps:

1. Create a file called example_sms_gateway.php. This file must contain a class which is named after the filename. The class should contain at least these two methods (Note: We are using the name example_sms_gateway.php purely as an example, you have to replace example with real SMS gateway name):

<?php
include_once(APPPATH.'/sms_gateways/base_sms_gateway.php');

class Example_sms_gateway extends Base_sms_gateway {

    public function name() {
         return _('Example.com');
    }

    function send_coupon_code($phone_number, $coupon) {
         parent::send_coupon_code($phone_number, $coupon);
    }

}
?>

2. Then save the file to your system/application/sms_gateways/ folder. Note: Class name must start with an uppercase letter and class name and file names must match. In other words, if file name is example_sms_gateway.php then this is valid:

<?php
class Example_sms_gateway extends Base_sms_gateway {

}
?>

This is not valid:

<?php
class example_sms_gateway extends Base_sms_gateway {

}
?>

Also, always make sure your class extends the Base_sms_gateway class and you include base_sms_gateway.php so that it can inherit all its functions.


3. Write code for send_coupon_code() function

When user wants to receive coupon code via SMS, send_coupon_code() function is called from application/controllers/frontend/profile.php controller

Function send_coupon_code() must return: If message is sent successfully - TRUE If error occurred - STRING, string should contain appropriate error message, if phone number is incorrect "Sending failed, phone number is invalid" or some other error "Sending failed, try again later" Function Parameters:

STRING $phone_number
Doctrine object $coupon


4. There are many ways to send SMS- every API has different method, but we would suggest you to use our made eztexting_sms_gateway class located in system/application/sms_gateways/ folder, copy it and then modify it to fit your needs.

5. Activating SMS Gateway

In order to activate your newly created SMS gateway, you must follow these steps:

  1. Navigate to "Settings" section in backend of site. (Settings->SMS Settings->Global setting)
  2. Choose your sms gateway from dropdown list, it should be displayed as "example_sms_gateway"
  3. Check the "Sms sending enabled" checkbox

Base_sms_gateway functions

sms_limit_reached($phone_number, $coupon)

Limits users from sending unlimited ammount of messages. Limit is set in site settings. Return value: If limit reached error string is returned. If not then FALSE is returned.

Parameters:

String $phone_number
Doctrine object $coupon


log_response($response, $gateway, $coupon)

Logs response for current sending try, should log succesfull and unsuccesfull, without log_response it's impossible to track if the message was sent, so SMS limit per user won't work.

Parameters:

String $response
String $gateway
Doctrine object $coupon

save_user_phone($phone_number)

Saves current user phone number used for message, should be called only if phone number is known to be valid. Parameters:

String $phone_number

get_message_text($coupon)

Prepares SMS subject and message text, containing coupon code, name, last name, campaign title Parameters:

Doctrine object $coupon

Return Values:

array
Personal tools

Groupon Clone