Webhook Test

node v8.14.0
version: 3.0.7
endpointsharetweet
const express = require('express'); const app = express() app.use(express.json()); app.get('/', (req, res) => res.send('Hello World!')) app.listen(3000, () => console.log('Example app listening on port 3000!')) // POST method route app.post('/', function (req, res) { console.log(req); let action = req.body.queryResult.action; console.log(action); let responseJson = {}; responseJson.fulfillmentText = 'This is an endpoint published to RunKit'; // displayed response if(action === 'getPlanetAttribute'){ let planetName = req.body.queryResult.parameters.planet; let attributeName = req.body.queryResult.parameters.attribute; var request = require("request"); var queryString = '{"Planet":"'+planetName+'","Attribute":"'+attributeName+'"}'; console.log(queryString); var urlPrefix = process.env.DBURL; console.log(urlPrefix); var options = { method: 'GET', url: urlPrefix+'/rest/planets?q='+queryString, headers: { 'cache-control': 'no-cache', 'x-apikey': process.env.API_KEY } }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); bodyJson = JSON.parse(body); responseJson.fulfillmentText = 'The '+attributeName +' of '+planetName +' is '+bodyJson[0]["Value"]; res.json(responseJson); }); } else if(action === 'changedAttribute'){ //let planetName = req.body.queryResult.parameters.planet; let attributeName = req.body.queryResult.parameters.attribute; let contexts = req.body.queryResult.outputContexts; console.log(contexts); var item = contexts.find(item => item.name.endsWith('/contexts/planet-attribute-set')); var planetName = item.parameters.planet; var request = require("request"); var queryString = '{"Planet":"'+planetName+'","Attribute":"'+attributeName+'"}'; console.log(queryString); var urlPrefix = process.env.DBURL; console.log(urlPrefix); var options = { method: 'GET', url: urlPrefix+'/rest/planets?q='+queryString, headers: { 'cache-control': 'no-cache', 'x-apikey': process.env.API_KEY } }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); bodyJson = JSON.parse(body); responseJson.fulfillmentText = 'The '+attributeName +' of '+planetName +' is '+bodyJson[0]["Value"]; res.json(responseJson); }); } })
Loading…

no comments

    sign in to comment