Last active
November 19, 2017 07:31
-
-
Save neilghosh/6931a8ed3d3e7d5eaec2769712596bc2 to your computer and use it in GitHub Desktop.
Webhook for devfest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
const MAX_COMPANIES_IN_RESPONSE = 2; | |
function sendResponse(response,responseTxt, context, end){ | |
let responseJson = {}; | |
responseJson.speech = responseTxt; // spoken response | |
responseJson.displayText = responseTxt; // displayed response | |
responseJson.contextOut = context; | |
if(end) { | |
responseJson.data = {google:{expect_user_response: false}}; | |
} | |
response.json(responseJson); // Send response to Dialogflow | |
} | |
function fetchPrice(res, symbol) { | |
var lastPrice; | |
var options = { | |
url: 'https://trade-junky.appspot.com/api/getquote?name=' + symbol, | |
headers: { | |
'api-key': '2246696acd8638f0fbfe5d6e4d515a3eaefed5c19b5a2c18' | |
} | |
}; | |
request.get(options, function(error, response, body) { | |
console.log('error:', error); // Print the error if one occurred | |
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received | |
console.log('body:', body); //Prints the response of the request. | |
responseOut = JSON.parse(body); | |
lastPrice = responseOut["lastPrice"]; | |
sendResponse(res, "The price for "+symbol+" is "+ lastPrice + " INR.", [], true); | |
}); | |
} | |
/** | |
* Responds to any HTTP request that can provide a "message" field in the body. | |
* | |
* @param {!Object} req Cloud Function request context. | |
* @param {!Object} res Cloud Function response context. | |
*/ | |
exports.dialogflowFulfillment = function stockTellerWebHook(req, res) { | |
console.log(req.body.result.parameters); | |
var companyname = req.body.result.parameters.company_name; | |
var options = { | |
url: 'https://trade-junky.appspot.com/api/companysearch?name=' + companyname, | |
headers: { | |
'api-key': '2246696acd8638f0fbfe5d6e4d515a3eaefed5c19b5a2c18' | |
} | |
}; | |
request.get(options, function(error, response, body) { | |
console.log('error:', error); // Print the error if one occurred | |
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received | |
console.log('body:', body); //Prints the response of the request. | |
var respJson = JSON.parse(body); | |
var keys = Object.keys(respJson); | |
var count = keys.length; | |
if(count === 0 ) { | |
sendResponse(res, "No such company found",[], false); | |
} else if(count === 1) { | |
fetchPrice(res, keys[0]) | |
} else if(count > 1) { | |
var contexts = [{name:"stock-teller-followup",parameters:{symbols:keys.join(';')}}]; | |
var outStr = respJson[keys[0]]+" or "+respJson[keys[1]]+"."; | |
sendResponse(res, "More than one company found. Which one do you want? "+outStr,contexts, false); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "sample-http", | |
"version": "0.0.1", | |
"dependencies": { | |
"request": "^2.81.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment