Creating a bot framework for WhatsApp can seem daunting, but with the right guidance, it’s easier than you think! Today, we’ll walk you through the installation process, building intents, and connecting your bot using the WhatsApp Info Bot framework. Buckle up for an exciting ride into the world of messaging bots!
Getting Started with Installation
To get underway with building your WhatsApp Info Bot, you need to install the necessary package. There are two versions available: the edge version and the stable version. Here’s how to do it:
- Edge version:
yarn add github:adiwajshing/WhatsappInfoBot - Stable version:
yarn add @adiwajshing/whatsapp-info-bot
Building Intents
Intents are central to how your bot understands queries. Think of them as the “thoughts” of your bot that help it answer different kinds of questions. Let’s consider the example of building a simple intent to answer questions about timings.
Example: Timing Intent
The intent for timings can be structured as follows:
{
"keywords": ["timings", "time", "when", "timing", "schedule", "open", "close"],
"answer": "Timings for entity.key are: entity.value",
"entities": {
"piano room": "6:00AM-12:00AM",
"lunch": "12:15PM-02:30PM",
"breakfast": "08:00AM-10:30AM",
"snacks": "04:45PM-06:15PM",
"dinner": "07:30PM-10:15PM",
"laundry": {
"dropoff": "Mon - Thu 08:00AM-02:00PM",
"pickup": "Wed - Sat 04:30PM-06:00PM"
},
"library": "all the time fren, except friday",
"salon": {
"alternates": ["parlour", "parlor", "saloon"],
"value": "11:00AM-7:00PM, closed on tuesdays"
},
"asg": "shuts at 11:30PM"
},
"meta": {
"userFacingName": ["timings"],
"description": "Timings for facilities",
"examples": ["mail room timings", "timing 4 dinner", "yo bro, when can i get lunch"]
}
}
Understanding the Intent Structure
Imagine that your bot is a librarian. When you ask about the opening hours, the bot searches through its catalog (keywords) to find the right section (entities) that answers your query. Then, it checks its schedule (answer) and gives you the right information. The Mustache templating system is like the librarian’s shorthand, allowing you to refer to the entities dynamically.
Using Your Intent
Once you have defined the timings intent, you can use it in your bot’s language processor. This is akin to getting the librarian to engage with customers:
import timings from './timings.json';
import createLanguageProcessor from '@adiwajshing/whatsapp-info-bot/LanguageProcessor';
createLanguageProcessor([timings]).chat();
// This will initiate a chat in the terminal.
Advanced Intent Handling with Classes
Sometimes, you might need to construct a more dynamic intent, such as fetching weather information. In such cases, a JavaScript class can handle these complex requests efficiently.
export default async () => {
const entities = {};
const fetchCities = async () => {
// fetch the cities you can answer for from an API
entities = { "new york": "", "sf": "", "new delhi": "", "tokyo": "" };
await fetchCities();
};
return {
keywords: ["weather", "like"],
entities: entities,
answer: (entities, user) => {
// Fetch the weather
return "lol I dont know"; // Placeholder
},
meta: {
userFacingName: ["weather"],
description: "Ask about the weather in different cities",
examples: ["weather in SF?", "new york weather", "listen fren, you better tell me what its like in Bombay"]
}
};
};
Regular Expression-Based Intents
For more precision in intent parsing, you can incorporate regular expressions. This is like giving the librarian a quick search tool for specific documents:
export default {
regexps: [/(?:i want to )access doc(?:ument) ([a-z0-9]*)(?: and ([a-z0-9]*))/],
entities: {},
answer: entities => `I see you want to access docs ${entities.join(", ")}`,
meta: {
userFacingName: ["documents", "document access"],
description: "Access a document by ID.",
examples: ["I want to access document 1234 and 5678", "access doc 1234"]
}
};
Connecting with WhatsApp
Now that you have your intents ready, it’s time to connect your bot to WhatsApp using Baileys:
import timings from './timings.json';
import weather from './weather';
import createLanguageProcessor, { createBaileysResponder } from '@adiwajshing/whatsapp-info-bot/LanguageProcessor';
(async () => {
const languageProcessor = createLanguageProcessor(
[timings, await weather()],
{ parsingFailedText: 'I dont understand input' }
);
createBaileysResponder(languageProcessor, {
authFile: './auth_info.json',
respondToPendingMessages: false
}).start(); // This will connect and start responding to messages.
})();
When you run the bot for the first time, you’ll need to scan the QR code to enable WhatsApp Web. Once authenticated, your bot will start receiving and responding to messages!
Troubleshooting
If you encounter any issues during setup or while using your bot, consider the following:
- Ensure that all dependencies are correctly installed.
- Double-check your intent structure for any syntax errors.
- Make sure your authentication information is correctly set in the authorization file.
- If your bot is not responding, verify if it’s connected to WhatsApp.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Final Thoughts
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
