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.
Let's build on our previous subscriber tracker and add some extra functionality. We'll call this recipe listener.php
.
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 ); $msg = "We have unsubscribed you. Text 'unpause' to be resubscribed"; break; case "unpause": $sql = "UPDATE subscribers SET status=1 WHERE phone_number='{$phone}'"; $pdo->exec( $sql ); $msg = "We have resubscribed you. Text 'pause' to be unsubscribed"; break; default: $sid = $_POST['SmsSid']; $sql = "UPDATE responses SET answer='{$action}' WHERE phone_number='{$phone}' AND sms_sid='{$sid}'"; break; } print_sms_reply($msg); } function print_sms_reply ($sms_reply){ echo "<?xml version="1.0" encoding="UTF-8"?> "; echo "<Response> <Sms> "; echo $sms_reply; echo "</Sms></Response> "; } ?>
Insert the URL to this page in the SMS Request URL box. Then, any calls that you receive on this number will be processed via listener.php
.
The listener.php
file serves three purposes, all depending on the value of the $action
variable:
$action
is "pause"
, the subscriber is unsubscribed and will no longer be sent any surveys$action
is "unpause"
, the subscriber is resubscribed and will receive surveys again$action
is anything else, it is considered a response and stores the subscriber's answer in the database