Building a cross-platform chatbot

David Stotijn
MessageBird
Published in
4 min readAug 11, 2016

Only halfway through the year, and it’s safe to say 2016 has become the year of chat. With major players like Facebook opening up their messaging platforms to third party developers, the possibilities for offering rich and meaningful chat features to your users are endless.

With MessageBird’s recent launch of its Chat API, building cross-platform chat solutions is now easier than ever. Once you’ve integrated our platform agnostic API, you’ll be sending and receiving messages to and from Facebook Messenger, WeChat, Telegram, LINE and SMS in no time. Want to dive in? Read along and build a question-answering chatbot. Let’s get started!

KnowItAllBot

We’re going to build a chatbot called KnowItAllBot that tries to answer whatever factual questions you throw at it: movie trivia, weather forecast, scientific data, etcetera. We’ll make use of Wolfram|Alpha’s API to query for answers and send them back to the user on whichever platform the question was asked.

We’ll start off with a simple Node.js application using the hapi framework that handles incoming HTTP webhooks and interacts with the Wolfram|Alpha API and MessageBird’s Chat API. You can browse the repository of the finished project on GitHub.

Demo

To get an idea of what the end result will be, give our demo chatbot a try by asking it a question via Facebook or Telegram.

Creating a web service

To answer incoming questions from users, we need to set up a web service that handles incoming webhooks from the Chat API. Whenever a message status changes (e.g. the status of an outgoing message changes to “delivered”), the Chat API platform sends an HTTP request to one or more URLs configured for a given channel.

For our project, the web service should read the incoming data (sender, message contents), craft an answer, and call the Chat API to send that answer to the user.

Here’s what our index.js file looks like:

Inside our route handler, we optionally welcome users who started the chat with Telegram, and for the magic part: We call the Wolfram|Alpha API to get the answer to the question:

Our chat-api.js file that calls the Chat API:

Creating a channel

Head over to the Chat API page on the MessageBird portal to create your first channel. The quickest platform to provision is Telegram. During the creation wizard demo you’ll find more details on how to create a Telegram bot.

Running the web service

From the Chat API “Settings” page in the MessageBird portal, find your API access token and set it as an environment variable:

$ export CHAT_API_ACCESS_TOKEN="your_chat_api_access_token"

Create a WolframAlpha app and set the app ID as an environment variable:

$ export WOLFRAM_ALPHA_APP_ID="your_wolfram_alpha_app_id"

Now run the Node.js app from the directory containing the app:

$ node .

The app will bind on local on the port defined in index.js, which is 8000.

If you run the Node.js app locally, you can use a free service like ngrok to create a publicly accessible URL that tunnels to your local machine:

$ ngrok http 8000

Configure the assigned forwarding address as the “callbackUrl” of your channel, suffixed with “/webhook”. For example: https://12345a6.ngrok.io/webhook. Now the route in our Node.js application is used for webhooks from the Chat API platform.

Ask me anything!

So now that our channel is configured, the webhook URL is set up, and the Node.js app is running, let’s ask our first question! Simply search for the name of your Telegram bot, start a chat and ask it something, e.g. “How many Pokémon are there?” Within a few seconds, you should receive the answer back:

Q&A in Telegram

Success! Now, the fun part is adding additional bots, e.g. for Facebook Messenger and WeChat. Without writing a single additional line of code our Node.js will be able to work with these platforms, as the Chat API is platform agnostic. As we add new platforms in the future, you’ll benefit instantly since they’re all available to every tier we offer and require no code changes.

Q&A in Facebook Messenger

What’s next?

Check out the README of the finished KnowItAll project. You can fork our repository and start hacking or create something totally different from scratch. If you run into any trouble or want to show off what you’ve built, just hit us up on support@messagebird.com.

--

--