PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 18, 2022

[FIXED] How to do a callback with user's number when user replies STOP via SMS using Twilio?

 November 18, 2022     php, twilio, twilio-api, twilio-php     No comments   

Issue

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?


Solution

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;
    }


Answered By - Ivan
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing