How to Send SMS Reminders with Laravel Lumen
Time to read: 5 minutes
Introduction
In the previous tutorial, we worked on creating an iCal feed using Laravel Lumen. An iCal feed allows users to export a calendar event and view it on an external calendar. In this tutorial, we will take an extra step. Using the Twilio API, we will send SMS reminders to users who subscribed to an event.
We are going to extend the application used to create an iCal feed, therefore you need to have followed the previous tutorial. Otherwise, you can clone the code from Github.
Requirements
- A PHP development environment
- An installation of Composer
- A PostgreSQL database
- Twilio Account
Setup
Install Dependencies
We need to install the Twilio SDK for PHP, which makes it easy to interact with the Twilio API from our Lumen application. In your terminal, within the project directory, run the command:
Update the .env
In the root directory, we have a .env file. Previously we updated it with our database credentials.
After installing the Twilio SDK, we need to update our .env file with three Twilio credentials:
You can find the credentials on your Twilio dashboard:
Create the Subscribers Migration
We previously had one table, tech_events. We need to create a new table for subscribers and link it to the tech_events table. Each subscriber should be linked to a tech event.
Let's go ahead and run the command:
We can find our migration in the database/migrations folder. In our newly created migration, let’s add the following lines of code:
We have added the fields: first_name, last_name, phone_number and tech_events_id.
The key thing to note from the code above is that the field tech_events_id, has a foreign key relationship with the id field in the tech_events table.
To create the new subscribers table and update foreign keys, run the command:
Let’s check our database. We should now have a new table called subscribers. The fields we made in the migration should be reflected in the table. Using Postico, this is what the database looks like:
Create the Subscribers Model
Our models are currently in the app directory as single files. Since we’re adding one more model, this is a great opportunity for us to organize them by putting them in a Models folder.
Once we have moved our models to the Models folder, let’s create a new model called Subscribe.php.
This is what our directory looks like:
For the files we just moved to the Models folder, remember to change the namespace to: namespace App\Models;
In the newly created Subscriber.php file, let’s add the following code:
In this model, we have the mass assignable fields and the rules for assigning values to said fields. Lastly, we have indicated the relationship to the TechEvents model.
We also need to update the TechEvents model in order to define the relationship with the Subscriber model. A tech event can have many subscribers. In the file app/Models/techEvents.php, inside the TechEvents class, let’s add this piece of code:
Seed the Subscribers Table
Before we can get to the logic of sending SMS reminders, we need to have numbers or at least one number in the database. For this tutorial, I am using a trial account so we can only send text messages to verified numbers. We will go ahead and add that number to the database. If you haven’t created a verified number, please do so on the verified numbers dashboard.
In the previous tutorial, we used Model Factories to create fake data. In this tutorial we are going to do it slightly differently. We need to enter an actual verified phone number, so we will create a seeder from scratch.
In the app/database/seeds directory, create a file called SubscriberTableSeeder.php and add the following code:
That was easy! If you are using a trial account, make sure the number you entered in the phone_number field is a verified number otherwise the SMS will not be sent.
We need one more step to make this available for seeding. Let’s update the file app/database/seeds/DatabaseSeeder.php with the following piece of code:
The run method in this file is called when we run the command php artisan db:seed. We have added the line:
This calls the seeder file we just created and seeds the subscribers table with the data we entered. It is important to note that on line 4, we have changed the import from use App\TechEvents; to use App\Models\TechEvents;. This is because we made a change earlier and put our models inside the Models folder.
Let’s go ahead and make similar changes in the file app/database/factories/ModelFactory.php as well. Here’s the updated file:
That’s everything we need to do. If you are working with a fresh database, go ahead and run the command:
If you are following from the previous tutorial and you already have data in the database, run the command:
That only seeds data to the Seeders table. Great work! We have now defined the subscribers table and seeded data. Now we’re ready to write the logic for sending reminders.
The Code
For this tutorial, SMS reminders will only be sent two days prior to the event. To accomplish this, we are going to create a command and schedule it to run daily. Since it is good practice to keep console commands light, we will create a reminder client that handles the logic and let the command defer to it.
Create a Reminder Client
In the app folder, let’s create a folder and name it Clients. Inside the folder, let’s create a file called EventReminderClient.php with the following code:
In the constructor we have initialized the Twilio credentials we added to our .env earlier. We have also created a Twilio Rest Client, which we’ll use to send messages.
sendEventReminders() is the main method in the Event Reminder Client. This method will be called from the command we create in the next step.
The first line in the method fetches all events that have subscribers. In our case, it is only one. Secondly, it calls the getEventsDueInTwoDays($events) method which loops through the events and checks if the event is due in two days.
Once we have the events that are due in two days, the next step is to retrieve the subscribers information in order to send them SMS reminders.
Create the SMS Reminder Command
Creating a command makes it possible to schedule our logic to run as often as we require.
In the app/Console/Commands directory, let’s create a new file called, SendSmsReminderCommand.php and add the following code:
We have just created an Artisan Console Command. For more information on how to create commands, have a look at the official documentation.
Register and Schedule Command
Now that we have created our command, we need to register, test and schedule it.
In the app/Console/Kernel.php file, lets add the following code:
We have registered our command and scheduled it to run daily at midnight. You can check the documentation for more options on scheduling.
Test our Code
In order to test our command, we should run this in our terminal:
Please remember that we entered random dates for events using Model Factories. You may therefore not have an event that has a subscriber and is due in two days. This is what I get when I first run the command:
In order to test this, we need to manually change the start date of the first event in our tech_events table. Make it two days from today. This time, when you run the command, this is what you get:
You will also receive a text message on your verified phone number.
You have successfully scheduled a reminder for your subscribers!
Conclusion
Now that you have learned how to create an iCal feed and send SMS reminders, an obvious next step is to complete your own event scheduling application. If you’d like to check out the complete code, please do so on Github. The work we have done in this tutorial is available in the send-sms-reminder branch.
I look forward to seeing what you build. You can reach me on:
- Email: odukjr@gmail.com
- Github: charlieoduk
- Twitter: @charlieoduk
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.