Prerequisites
- Ensure you have a Google Dialogflow account set up and a working agent.
- Create a Mailchimp account and have API access to your Mailchimp account settings.
- Familiarity with APIs and webhooks is essential for this integration.
Set Up Google Dialogflow
- Go to your Dialogflow console and open your agent.
- Click on the gear icon next to your agent's name to enter the settings page.
- Under the General tab, click on the Enable button in the Webhooks section.
- Note the endpoint URL for the webhook. You will need to set up a service to handle this.
Create a Webhook for Dialogflow
- Set up a server using Node.js, Python, or any language you're comfortable with. This will act as the intermediate service between Dialogflow and Mailchimp.
- Code the webhook to receive requests from Dialogflow and process them. For example, in Node.js:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.post('/webhook', (req, res) => {
const action = req.body.queryResult.action;
if (action === 'subscribe') {
// Extract required data and make logic to handle subscription
}
res.json({fulfillmentText: 'Data processed successfully!'});
});
app.listen(3000, () => console.log('Webhook server is listening on port 3000'));
- Deploy the webhook server so it is accessible to Dialogflow. Ensure it's running on HTTPS.
Set Up Mailchimp API Access
- Log in to your Mailchimp account and navigate to the account page.
- Select API keys under the Extras menu and create a new API key.
- Save the API key securely as you will need it to access your Mailchimp account programmatically.
Integrate Mailchimp API with the Webhook
- Install a library for interacting with Mailchimp within your webhook server. For Node.js, mailchimp-marketing is a good choice:
npm install @mailchimp/mailchimp_marketing
- Incorporate the Mailchimp logic into your webhook code. For instance:
const mailchimp = require('@mailchimp/mailchimp_marketing');
mailchimp.setConfig({
apiKey: 'your-api-key',
server: 'your-server-prefix'
});
async function subscribeUser(email) {
const response = await mailchimp.lists.addListMember('list-id', {
email_address: email,
status: 'subscribed'
});
return response;
}
app.post('/webhook', (req, res) => {
const action = req.body.queryResult.action;
const email = req.body.queryResult.parameters.email;
if (action === 'subscribe') {
subscribeUser(email)
.then(() => {
res.json({fulfillmentText: 'Subscription successful!'});
})
.catch(() => {
res.json({fulfillmentText: 'Failed to subscribe user.'});
});
}
});
- Update your server's configuration settings to handle Mailchimp-specific values like the server prefix and list ID.
Configure Dialogflow to Trigger the Webhook
- In Dialogflow, open the intent that you want to associate with Mailchimp subscription.
- In the Fulfillment section, enable Webhook call for this intent.
- Configure fulfillment responses and ensure your webhook URL is correctly set as mentioned in earlier steps.
Test the Integration
- Launch Dialogflow's simulator and attempt to trigger the intent linked with Mailchimp subscription.
- Confirm that the conversation triggers your webhook and handles Mailchimp operation correctly according to responses in the simulator.
- Check logs for any errors if the subscription doesn't work, and ensure all API credentials and endpoints are correct.