Set Up IBM Watson
- Create an IBM Cloud account if you don't have one. Follow this by logging in to the IBM Cloud dashboard.
- Navigate to the Watson section and select the Watson Assistant service. Click to create a new instance of Watson Assistant.
- Once the instance is created, access the service. Within the Watson Assistant GUI, create a new skill (or use an existing one) to define the conversational logic for your application.
- Within the skill, configure intents, entities, and dialog flows that will govern the conversation patterns your Watson Assistant will handle.
Generate Watson Assistant API Credentials
- After configuring the Watson Assistant skill, generate the API Key and Assistant ID from the service credentials section. These credentials will be used to authenticate requests to Watson Assistant.
- Note these credentials securely as they are essential for integration with WhatsApp.
Set Up WhatsApp Business API
- Sign up for the WhatsApp Business API via a business solution provider (BSP) or directly through the Meta for Developers platform.
- Once registered, verify your business and phone number following WhatsApp’s guidelines.
- Configure your WhatsApp Business API client and obtain an access token. This is important for authorizing API requests to WhatsApp.
Deploy a Webhook to Integrate Watson and WhatsApp
- Develop a server-side application that serves as a webhook to receive events from WhatsApp and connect to Watson Assistant. This can be done using Express.js, Flask, or any web framework of your choice.
- Set up a POST endpoint to handle incoming messages from WhatsApp. The endpoint should parse incoming JSON data for messages and sender information.
- For example, using Express.js:
const express = require('express');
const app = express();
app.use(express.json());
app.post('/webhook', (req, res) => {
const message = req.body.Body;
const sender = req.body.From;
// Call Watson Assistant service here
res.sendStatus(200);
});
app.listen(3000, () => console.log('Webhook server is running'));
- Within the webhook, implement logic to forward the message text to the Watson Assistant API using the credentials obtained earlier. Process the response generated by Watson and prepare it for delivery back through WhatsApp.
Configure Watson Assistant API Call
- Integrate the Watson Assistant SDK or use direct HTTP requests to communicate with Watson’s API from your webhook. Example using the Watson SDK:
const AssistantV2 = require('ibm-watson/assistant/v2');
const { IamAuthenticator } = require('ibm-watson/auth');
const assistant = new AssistantV2({
version: '2023-10-15',
authenticator: new IamAuthenticator({
apikey: 'YOUR_API_KEY',
}),
serviceUrl: 'https://api.us-south.assistant.watson.cloud.ibm.com',
});
function sendToWatson(sessionId, message) {
return assistant.message({
assistantId: 'YOUR_ASSISTANT_ID',
sessionId: sessionId,
input: {
'message_type': 'text',
'text': message
}
});
}
- Remember to initialize a session with Watson in your webhook to keep track of conversation context between messages.
Return Response to WhatsApp
- Upon receiving a response from Watson Assistant, format this message appropriately to comply with WhatsApp’s message structure.
- Send the formatted response back to the user on WhatsApp using the WhatsApp Business API, leveraging the previously acquired access token for authentication.
- Example using an HTTP POST method to WhatsApp API:
const axios = require('axios');
function sendToWhatsApp(responseMessage, recipient) {
axios.post('https://graph.facebook.com/v13.0/me/messages', {
messaging_product: 'whatsapp',
to: recipient,
text: { body: responseMessage }
}, {
headers: {
'Authorization': `Bearer YOUR_ACCESS_TOKEN`,
'Content-Type': 'application/json'
}
}).then(response => console.log('Message sent: ', response.data))
.catch(error => console.error('Error sending message:', error));
}
Test Your Integration
- Ensure your webhook is accessible over the internet, possibly using tools like Ngrok during development.
- Test the integration by sending a message to your WhatsApp Business account and verifying the response from Watson Assistant.
- Iterate on conversation design and message formatting as necessary for optimal user experience.