Set Up IBM Cloud Account and Services
- Create an IBM Cloud account. If you already have one, log in to your dashboard.
- Navigate to the IBM Cloud Catalog and find Watson Assistant by searching for it in the AI services section.
- Provision a Watson Assistant service instance. Follow the prompts to create and name your instance.
- Access the Watson Assistant service dashboard once your instance is ready. Take note of your API Key and Assistant ID as you will need them later.
Configure Intercom Account
- If you do not have an Intercom account, sign up for one and log into the dashboard.
- Set up a new App on Intercom if you don't already have one. Go to the "App settings" section to retrieve your Intercom API Key.
Develop the Integration using Node.js
- Ensure Node.js and npm are installed on your system. If not, download and install them from the official Node.js website.
- Create a new Node.js project:
mkdir ibm-watson-intercom-integration
cd ibm-watson-intercom-integration
npm init -y
- Install necessary npm packages for IBM Watson SDK and Intercom client:
npm install ibm-watson intercom-client express body-parser
Write the Server Code
- Create an `index.js` file and set up a basic Express server:
const express = require('express');
const bodyParser = require('body-parser');
const AssistantV2 = require('ibm-watson/assistant/v2');
const Intercom = require('intercom-client');
const app = express();
app.use(bodyParser.json());
const port = 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
- Initialize Watson Assistant and Intercom clients using your credentials:
const assistant = new AssistantV2({
version: '2021-06-14',
authenticator: new iamAuthenticator({
apikey: 'your-watson-api-key',
}),
serviceUrl: 'https://api.us-south.assistant.watson.cloud.ibm.com',
});
const intercomClient = new Intercom.Client({ token: 'your-intercom-api-key' });
- Add an endpoint to handle incoming messages from Intercom and forward them to Watson Assistant:
app.post('/webhook', async (req, res) => {
const userMessage = req.body.message;
try {
const session = await assistant.createSession({ assistantId: 'your-assistant-id' });
const response = await assistant.message({
assistantId: 'your-assistant-id',
sessionId: session.result.session_id,
input: {
'message_type': 'text',
'text': userMessage,
},
});
const assistantReply = response.result.output.generic[0].text;
await intercomClient.messages.create({
from: { type: 'user', id: req.body.user_id },
body: assistantReply,
});
res.sendStatus(200);
} catch (err) {
console.error(err);
res.sendStatus(500);
}
});
Deploy and Test the Integration
- Host your Node.js application. For local testing, you can use tools like ngrok to expose your localhost.
- Set up a webhook in your Intercom settings to point to your server's `/webhook` endpoint URL.
- Communicate via your Intercom app and observe responses from Watson Assistant to ensure the integration works as expected.
Refine and Secure Your Integration
- Ensure secure data transmission between your services by using HTTPS and managing API key confidentiality.
- Refactor and modularize code for better maintainability and adhere to best practices for product readiness.