DialogFlow to Chatfuel version for justbot

node v6.12.2
version: 1.0.0
endpointsharetweet
let { API_AI_KEY } = process.env; let URL = require('url'); let API_AI = require('apiai'); let randomize = (arr) => arr[Math.floor(Math.random() * arr.length)]; let sendResponse = ({ response, message }) => { response.writeHead(200, { 'Content-Type': 'application/json' }); response.end( JSON.stringify(message) ); } let createTextMsg = (text) => { return { messages: [{ text }] }; } let handleResponse = (response) => ({ result }) => { let message; if (result.source === 'agent') { let randomMsg = randomize(result.fulfillment.messages); message = randomMsg.payload ? randomMsg.payload : createTextMsg(randomMsg.speech); } else if (result.source === 'domains') { message = createTextMsg(result.fulfillment.speech); } sendResponse({ response, message }); } let handleError = (response) => (error) => { let message = { error }; sendResponse({ response, message }); } exports.endpoint = function(req, res) { let query = URL.parse(req.url, true).query; let app = (query.API_AI_KEY) ? API_AI(query.API_AI_KEY) : API_AI(API_AI_KEY); let request = app.textRequest(query.queryString, { sessionId: Math.random().toString().slice(2), contexts: [{ name: query.context || 'DEFAULT', parameters: query, }] }); request.on('response', handleResponse(res)); request.on('error', handleError(res)); request.end(); }
Loading…

no comments

    sign in to comment