Create conversational experiences with the Chatbot API for Drupal 8
This is my second post based on the talk I delivered at DrupalCamp London 2018: “Hi user, I am Drupal. How can I help you?” In the first post I looked at some of the scenarios in which chatbots and personal assistants might be used to beneficially serve website content to your audiences and briefly introduced the Chatbot API for Drupal 8. In this post, I’ll walk through the second half of the talk in which I show how easy it is to surface content through a chatbot using the API.
Check out the slides and code examples used in this post on GitHub.
Chatbot API features
To recap, Chatbot API provides a common flexible extra layer that sits between Drupal, your NLP and your various chatbots and personal assistants. It makes your website chatbot-friendly so you don’t have to write new code each time you translate your conversational experiences from one interface to another.
Chatbot API works with Dialogflow and Alexa out of the box, with support for more platforms on the way.
Using Chatbot API, Intents become plugins, making use of the flexible Drupal Plugin API.
Entities can be supplied by your website data, and pushed easily to the NLP where you’re creating your conversational experience.
The module also provides a Views integration to easily create Intents to return lists of content items for the user to explore further e.g. event listings, news articles, pages, reviews etc.
Creating an Intent plugin
You create the plugin in the usual way, using the Intent Name (the machine name of your intent) as the Plugin ID, adding your logic and defining the responses of the bot/assistant.
The module includes a Drupal Console command generate:plugin:chatbotintent utility to easily create Intent plugin skeleton through a wizard.
Pushing Entities to your NLP
Entities are great for improving the way your NLP understands and recognises the parts of the conversation you want to perform logic on. The more the Entities entries you provide, the easier it will be for you to write your intents and their logic. Most NLPs also support synonyms, and again the more, the better.
In the Drupal context, these Entities may be nodes of a specific content type, or taxonomy terms. But what if we have a lot of entries in our Entities list? We need to push the Entities from our website to the NLP so they can be used in our logic. This is a simple matter with the Chatbot API Entities submodule: the configuration wizard will help you create an Entity Collection, selecting what from your website should be synced as an NLP Entity. It supports synonyms too, and also gives high flexibility on what or where exactly are the Entities you want to push (nodes, terms, fields, bespoke condition, results from a database query, etc) by creating new ChatbotApiEntities/QueryHandler plugins.
As part of the Entity Collection setup, you can choose to which endpoint (in this case, API.AI – the old name for Dialogflow) to push the Entities during the next cron run.
Adding new Entities within those collections can now be done entirely in Drupal. The NLP, in this case Dialogflow, will then automatically synchronise after the next cron run.
Serving content via Views integration
If we were to answer a user’s query like ‘show me a list of properties in Shoreditch’ on our Drupal website, we’d serve a list of search results using Views.
In a conversational experience, we can do the same thing, with the (voice) assistant, for example, presenting one result at a time and asking the user if they’d like to hear more or skip to the next result. To achieve this, we set up a view with pagination, showing only one result per page.
With the Views integration provided by Chatbot API you just need to create – or reuse – your view and add a Chatbot Intent display. By doing this the module will automatically select best default options (pagination, 1 item per page), but you are free to personalise anything you want. For instance you may want to add a “Footer” element (in a conversational context that will be the suffix of your response) and a No Result message (which will be our response when there are no results).
The only requirement is for you to set up the Intent Name exactly as its name on the NLP. That’s it, your view is now exposed. Go ahead and play with it.
The code examples have both the Dialogflow configuration and a View for you to play with.
Chatbots: a new way to serve your website content
Here’s an example of my Drupal Concierge in action, demoing the maintenance_support intent.
As you’ll see, the chatbot is merely serving content to the end user from the repository of available content on my site. The difference is, it’s using natural language processing to determine the user’s intent and the entities referenced, and serving content in a way that makes navigating the content easier via text and speech interfaces. Designing a conversational experience requires that you take these differences into account.
With that in mind, here are my top tips for serving content via chatbots:
- Plan your Intents in advance – think about the context in which the user is asking for the information and the how it can be served to them in the best way;
- Consider the variety of utterances that might be used to ask for the same thing – there will be surprises. A useful resource here is answerthepublic.com, which returns Google’s suggested searches for any query;
- Train your bot – as sophisticated as the NLPs are, they’re still pretty dumb. But by answering queries left unanswered by the bot (kind of like marking its homework), you can train it become smarter.
- Build your content with Intents in mind – your content may not be ready to be exposed by a chatbot. You may want to redesign some of your content types with shorter titles, chatbot-only content snippets etc.
- Create a Display Mode for your bot – this can also help present content in a bot-friendly way, by stripping out extraneous fields or media which would be inappropriate to serve via a bot.
Have fun! And do let me know about how you’re using Chatbot API to serve content – I’m sure I’ve only thought of a small number of the potential uses for .
And finally, if you’d like to discuss the possibility of working together on an implementation or new integration, don’t hesitate to get in touch.