chatbot is a software application that is able to handle a conversation with a human user through written or spoken language. The level of intelligence among chatbots varies greatly. While some chatbots are fairly basic, responding to keywords or phrases. Others employ sophisticated artificial intelligence (AI) and machine Learning (ML) algorithms, or like Twilio Autopilot, take advantage of natural language understanding (NLU) to provide a better experience and build out more complicated conversations.

In this tutorial, we’re going to build a chatbot for WhatsApp that finds all nearby hospitals using the Twilio API for WhatsAppLaravel framework for PHP, and Redis.

Prerequisites

In order to complete this tutorial, you will need the following:

  • Twilio Account (Sign up with this link and receive an additional $10 credit.)
  • Composer globally installed
  • The Laravel installer
  • WhatsApp enabled Twilio Number
  • Redis (we will use this as our database)
  • ngrok (we will use this to connect to our locally installed Laravel application to a public URL that Twilio can connect to via webhook. If you don’t have ngrok installed, you can down a copy for Windows, MacOs, or Linux)

Architecture/Technical Overview

Our WhatsApp chatbot answers the question, “what hospitals are near me?” To achieve this, we will be storing a list of hospitals and their locations in a database; Redis in our case.

When a message is sent from WhatsApp to our WhatsApp bot Twilio number, a factory method, HospitalResponderFactory::create() , determines how our bot responds to a user. WhatsApp allows a user to send either a text or location message, but not at the same time. This is where the factory method comes to play. If the message sent by a user is a string containing “hi,” our chatbot simply responds with a greeting, else it prompts the user that the text sent is invalid. Lastly, if it’s a location message, then our bot finds hospitals (which are already stored in Redis) that are nearby to the users using the Redis geospatial queries.

Create a Laravel Application

You’ll first need to install a new Laravel application locally. The Laravel installer will help us expedite the installation. Run the following command in your terminal:

$ laravel new hospital-finder-whatsapp-bot && cd hospital-finder-whatsapp-bot

This will scaffold a new Laravel application for us into a folder called hospital-finder-whatsapp-bot.

Add the Twilio PHP SDK to the Project

We need to also install Twilio’s SDK, which we will use to interact with the Twilio API for WhatsApp and return our response (we will see this in use later). This can be installed using Composer by running the following command in your terminal:

$ composer require twilio/sdk

Configure the Twilio WhatsApp Sandbox

To launch a bot on WhatsApp, you must go through the approval process with WhatsApp. However, Twilio allows us to build and test our WhatsApp apps using the sandbox.

Once your application is complete, you can request production access for your Twilio phone number, which requires approval by WhatsApp.

Let’s start by configuring the sandbox to use with your WhatsApp account.

The Twilio console walks you through the process, but here’s what you need to do:

  • Head to the WhatsApp sandbox area of the Twilio console, or navigate from the console to Programmable SMS and then select WhatsApp
  • The page will have the WhatsApp sandbox number on it. Open your WhatsApp application and send a new message to that number.
  • The page also has the message you need to send, which is “join” plus random words like “join younger-home”. Send your message to the sandbox number.

Twilio Sandbox for WhatsApp dashboard

#laravel

Build a Nearby Hospital Finder WhatsApp Chatbot with Laravel, Redis, and Twilio
9.90 GEEK