I made a chatbot which talks to an external API and calls another intent using the data provided by the API. The API provides the followup event's name. I have to make use of the inline editor only and not the webhook for certain reasons.
function setFollow(agent) {
agent.add("hello");
axios.post(url, body, {headers})
.then (({ data }) => {
agent.add("setting followup event");
agent.setFollowupEvent({ "name": data.followupEventInput_name, "parameters" : { "name": data.name}});
})
}
I am successfully calling the API and getting an appropriate response from it (the event name and the parameter value). The event and thus the next intent is still not being called. I even tried using hardcoded values to call the event but it did not work. The execution is going inside the then
statement (checked with console logs) but the event is not being set (even with hardcoded values).
What is the reason behind this and how can I solve it?
I have already checked the contexts: there are 2 active contexts (A and B) and the intent which should be called based on the event has 1 input context (A). Right now, no intent is being triggered.
It was a JS issue. I was not returning the promise.
return axios.post(url, body, {headers}).then(...)
solved the issue.
This was happening because JavaScript code execution is asynchronous by default, which means that JavaScript won’t wait for a function to finish before executing the code below it. The return statement before the promise makes JS wait for the promise to resolve
or reject
.
Without the return statement, the function completed execution before agent.add() was executed and hence the Dialogflow bot was not working as expected.