Book Image

Twilio Cookbook: Second Edition

By : Roger Stringer
Book Image

Twilio Cookbook: Second Edition

By: Roger Stringer

Overview of this book

Table of Contents (20 chapters)
Twilio Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Listening to user responses and commands


This recipe is the brain of the system; it handles users sending in responses and also handles what to do if a user wants to be unsubscribed. This script will listen on a phone number and do exactly that.

The listener.php file replaces tracker.php, and it will handle pausing, resuming, and the responses.

Getting ready

The complete source code for this recipe can be found in the Chapter3/ folder.

How to do it...

Let's build on our previous subscriber tracker and add some extra functionality. We'll call this recipe listener.php.

  1. Upload the listener.php file (with the following content) on your web server.

    <?php
    include("config.php");
    include("pdo.class.php");
    $pdo = Db::singleton();
    if( isset($_POST['Body']) ){
      $phone = $_POST['From'];
      $phone = str_replace('+','',$phone);
      $action = strtolower($_POST['Body']);
      switch($action){
        case "pause":
          $sql = "UPDATE subscribers SET status=0 WHERE phone_number='{$phone}'";
          $pdo->exec( $sql...