www.it-ebooks.info Twilio Cookbook Second Edition Over 70 easy-to-follow recipes, from exploring the key features of Twilio to building advanced telephony apps Roger Stringer BIRMINGHAM - MUMBAI www.it-ebooks.info Twilio Cookbook Second Edition Copyright © 2014 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information First published: September 2013 Second edition: March 2014 Production Reference: 1200314 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78355-065-4 www.packtpub.com Cover Image by Rick Cartledge (rick_bc@shaw.ca) www.it-ebooks.info Credits Author Project Coordinator Roger Stringer Sanchita Mandal Reviewers Proofreaders Zakir Hyder Amy Johnson Tim Rogers Bob Phillips Prateek Sachdev Indexer Rekha Nair Acquisition Editors Joanne Fitzpatrick Sam Birch Production Coordinator Adonia Jones Content Development Editor Poonam Jain Cover Work Adonia Jones Technical Editors Novina Kewalramani Shweta S Pant Copy Editors Alisha Aranha Mradula Hegde Gladson Monteiro www.it-ebooks.info About the Author Roger Stringer has been a PHP developer since 2001 and has been working on projects of all sizes for companies all over the world He has formed several start-ups over the years and most of them have been powered by Twilio to provide services When not working on the web, Roger can be found reading, cooking, and spending time with his daughter, Kaitlyn, at the local farmer's markets Roger is the founder of The Interviewer, a Twilio-powered startup, which makes interviewing more efficient by helping with scheduling, contact management, and conducting and recording interviews I'd like to thank my wife, Patsy, and our daughter, Kaitlyn, for having the patience to let me get this book written, as I spent many hours behind a computer typing Also, I wish to thank Rob and the Twilio crew for answering any questions that came up as I worked on the various topics covered I also want to thank Jason and the crew at Copter Labs and Gary and the VeriCorder team www.it-ebooks.info About the Reviewers Zakir Hyder graduated in Computer Science from Southeast University and is pursuing his MS in Computer Science and Engineering from North South University in Bangladesh He has been building websites and social networking apps since 1999 He has also been developing Facebook apps since 2007 He gave his talk at Facebook Developer Garage Dhaka He has developed websites that were deeply integrated with Facebook, Twitter, LinkedIn, and YouTube APIs He writes tests with RSpec, Cucumber, and Capybara with Selenium He follows Agile methodologies of software development and is especially fond of scrum and standup Besides his full-time job, Zakir writes his blog at blog.jambura.com You can follow him on Twitter @zakirhyder and on his LinkedIn profile at bd.linkedin.com/in/zakirhyder He lives in Bangladesh with his wife, Fathema, and his son, Arham Currently, he is a senior software engineer at somewherein Ltd (http://www.somewherein.net/) He has also worked on Getting Started with Citrix® CloudPortal, Packt Publishing I would like to thank my wife for taking care of our three-month old boy while I reviewed the book I'd also like to thank Sanchita Mandal for being patient with me Tim Rogers is a developer based in London, UK Ever since he learned to work with Twilio while building a cloud phone support system for his employer, GoCardless (an online payments start-up) he has done freelance work on the platform for a number of SMEs and has spoken about his experiences at Twilio's TwilioCon Europe conference www.it-ebooks.info Prateek Sachdev grew up in Bhopal, Madhya Pradesh He is a prefinal year student of International Institute of Information Technology, Hyderabad, India He is pursuing his B.Tech in Computer Science His interests lie in Artificial Intelligence, computer networking, concurrency and parallelism, and operating systems He is currently working on projects on Distributed Systems He would like to work on books on Python and Algorithms When he isn't glued to a computer screen, he spends time playing the guitar and listening to music www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy Get in touch with us at service@packtpub.com for more details At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library Here, you can access, read and search across Packt's entire library of books Why Subscribe? ff Fully searchable across every book published by Packt ff Copy and paste, print and bookmark content ff On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books Simply use your login credentials for immediate access www.it-ebooks.info www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Into the Frying Pan Introduction 5 Adding two-factor voice authentication to verify users Using Twilio SMS to set up two-factor authentication for secure websites 10 Adding order verification 13 Adding the Click-to-Call functionality to your website 15 Recording a phone call 17 Setting up a company directory 21 Setting up Text-to-Speech 26 Chapter 2: Now We're Cooking 31 Chapter 3: Conducting Surveys via SMS 49 Introduction Tracking account usage Screening calls Buying a phone number Setting up a voicemail system Building an emergency calling system Introduction Why use PDO instead of the standard MySQL functions? Letting users subscribe to receive surveys Building a survey tree Sending a survey to your users Adding tracking for each user Listening to user responses and commands Building a chart of responses www.it-ebooks.info 31 32 36 39 42 45 49 50 52 54 58 60 62 64 Chapter 13 // wrap the phone number or client name in the //appropriate TwiML verb // by checking if the number given has only digits and format symbols if (preg_match("/^[\d\+\-\(\) ]+$/", $number)) { $numberOrClient = "" $number ""; } else { $numberOrClient = "" $number ""; } ?>