Imagine You’re Sitting in a Coffee Shop
It’s a typical Tuesday afternoon and you’re sipping on your favorite brew. The caffeine kicks in just as you think about how awesome it would be if you could automate some of your routine website interactions. You’re not alone in this thought. Many businesses, both big and small, are now using chatbots to handle everything from customer service inquiries to lead generation. But where do you, a budding developer, start with creating your own chatbot? Let’s explore some beginner-friendly tools that can help you on your journey.
The Building Blocks: Tools and Platforms
Building a chatbot may seem like a daunting task, but it’s more accessible than ever thanks to an array of tools designed for developers at different skill levels. For those just starting, platforms such as Dialogflow, Microsoft’s Bot Framework, and Rasa provide the necessary building blocks to get your chatbot up and running.
Dialogflow: A product by Google, Dialogflow offers a natural language understanding platform that makes it easy to design and integrate a conversational user interface into your mobile app, web application, device, bot, etc. Let’s get our hands dirty with a simple example.
// Example of intent in Dialogflow
{
"id": "bdbdbdba-f9c4-4c26-94ba-e23c3e654fb5",
"name": "Weather Inquiry",
"auto": true,
"contexts": [],
"responses": [
{
"resetContexts": false,
"affectedContexts": [],
"parameters": [
{
"id": "3d5adc5a-5d6c-44dd-bc6e-1f85b2f4e25a",
"name": "city",
"required": true,
"dataType": "@sys.geo-city",
"value": "$city",
"isList": false
}
],
"messages": [
{
"type": 0,
"platform": "VIBER",
"speech": "The weather in $city is currently sunny with a high of 85 degrees."
}
]
}
],
"priority": 500000
}
With Dialogflow, you can start by creating intents, which represent a mapping between what a user says and what action should be taken by your software. After defining the “Weather Inquiry” intent, your bot can interpret, “What’s the weather in New York?” and respond appropriately using an API to fetch real-time data.
Microsoft’s Bot Framework: This tool enables you to build, test, and publish your chatbot across multiple platforms, including Skype, Slack, and Facebook Messenger. It provides a rich set of tools and a thorough SDK in C# and JavaScript. Here’s a snippet to implement a basic bot using Node.js:
const { BotFrameworkAdapter, MemoryStorage, UserState } = require('botbuilder');
const { ActivityHandler } = require('botbuilder-core');
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});
const memoryStorage = new MemoryStorage();
const userState = new UserState(memoryStorage);
class MyBot extends ActivityHandler {
constructor() {
super();
this.userState = userState;
this.onMessage(async (context, next) => {
await context.sendActivity(`Echo: ${context.activity.text}`);
await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity('Welcome to the bot!');
}
}
await next();
});
}
}
This basic setup has your bot echo back any messages it receives, making it a great starting point for learning how to handle user input and output elegantly.
Bringing in the AI with Rasa
Rasa is a popular open-source framework known for its compatibility and versatility in creating advanced chatbots. It allows for complete control over the conversational flow and integrates well with machine learning models. Rasa's two-part model includes NLU (Natural Language Understanding) and Core, providing full-stack capabilities for bot-building.
Here's how you can create a simple starter bot with Rasa:
# config.yml for Rasa NLU pipeline
language: en
pipeline: supervised_embeddings
policies:
- name: MemoizationPolicy
- name: KerasPolicy
- name: MappingPolicy
- name: FormPolicy
# domain.yml for Rasa domain
intents:
- greet
entities:
- name
slots:
name:
type: text
responses:
utter_greet:
- text: "Hello {name}! How can I assist you today?"
actions: []
When a user greets the bot, Rasa will match the input against the trained models and trigger the appropriate intent. The bot responds using pre-defined templates, making every conversation smooth and intelligently driven.
Each of these tools comes with ample documentation and community support to help you get started on your chatbot development journey. Whether you're automating simple tasks or creating complex AI-driven interfaces, there's a platform out there to suit your needs. Now that you're armed with these tools, the only limitation is your imagination and willingness to experiment.