Send Appointment Reminders with PHP and Laravel
Ahoy! We now recommend you build your appointment reminders with Twilio's built in Message Scheduling functionality. Head on over to the Message Scheduling documentation to learn more about scheduling messages!
This Laravel 5 web application shows how to create appointments for customers at a date in future, and send out reminders for those appointments in a background job that runs every few minutes.
In this tutorial, we'll point out the key bits of code that make this application work. Check out the project README on GitHub to see how to run the code yourself.
Check out how Yelp uses SMS to confirm restaurant reservations for diners.
Let's get started! Click the button below to begin.
Configure the application to use Twilio
Before we can use the Twilio API to send reminder text messages, we need to configure our account credentials. These can be found on your Twilio Console. You'll also need an SMS-enabled phone number - you can find or purchase a new one to use here.
We configure these values using Dotenv, the configuration library of choice for Laravel applications. More information on how to configure this application can be found in the project README.
Next let's see how we create a new Appointment
.
Create a new appointment
In order to send an appointment reminder, we first need to create an appointment! In the controller, we take information submitted in a form (notably a customer's name and phone number, plus a time for the appointment in the future) and save it in an Appointment
model.
Notice that we use the Carbon date library to make it easier for us to parse and do simple operations with the time.
Now that we have our Appointment
created, let's see how to schedule a reminder for it.
Schedule a job to send reminders
Every ten minutes, we'd like our application to check the appointments database to see if any appointments are coming up that require reminders to be sent out. We configure both the job code we'd like to run and the interval on which to run it here.
With our job configured, we're now ready to write the actual console command code that will send out our reminders.
Create a console command to run the job
To actually execute our recurring job logic, we create an Artisan console command which queries the database for upcoming appointments and sends reminders as necessary. As an added bonus, defining our job logic in this way allows us to run the reminder job whenever we want from the command line.
Let's dig further into the ApplicationReminder class
Find appointments that need reminders
Our recurring job uses an instance of the AppointmentReminder
class to query the database for appointments coming up in the next ten minutes and send out reminder messages.
In the constructor, we execute the database query using a custom scope on the Appointment
model. This should give us a list of all appointments with a date and time that falls within the next ten minutes.
Now let's setup the Twilio REST Client in order to send some SMS reminder messages.
Set up a Twilio API client
Also in the AppointmentReminder
constructor, we create a Twilio REST Client to send out reminders via SMS. We initialize it with the Twilio account credentials we configured earlier.
With the client and the reminders in hand. All that is left is to send an SMS for them.
Send reminder messages with the Twilio API
These two private functions are called for every appointment coming up that requires a reminder to be sent. The first formats the text of the message to be sent out. The second actually uses the Twilio REST API client to send out a text message.
We provide a to
parameter which is the customer's phone number, a from
parameter which is a number in our account, and a body
parameter which contains the text of the message.
That's it! Our Laravel application is all set to send out reminders for upcoming appointments.
Where to next?
We hope you found this sample application useful. If you're a PHP developer working with Twilio, you might enjoy these other tutorials:
Put a button on your web page that connects visitors to live support or sales people via telephone.
Improve the security of Laravel's built-in login functionality by adding two-factor authentication via text message.
Did this help?
Thanks for checking out this tutorial! If you have any feedback to share with us, please reach out on Twitter... we'd love to hear your thoughts, and know what you're building!
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.