Creating A Chatbot with Twilio - Part 1
Today I wanted to share a really fun side project I did as part of a surprise birthday present.
It involves four toolings total.
- the local server environment: MAMP
- the php micro-framework: Lumen
- SMS API: Twilio
- service for tunneling to your localhost webhook: ngrok
To create a chatbot (I wanted to call it Woodhouse) that they could text at any point of the day for food ideas, music… etc.
And at the end of this tutorial series, you’ll have all the knowledge to create your own chatbot…
Setting Up Our Local Environment
If you don’t have MAMP installed on your machine, I suggest you take a break and download it now and read my article on project environment setup. Once your localhost is up and running, we can now install Lumen.
NOTE: At the time of writing this article, Lumen 5.6 requires PHP >= 7.1.3.
Setting Up Lumen
Head over to Lumen’s docs on installing Lumen. Once it’s installed, and you navigate to the lumen site in your favorite browser, you should see something like this:
Lumen (5.6.3) (Laravel Components 5.6.*) we can move to the next section.
Routing and Creating Our Logic Controller
First we need to create a route we are going to use for the Twilio webhook, let’s call it logic. So head over to the file:
/routes/web.php within our new Lumen site and edit the base route and add a new route as shown below:
Cool cool, now we have emptied the http://yoursite/ route and created a http://yoursite/logic route (that accepts only POST methods) that points to our non-existent LogicController. Let’s change that, head over to
/app/Http/Controllers/ directory and create the following file named: LogicController.php.
Now if you navigate to http://yoursite/logic, you should see a simple message: we got here!
Adding Twilio Support to Lumen/Laravel
Next step is relatively straight forward, and very well documented here on Twilio’s site: TWILIO SMS PHP QUICKSTART
But high level, go to the site’s root within your command line and install the Twilio SDK by running:
After that, we can use it within our LogicController by updating the top of the file with two new lines:
That’s it for this article, but next we will talk about the use of tunneling our local application with .ngrok and buying a Twilio number to send and receive text from. Go ahead and download .ngrok ;-)