-2

I would like to know how to get user's number or some data to update the database to be opted-out in my server using callback from Twilio when user replies STOP via SMS.

I'm using PHP.

How can I solve this issue?

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Ivan
  • 1,221
  • 2
  • 21
  • 43
  • 1
    What have your tried so far? – Demon Sep 28 '18 at 21:15
  • @Demon I followed this https://www.twilio.com/blog/2016/08/receive-sms-php-twilio.html my server replied to my cellphone, also I did "stop" to unsuscribe and did "start" to reset, anyway I dont know how to detect "stop" before to do a callback and update the database. – Ivan Sep 28 '18 at 21:24
  • 3
    @Ivan It's (essentially) gonna be `if($_POST['Body'] === 'STOP') { // do something }` in your handler. (Slightly more complex than that - you'll want to handle extra spaces using `trim()`, make it case-insensitive, etc., but this is the *general* idea...) – ceejayoz Sep 29 '18 at 02:09
  • @ceejayoz I was thinking that too, but how to get telephone's number, I needed any kind of data from user so I can lookup to match in the database before to do opt-out. – Ivan Sep 29 '18 at 03:30
  • @Ivan You really need to go back and re-read that tutorial. The sending phone number is right there in the code samples shown. – ceejayoz Sep 29 '18 at 04:20
  • @ceejayoz I know, there is a $number in TwiML, I expect I may get different data than $number, I couldn't find it in the documentation. – Ivan Sep 29 '18 at 05:12
  • 2
    If a `$number` texts you `STOP`, stop sending to it entirely. You should have a database of opted-out numbers that you check before sending anything. The full content of a Twilio payload is [well documented](https://www.twilio.com/docs/sms/twiml#twilios-request-to-your-application). – ceejayoz Sep 29 '18 at 17:40

1 Answers1

0

I managed to compare $_POST['Body'] with some filter functions, here is my complete code:

// You need to include Twilio library path here... for example require_once '/path/to/twilio-lib/autoload.php';
use Twilio\Twiml;

global $wpdb;

$response = new Twiml;


    if (!empty($_POST))
    {
        $number = $_POST['From'];
        $body = $_POST['Body'];
        $default = "Hi " .$number. ", I'm a bot, please do not reply until you will get an update in some day. Have a nice day!";   


        $result = preg_replace("/[^A-Za-z0-9]/u", " ", $body); 
        $result = trim($result); 
        $result = strtolower($result); 



        //words to match and $result must be lowercase to define.
        if ($result == 'stop' || $result == 'stopall' || $result == 'cancel' || || $result == 'end') || $result == 'quit' || $result == 'unsubscribe'{
         //Your function to update database with $number

        }
        else if ($result == 'start' || $result == 'yes' || $result == 'unstop'){
            $response->message('Thanks for re-subscribing');
           //Your function to update database, again with $number
        }   
         else {
            $response->message($default);
        }

        print $response;
    }
Ivan
  • 1,221
  • 2
  • 21
  • 43