Create a WhatsApp Bot with Twilio

WhatsApp is set to roll out the Bot API for developers but currently is in beta for select verified businesses. If you are eager to build and try a WhatsApp Bot then you can try Gupshup or Twilio Sandbox.

Remember to use this for testing purposes only.

In this post, we are going to see Twilio WhatsApp sandbox, and we will build a sample bot too.

Get Started

To get started, go to https://www.twilio.com/whatsapp and click on Try the Sandbox today button.

/uploads/click_sandbox.png

After clicking the try button, you will be redirected to the login page. Login or Sign Up there.

Once you get login, you will able to see your Twilio Console Dashboard.

From Dashboard go to Programmable SMS and then WhatsApp or visit this URL https://www.twilio.com/console/sms/whatsapp/learn

Now you will see a screen something like below.

You will get a WhatsApp mobile number and a sandbox joining code. Save the mobile number to your phone and send the join code to the Twilio sandbox.

join <your_join_code>

After sending the message to the sandbox, it will get received to the console showing message “waiting for your message”.

If you receive Message Received! in the console, it means you are now connected with your app using the sandbox.

Now let’s try to send some messages to the connected clients from the console. To do that click on the Next: Send One-Way Message button.

Next, you will see a dashboard to send a one-way message. To do that click on any of the three built-in messages to try and after the message loads click on the Make Request button.

After making the request from the console, you will receive a sample order notification message.

Similarly, you can try Two-way Messages next.

Use your Backend to send messages

Twilio allows you to send WhatsApp messages from your server using their Twilio API.

To configure your server go to Sandbox from the sidebar menu or https://www.twilio.com/console/sms/whatsapp/sandbox and enter your server URL where the requests to be sent from Twilio.

To set up your server install the required Twilio SDK compatible with your programming language. I have used Python SDK here for the demo.

For SDK code and examples visit this URL https://www.twilio.com/docs/sms/whatsapp/quickstart/ and choose your preferred programming language get started.

Here is a sample Python code below from Docs

# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client


# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'your_account_sid_here'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

message = client.messages.create(
                              body='Hello there!',
                              from_='whatsapp:+14155238886',
                              to='whatsapp:+15005550006'
                          )

print(message.sid)

If you don’t want to use Twilio SDK, then you can return the responses in the form of TwiML XML. It will be parsed and sent by Twilio.

Learn more about TwiML here https://www.twilio.com/docs/sms/twiml.

That’s it. We are done building our own WhatsApp Bot using Twilio WhatsApp Sandbox.

Thank you for reading. Try making a WhatsApp bot now. 🤗