Twilio Cookbook Over 60 easy-to-follow recipes ranging from walking you through key features of Twilio's API to building advanced telephony applications Roger Stringer BIRMINGHAM - MUMBAI Twilio Cookbook Copyright © 2013 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 Production Reference: 1300813 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78216-606-1 www.packtpub.com Cover Image by Abhishek Pandey ([email protected]) Credits Author Project Coordinator Roger Stringer Apeksha Chitnis Reviewer Proofreaders Jakir Hayder Stephen Copestake Maria Gould Acquisition Editors Rukhsana Khambatta Indexer Sam Birch Hemangini Bari Lead Technical Editor Production Coordinator Dayan Hyames Arvindkumar Gupta Technical Editors Cover Work Sharvari H. Baet Arvindkumar Gupta Jalasha D'costa Dipika Gaonkar Aparna Kumari Copy Editors Mradula Hegde Gladson Monteiro Aditya Nair Laxmi Subramanian 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 also formed several startups 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 local farmers' markets. Roger is the founder of The Interviewr, a Twilio-powered startup that 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. There were many hours that I spent behind a computer typing. 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. About the Reviewer Jakir Hayder graduated in Computer Science from South East University and is doing 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 been developing Facebook apps since 2007. He gave his talk at Facebook Developer Garage, Dhaka. He has developed websites with deep integration 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 specially fond of scrum and standup. Besides his full-time job, Jakir writes his blog at blog.jambura.com. He can be followed on Twitter @jakirhayder and on his LinkedIn profile bd.linkedin.com/in/zakirhyder. He lives in Bangladesh with his wife Fathema and his son Arham. Currently, Jakir is working as a Sr. Software Engineer at Cellbazaar, the biggest classified ads site in Bangladesh. I'd like to thank my wife for taking care of my three-month old boy while I reviewed the book and did my job. I'd also like to thank Apeksha Chitnis for being patient with me. 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 [email protected] 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? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On-demand and accessible via web browsers 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. Table of Contents Preface 1 Chapter 1: Into the Frying Pan 5 Introduction 5 Adding two-factor voice authentication to verify users 6 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 Introduction 31 Tracking account usage 32 Screening calls 36 Buying a phone number 39 Setting up a voicemail system 42 Building an emergency calling system 45 Chapter 3: Conducting Surveys via SMS 49 Introduction 49 Why use PDO instead of the standard MySQL functions? 50 Letting users subscribe to receive surveys 52 Building a survey tree 54 Sending a survey to your users 58 Adding tracking for each user 60 Listening to user responses and commands 62 Building a chart of responses 64 Table of Contents Chapter 4: Building a Conference Calling System 69 Introduction 69 Scheduling a conference call 70 Sending an SMS to all participants at the time of the call 74 Starting and recording a conference 76 Joining a conference call from the web browser 80 Monitoring the conference call 84 Muting a participant 88 Chapter 5: Combining Twilio with Other APIs 91 Introduction 91 Searching for local businesses via text 93 Getting the local weather forecast 98 Searching for local movie listings 100 Searching for classifieds 101 Getting local TV listings 102 Searching Google using SMS 104 Searching the stock market 106 Getting the latest headlines 107 Chapter 6: Sending and Receiving SMS Messages 109 Introduction 109 Sending a message from a website 110 Replying to a message from the phone 111 Forwarding SMS messages to another phone number 114 Sending bulk SMS to a list of contacts 117 Tracking orders with SMS 118 Sending and receiving group chats 125 Sending SMS messages in a phone call 129 Monitoring a website 130 Chapter 7: Building a Reminder System 135 Introduction 135 Scheduling reminders via text 136 Getting notified when the time comes 139 Retrieving a list of upcoming reminders 141 Canceling an upcoming reminder 144 Adding another person to a reminder 148 Chapter 8: Building an IVR System 153 Introduction 153 Setting up IVRs 154 Screening and recording calls 158 Logging and reporting calls 160 ii Table of Contents Looking up HighriseHQ contacts on incoming calls 163 Getting directions 167 Leaving a message 170 Sending an SMS to your Salesforce.com contacts 176 Chapter 9: Building Your Own PBX 179 Introduction 179 Getting started with PBX 180 Setting up a subaccount for each user 184 Letting a user purchase a custom phone number 187 Allowing users to make calls from their call logs 192 Allowing incoming phone calls 197 Allowing outgoing phone calls 202 Deleting a subaccount 208 Chapter 10: Digging into OpenVBX 215 Introduction 215 Building a call log plugin 216 Building a searchable company directory 218 Collecting Stripe payments 224 Tracking orders 231 Building a caller ID routing plugin 238 Testing call flows 243 Index 249 iii