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.
Let's get started! Click the button below to begin.
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.
Next let's see how we create a new
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
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.
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.
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
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.
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.
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.
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.
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!