Start building your own chatbot now >

In this guide, you’re about to learn how to build a Twitter bot using their recently released Direct Message API. Right now, it’s still in beta, so you have to ask for access using this form. This process can take up to a few days, so be patient 🙂

By the end of this tutorial, you’ll have a bot responding to private messages with random funny Chuck Norris facts. I won’t spend much time explaining how to train a bot and create a conversation flow on Recast.AI, so if you’re not confortable with that part of the process, please check out our other tutorials.

We’re going to start by connecting a very simple bot on Twitter, and will then dive deeper into what the Twitter API can offer by sending richer messages like Quick Replies.

Connect your bot to Twitter

We are reusing a Chuck Norris bot built in a previous tutorial. Go on this url and fork it.

chuck-norris-bot

This bot comes with three pre-trained intents: greetings, goodbye and get-joke. The two former are small-talks ones, triggered by sentences like “Hello” or “Bye bye”. The later comes into play when the user asks for a joke.

There is also a small conversation flow in the Build tab, with pre-defined replies per actions.

Chuck Norris joke

Now let’s connect it to Twitter! Go on the Run/Bot Connector tab and click on the Twitter channel, at the end of the list.

A popup opens with a list of steps to follow to create a connection between Twitter and your bot. Those steps are quite easy: create a Twitter app, give it the permission to read, write and send direct messages, and you’re good to go!

Bot Connector Twitter

Once you have filled the form with your credentials, click on the CONNECT button to make the connection with Twitter effective.

Note: as explained more thoroughly in this tutorial, when you create your first channel on the Bot Connector, your bot is automatically hosted. As we’ll see later in this tutorial, you can host your own code to get more control.
Go on Twitter and click on Messages in the header section. A popup opens, where you can search for the Twitter account your bot is linked to. If we send a message, our bot kindly reply with the sentences filled in the Build tab on Recast.AI.

Chuck Norris Twitter

Rich messages

Twitter allows you to send not only text, but also richer messages such as buttons. Let’s improve our bot with a quick reply!

Go on https://github.com/RecastAI/chucknorris-bot, and fork the project. If you want more details on how it works, you can follow this tutorial, it explains everything. This code allows you to fetch random jokes at runtime from an external API, and to send messages other than text (in this case, quick replies).

Follow the steps in the README to get your bot up and running. Do not forget to add your config file with your Recast.AI credentials.
Open the file src/bot.js, and update your code so it looks like below (starts at the line 13).

 .then(recastaiRes => {
    const intent = recastaiRes.intent()

    if (intent && intent.slug === 'greetings') {
      const reply = {
        type: 'quickReplies',
        content: {
          title: 'Hi :-)',
          buttons: [
            {
              title: 'Chuck Norris fact',
              value: 'Tell me a joke',
            },
            {
              title: 'Goodbye',
              value: 'Goodbye',
            },
          ],
        },
      }

      return message ? message.reply([reply]) : res.json({ reply: 'Hi, what can I do for you? :-)' })
    }

    if (recastaiRes.entities.hasOwnProperty('category')) {
       //... some code
    }

Check if the current intent in the conversation is “greetings”, and if so, send back a quick reply message with a button to end the conversation and a button to get a Chuck Norris fact.

It’s as simple as that.
Now let’s link this code with your bot. Go back on Recast.AI, in the Run>BotHosting section, and link the Github repository.

Click on “Connect your repo” to start deploying your repository! Wait for 1 or 2 minutes for it to be deployed, and speak with your bot again. This time, it greets you with a quick reply!

Chuck Norris fact

There’s more

Congratulations, you’ve made it! But wait, there are more format of messages you can send, from picture to cards and videos. The full list is available here.

Now you can build your own bot and connect it to Twitter 👍 If you have any feedback, question or suggestion for the next features, join and chat on our Slack Community (https://slack.recast.ai – Jerome).

Want to build your own conversational bot? Get started with Recast.AI !

Subscribe to our newsletter


There are currently no comments.