
Bird recently launched Programmable Conversations. It lets companies blend communications platforms like WhatsApp, Messenger and SMS into their systems — using a single API.
Bird recently launched Programmable Conversations. It lets companies blend communications platforms like WhatsApp, Messenger and SMS into their systems — using a single API.
I wanted to give it a whirl, so I built a WhatsApp bot to-do list, because who doesn’t need an automated to-do list to help organize their day? It may sound complicated, but it was actually easy, and I’d like to tell you all about it.
Now, I work at MessageBird, so I could just dive in and start building. If you try this, you’ll need to request early access. But once you’re set up with a WhatsApp channel, you can log on to the Dashboard on the MessageBird website and get started.
The first thing I did was read the docs. I learned that, in order to get messages from the bot, I would have to use a webhook. This meant that my bot would need to be accessible from the internet. When building APIs like this, it's important to follow API versioning best practices for maintainability. Since I was just starting to code it, I decided to use ngrok. It creates a tunnel from the public internet to your dear localhost port 5007. Engage!
ngrok http 5007 -region eu -subdomain todobot
Next, I needed to do a call to the Programmable Conversations API to create the webhook. It’s a POST to https://conversations.messagebird.com/v1/webhooks
and it looks something like this:
Sweet. Now the Conversations API is going to do a POST request to:
https://todobot.eu.ngrok.io/create-hook
whenever a new message gets created on the WhatsApp channel you set up earlier.
This is what a webhook payload looks like:
We want to answer those messages. Let’s start by echoing them, what do you say?
Now, for the interesting part. Do a POST request to:
https://conversations.messagebird.com/v1/conversations/<conversationID>/messages
to answer the request.
There. This is all you need to create a bot that acts like 5-year-old human.
Now, let’s make a push towards building the whole to-do list. First, modify the createHookHandler function a bit so it calls the new handleMessage function instead of respond.
handle will simplistically parse the messages, do some work, and pick the response. Let’s look at the “add” command:
Here, we set up:list := manager.fetch(whp.Conversation.ID). Basically, “manager” is a concurrency safe map that maps conversation IDs to to-do lists.
A to-do list is a concurrency safe string slice. All in memory!
Another important thing! You can archive conversations. In some applications, like CRMs, it’s important to keep track of certain interactions — to track the effectiveness of customer support employees, for example. The Conversations API lets you archive a conversation to “close” the topic. If the user/customer sends another message, the Conversations API will open a new topic automatically.
Also. Doing PATCH request to https://conversations.messagebird.com/v1/conversations/{id}
with the right status on the body allows you to archive the conversation with that id. We do this with the “bye” command:
archiveConversation will do the PATCH request and manager.close(whp.Conversation.ID) will remove the to-do list conversation.
But hey, Programmable Conversations is an omni-channel solution. What if you wanted to reuse the code of the bot for a different platform, like WeChat? This multi-channel approach is part of deflecting inquiries to lower cost channels strategy. How would you go about it?
Just create a new webhook to target that channel! A webhook that sends requests to the same https://todobot.eu.ngrok.io/create-hook
url we used for WhatsApp!
This will work because the handler code always uses the conversationID from the webhook payload to answer the messages instead of a hardcoded channelID. MessageBird’s Conversations API will automatically determine the channel for the conversation to send your message over.
Do you want to build your own bot? Take a look at the full code on Github: https://github.com/marcelcorso/wabot, request early access to WhatsApp by visiting the WhatsApp page and clicking the Contact Sales button to fill out the form. Happy botting!