const express = require('@runkit/runkit/express-endpoint/1.0.0');
const url = require('url');
const axios = require('axios');
const RECAST_REQUEST_TOKEN = 'XXX';
// Setting up our server
const app = express(exports);
// This route will be triggered when Chatfuel sends a message
app.get('/', (req, res) => {
const query = url.parse(req.url, true).query;
const userId = query['chatfuel user id'];
const userMessage = query['user_message'];
// Call Recast's API with the user message
return axios
.post(
'https://api.recast.ai/build/v1/dialog',
{
message: { content: userMessage, type: 'text' },
conversation_id: userId,
},
{ headers: { Authorization: `Token ${RECAST_REQUEST_TOKEN}` } }
)
.then(body => {
// Format messages to Chatfuel format
const formattedMessages = body.data.results.messages.map(chatfuelFormat);
// Sends the answer back to Chatfuel
res.json({
messages: formattedMessages,
});
})
.catch(err => res.status(400).send(err));
});
// We need to manually "translate" Recast.AI message object to Chatfuel's format
// For now I only implemented text and images but you can add others if you need to
// Check out the documentation I linked above to the see the json structure of the other formats
function chatfuelFormat(message) {
// Source : { type: 'text', content: 'XXX' }
// Destination { text: 'XXX' }
if (message.type === 'text') {
return { text: message.content };
}
// Source: { type: 'picture', content: 'URL' }
// Destination: { attachment: { type: 'image', payload: { url: 'URL' } } }
if (message.type === 'picture') {
return {
attachment: {
type: 'image',
payload: { url: message.content },
},
};
}
console.error('Unsupported message format: ', message.type);
return { text: 'An error occured' };
}