Reminders change and sometimes you have to cancel them ahead of time. This recipe will enable the system to handle that.
Ok, now we're going to build support for deleting reminders.
Services/
folder to your website.sql.sql
to your databaseconfig.php
to your website and make sure the following variables are set:<?php session_start(); $accountsid = '';//YOUR TWILIO ACCOUNT SID $authtoken = '';//YOUR TWILIO AUTH TOKEN $fromNumber = '';//PHONE NUMBER CALLS WILL COME FROM $dbhost = '';//YOUR DATABASE HOST $dbname = '';//YOUR DATABASE NAME $dbuser = '';//YOUR DATABASE USER $dbpass = '';//YOUR DATABASE PASS ?>
listener.php
with the help of the following code:<?php include('Services/Twilio.php'), include("config.php"); include("functions.php"); if( isset($_POST['Body']) ){ $phone = $_POST['From']; $body = $_POST['Body']; $from = $_POST['FromCity'].', '.$_POST['FromState']; $body = strtolower( $body ); $keywords = explode(" ",$body); $key = $keywords[0]; unset( $keywords[0] ); $keywords = implode(" ",$keywords); $key = strtolower( $key ); //actions if( $key == 'showme' ){ $lines = array(); $curtime = strtotime("+1 hour"); $sql = "SELECT * FROM reminders where `timestamp` > $curtime AND notified = 0"; $res = $pdo->query( $sql ); while( $row = $res->fetch() ){ $lines[] = $row['message'].' - '.date('d/m/Y @ h:i A',$row['timestamp']); } print_sms_reply ($lines); }else{ $reminder = explode(' - ',$body); $msg = $reminder[0]; $action = $reminder[1]; $actions = explode(" ",$action); if( $actions[0] == 'cancel' ){ $pdo = Db::singleton(); $pdo->exec("DELETE reminders WHERE `message`='{$msg}' AND `phone_number`='{$phone}';"); print_sms_reply("Your reminder has been cancelled."); }else if( $actions[0] == 'add' ){ }else{ //new reminder $timestamp = strtotime( $action ); $sql = "INSERT INTO reminders SET `message`='{$msg}',`timestamp`='{$timestamp}',`phone_number`='{$phone}'"; $pdo = Db::singleton(); $pdo->exec($sql); $qid = $pdo->lastInsertId(); print_sms_reply("Your reminder has been set."); } } //end actions }
functions.php
with the following code:<?php function print_sms_reply ($sms_reply){($sms_reply){ echo "<?xml version="1.0" encoding="UTF-8"?> "; echo "<Response> "; if( !is_array($sms_reply) ){ echo '<Sms>'.$sms_reply.'</Sms>'; }else{ $cnt = count($sms_reply); $i = 1; foreach($sms_reply as $line){ $line = $line." (".$i."/".$cnt.")"; echo '<Sms>'.$line.'</Sms>'; $i++; } } echo "</Response> "; }
Insert http://mysite.com/listener.php
in the SMS Request URL field. Then, any calls that you receive at this number will be processed via listener.php
.
In steps 1 and 2, we downloaded and installed the Twilio Helper Library for PHP; this library is at the heart of your Twilio-powered apps.
In step 3, we loaded our database schema into our database.
In step 4, we uploaded config.php
that contains our authentication information to communicate with Twilio's API.
In steps 5 and 6, we uploaded listener.php
and functions.php
, which records all incoming texts.
In step 7, we told our Twilio number to direct all SMS messages to listener.php
.
When we receive a text, we check it; if it contains the keyword showme
, we return a list of pending reminders.
Then we check to see if the cancel
keyword was sent with the reminder.
So change tires – cancel
would check for a pending reminder with my phone number and the message of "change tires" and delete it.