I'am fetching the data via axios from mini crypto compare api for getting the crypto prices in dialogflow, but in my fulfillment code I'am receiving this error (if unreachable code after return). If I bring the if
section inside the promise, than the agent.add()
will not work and it give no response defined for the platform error in the console.
function priceFinder(agent) {
const data = agent.parameters[CRYPTO_NAMES];
let btc = '';
let eth = '';
return axios.get('https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,BNB,SOL,LUNA&tsyms=USD')
.then(response => {
btc = response.data.BTC.USD;
eth = response.data.ETH.USD;
});
if(data === "btc") {
agent.add(`BTC Price is : ${btc}`);
}
else if(data === "eth") {
agent.add(`ETH Price is : ${eth}`);
}
}
I was able to get your function working on my side, here is my updated code to your function based on the block you provide:
used library
const https = require('https');
main function
function getBitcoinInfo(agent){
const url= 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,BNB,SOL,LUNA&tsyms=USD';
const req = https.get(url, res => {
console.log(`statusCode: ${res.statusCode}`);
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log(data);
console.log(JSON.parse(data).BTC);
});
});
agent.add(`I found bitcoins!`);
}
IntentMap
let intentMap = new Map();
intentMap.set('Get Bitcoin Info', getBitcoinInfo);
agent.handleRequest(intentMap);
Useful Tips
Useful links: