Integrate Dialogflow with Instagram: Prerequisites
- Create a Facebook Developer Account if you haven't already.
- Ensure you have a Facebook Page and an Instagram Business Account linked to it.
- Set up a Dialogflow project on the Google Cloud Platform.
Set Up Instagram Webhooks
- Go to the Facebook Developer Portal and create a new app.
- Select the 'Messenger' and 'Instagram' product from the app dashboard.
- Configure webhooks to subscribe to Instagram events. Verify the webhook by providing a callback URL and a verify token.
const crypto = require('crypto');
function verifySignature(req, res, buf) {
const signature = req.headers['x-hub-signature-256'];
if (!signature) {
throw new Error('Signature missing');
}
const hash = crypto.createHmac('sha256', process.env.APP_SECRET)
.update(buf)
.digest('hex');
if (hash !== signature.split('=')[1]) {
throw new Error('Invalid signature');
}
}
Create a Messenger/Instagram App
- Within your Facebook Developer app, navigate to Instagram settings and configure your Instagram account.
- Obtain the access token provided by Instagram for your app.
Connect Dialogflow to Your App
- In Dialogflow, navigate to the 'Fulfillment' section to enable webhook calls.
- Point the fulfillment to a service that handles HTTP POST requests like a Node.js/Express server.
- Ensure the web service can communicate with Instagram's webhook for sending and receiving messages.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.post('/webhook', (req, res) => {
const body = req.body;
if (body.object === 'instagram') {
body.entry.forEach((entry) => {
const webhookEvent = entry.messaging[0];
console.log(webhookEvent);
});
res.status(200).send('EVENT_RECEIVED');
} else {
res.status(404).send();
}
});
app.listen(process.env.PORT || 1337, () => console.log('Server is running.'));
Handle Messages and Send Responses
- Configure your server logic to process incoming messages from Instagram and send responses using Dialogflow's API.
- Use Facebook's Graph API to send messages back to Instagram. This typically involves sending a POST request to a specific Graph API endpoint.
const request = require('request');
function callSendAPI(sender_psid, response) {
const requestBody = {
recipient: {
id: sender_psid
},
message: response
};
request({
uri: 'https://graph.facebook.com/v8.0/me/messages',
qs: { access_token: process.env.PAGE_ACCESS_TOKEN },
method: 'POST',
json: requestBody
}, (err, res, body) => {
if (!err) {
console.log('message sent!');
} else {
console.error('Unable to send message:' + err);
}
});
}
Testing and Deployment
- Test the integration using Instagram Direct. Send messages and confirm Dialogflow processes them correctly.
- Deploy your server and ensure it's running continuously, either locally or using cloud services like Heroku or AWS.
Security and Maintenance
- Always verify incoming webhook requests to ensure they are genuinely from Instagram, using the secret and signature mechanism described.
- Regularly update and maintain your app to comply with the latest API changes and security practices.