Connect Botkit or BotBuilder to WhatsApp.
This package contains an adapter that communicates directly with the WhatsApp Client Interface, and translates messages to and from a standard format used by your bot. This package can be used alongside your favorite bot development framework to build bots that work with Whatsapp.
Add this package to your project using npm:
npm install --save @aliyssium/botbuilder-adapter-whatsapp
Import the adapter class into your code:
const { WhatsAppAdapter } = require('botbuilder-adapter-whatsapp');
If you are starting a brand new project, follow these instructions to create a customized application template.
WhatsAppAdapter provides a translation layer for Botkit and BotBuilder so that bot developers can connect to WhatsApp and have access to WhatsApp's Client Interface.
When used in concert with Botkit, developers need only pass the configured adapter to the Botkit constructor, as seen below. Botkit will automatically create and configure the webhook endpoints and other options necessary for communicating with WhatsApp.
Developers can then bind to Botkit's event emitting system using controller.on
and controller.hears
to filter and
handle incoming events from the messaging platform. Learn more about Botkit's core feature →.
const adapter = new WhatsAppAdapter({
auth: process.env.WHATSAPP_AUTH
});
const controller = new Botkit({
adapter,
// ...other options
});
controller.on('message', async (bot, message) => {
await bot.reply(message, 'I heard a message!');
});
Alternately, developers may choose to use WhatsAppAdapter
with BotBuilder. With BotBuilder, the adapter is used more
directly with a webserver, and all incoming events are handled
as Activities.
const { WhatsAppAdapter } = require('botbuilder-adapter-whatsapp');
const restify = require('restify');
const adapter = new WhatsAppAdapter({
auth: process.env.WHATSAPP_AUTH
});
const server = restify.createServer();
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
await context.sendActivity('I heard a message!');
});
});
In Botkit handlers, the bot
worker for WhatsApp
contains all of the base methods as well as the following platform-specific
extensions:
Returns the first step of an auth-flow that results in the Botkit application being enabled with WhatsApp.