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 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, IDE software, and the communication platform Twilio.

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 onboarding. 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

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

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

Leave a Reply

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

Worth reading...
Implementing A Blazing Fast IoT Network With MQTT

IN THE ARTICLES