Reputation: 2637
I am trying to create a firebase function to send the API response to dialogflow, using fulfillment webhook, but I am facing Error: 500.
const functions = require('firebase-functions');
var request = require('request')
exports.webhook = functions.https.onRequest((request, response) => {
console.log("request.body.result.parameters: ", request.body.result.parameters);
let params = request.body.result.parameters;
var options = {
url: `https://islam360api.herokuapp.com/${params.find}`,
json:true
}
request(options, function(error, response, body){
if(error) response.send(error);
else response.send(body);
});
});
The warning I got while deploying is
13:26 warning Unexpected function expression prefer-arrow-callback
✖ 1 problem (0 errors, 1 warning)
0 errors and 1 warning potentially fixable with the `--fix` option.
for example if params.find
is evoke
then parsing the API response should give this result
https://islam360api.herokuapp.com/evoke
{"speech":"In Surat-ul-Fateha, Ayat Number: 7, Quran says: not of those who have evoked [Your] anger or of those who are astray. ","displayText":"In Surat-ul-Fateha, Ayat Number: 7, Quran says: not of those who have evoked [Your] anger or of those who are astray. "}
Edit: This is the response I get while deploying firebase function
C:\Users\mnauf\Desktop\IOT\islam360\firebase>firebase deploy --only functions
=== Deploying to 'islam360-3cf18'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint C:\Users\mnauf\Desktop\IOT\islam360\firebase\functions
> eslint .
C:\Users\mnauf\Desktop\IOT\islam360\firebase\functions\index.js
13:26 warning Unexpected function expression prefer-arrow-callback
✖ 1 problem (0 errors, 1 warning)
0 errors and 1 warning potentially fixable with the `--fix` option.
+ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (37.98 KB) for uploading
+ functions: functions folder uploaded successfully
i functions: updating Node.js 6 function webhook(us-central1)...
+ functions[webhook(us-central1)]: Successful update operation.
+ Deploy complete!
Please note that it can take up to 30 seconds for your updated functions to propagate.
Project Console: https://console.firebase.google.com/project/islam360-3cf18/overview
Logs
RAW API RESPONSE
{
"id": "ed1108a6-a938-4cfb-8fe1-7a1fa0c856e4",
"timestamp": "2019-03-20T18:02:22.676Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "where does Quran talk about evoke",
"action": "",
"actionIncomplete": false,
"parameters": {
"find": "evoke"
},
"contexts": [],
"metadata": {
"isFallbackIntent": "false",
"webhookResponseTime": 129,
"intentName": "find",
"intentId": "dd549c44-25bd-48fa-915f-4810759968eb",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false"
},
"fulfillment": {
"speech": "Something went wrong",
"messages": [
{
"type": 0,
"speech": "Something went wrong"
}
]
},
"score": 1
},
"status": {
"code": 206,
"errorType": "partial_content",
"errorDetails": "Webhook call failed. Error: 500 Internal Server Error"
},
"sessionId": "4d887eaa-2dc1-649d-9ae6-238a6d79f085"
}
FULFILLMENT REQUEST
{
"id": "ed1108a6-a938-4cfb-8fe1-7a1fa0c856e4",
"timestamp": "2019-03-20T18:02:22.676Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "where does Quran talk about evoke",
"action": "",
"actionIncomplete": false,
"parameters": {
"find": "evoke"
},
"contexts": [],
"metadata": {
"isFallbackIntent": "false",
"webhookResponseTime": 129,
"intentName": "find",
"intentId": "dd549c44-25bd-48fa-915f-4810759968eb",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false"
},
"fulfillment": {
"speech": "Something went wrong",
"messages": [
{
"type": 0,
"speech": "Something went wrong"
}
]
},
"score": 1
},
"status": {
"code": 206,
"errorType": "partial_content",
"errorDetails": "Webhook call failed. Error: 500 Internal Server Error"
},
"sessionId": "4d887eaa-2dc1-649d-9ae6-238a6d79f085"
}
FULFILLMENT RESPONSE
Error: could not handle the request
FULFILLMENT STATUS
Webhook call failed. Error: 500 Internal Server Error
Console Screenshot
Firebase Logs
From firebase logs, it seems like, I have to pay. Do I have to?
Upvotes: 3
Views: 2904
Reputation: 3425
if you are using dialogflow v2:
request(options, function(error, response, body){
if(error)
response.send({ fulfillmentText: "error in api call"});
else
response.send({ fulfillmentText: body.speech });
});
if you are using dilogflow v1 (will deprecate soon)
request(options, function(error, response, body){
if(error)
response.send({ speech: "error in api call"});
else
response.send({ speech: body.speech });
});
to check/change version of dialogflow, goto dilogflow console(https://console.dialogflow.com), press gear icon, and check it out in general section, dont forget pressing save button in case you change it
According to your firebase function logs yes you need to pay,
Please note that firebase functions only allow calling o ly google apis and doesn't allow third-party api in free quota, you need to move your plan to "pay as you go" since it is the cheapest among all
Alternatively, you may use express server for making webhook and you may deploy it on heroku cloud, they allows third-party api call in their free plan
Upvotes: 3