Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiRAD Studio

Learn To Build A Powerful WhatsApp Chatbot With Twilio

smartmockups_kpvcpzow

Chatbots have managed to save many huge amounts of time and money for the customer service industry. Increasingly, companies are creating chatbots using well known and widely-used social media and communication apps which have expanded their platforms to offer services specifically targeted at this kind of market. An example is WhatsApp and their WhatsApp Business API. In this post, we will create a simple chatbot for WhatsApp using that API and communication platform Twilio.

What is a Chatbot?

Chatbots are applications that simulate a human conversation. Typically they help customer services departments answer online questions, saving time and money while also providing an alternate method for customers to engage and interact with staff. It is also possible to use chatbots to automate certain repetitive processes such as obtaining delivery schedules for orders, requesting refunds or pointing customers to additional areas such as on-boarding. Some chatbots are based on simple mechanisms which match parsed text input with a raft of likely answers matching keywords extracted from that parsed data. Others are more subtle and complex and use artificial intelligence to perform their tasks.

What is the WhatsApp Business API?

It is a solution to help companies that want to scale up their messenger service and can be integrated with service management platforms. To obtain an account it is necessary to seek a Solution Partner. They are companies that provide the infrastructure for the operation of the service.

What is Twilio?

Twilio is the world’s leading cloud communication platform and is one of the Solution Partners of WhatsApp. It is aimed at companies that need communication services to integrate with their systems. Twilio operates in the pay-as-you-go format, with no contracts. Also, it offers a Sandbox to develop applications for free.

How do I to create a Twilio account ?

Go to the Twilio website at https://www.twilio.com and create an account. Go to your console and check your ACCOUNT SID and AUTH TOKEN. On the left panel, click on the message icon, then settings and WhatsApp Sandbox Settings. You will see:

Learn To Build A Powerful Whatsapp Chatbot With Twilio stage 1

As it says, you need to send, in my case, the message ‘join poet-earth’ to that number. If you receive the message below, you are all set and ready to go:

Learn To Build A Powerful Whatsapp Chatbot With Twilio stage 2

How do I create a simple Delphi WhatsApp chatbot application?

To receive messages we need a server API. When someone sends a message, Twilio will get it and make an HTTP Post in an endpoint. The endpoint needs to be informed on Twilio Sandbox (When a message comes in):

image-1357739

On your application, in that endpoint, you can grab the data and send a response, based on the message received.

To create the Server API, we will use the Delphi MVC Framework. You can check an easy implementation of it, here and also, how to expose your API endpoint with ngrok. Also, you will want to use a ‘Twilio Client’. Download the TwilioClient.pas here and add it to your project.

How to create Twilio client application with Delphi

After creating a DMVC project and added the TwilioClient, go to your controller, add an endpoint:

Also, add as const your account sid and auth token as strings. On the receive procedure, you can access data through:

This returns a string with data so, we just need to make manipulate that. Add the following to your code and check the result on the DMVC command prompt:

How to receive and check a WhatsApp message using Delphi

Now we can check the message received and send a proper response. So, to test, you can do:

How do I send a WhatsApp message using Delphi and Twilio?

Now, create another procedure. On that, you should declare a TTwilioClient and send your message. Here is the code to send the message to Twilio using Delphi:


That’s it, you just learned how easy it is to use Delphi to send and receive a message to WhatsApp based on a received message. We also created a simple chatbot that integrates Delphi, Twilio and WhatsApp. The complete example code showing how to send and receive messages with Twilio and WhatsApp is here: https://github.com/checkdigits/delphi-twilio


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Related posts
CodeDelphiRAD Studio

This Is How To Make A Geocoding App in 5 Minutes

CodeDelphiRAD Studio

How To Make An Immensely Powerful Search Engine Results App

CodeDelphiRAD Studio

Everything You Need To Build A Real-time Flight Status App

CodeDelphiRAD Studio

How To Build A Powerful App For Live News And More

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Worth reading...
Powerful Dashboards with REST API & Delphi MVC Framework

IN THE ARTICLES