API.AI flow of logic

Viewed 483

This discussion applies to any integration, but as an example I'll talk about Messenger. Also, to give context, my backend is written in Java.

From what I've read, for custom scenarios (e.g. Account Linking), we shouldn't use API.AI directly. Instead, the "Callback URL" in Messenger settings (developer.facebook.com) should point to our own hosted app, which in turn calls api.api.ai/v1/query. Is that correct?

Secondly, my Intent in API.AI already has Use webhook ticked. I assume the flow of logic would be as follows:

Messenger → my app (webhook for Messenger) → api.ai → my app (webhook for api.ai)

Apparently it's not the case—API.AI never calls the webhook.

This is the JSON I sent to API.AI:

{"query":"hey","originalRequest":{"source":"facebook","data":{"object":"page","entry":[{"id":"xxx","time":yyy,"messaging":[{"sender":{"id":"zzz"},"recipient":{"id":"xxx"},"timestamp":yyy,"message":{"mid":"aaa","seq":bbb,"text":"hey"}}]}]}},"v":"20170704","sessionId":"1","lang":"en"}

And this is the response from API.AI:

{
    "id": "xxx",
    "timestamp": "yyy",
    "lang": "en",
    "result": {
        "source": "agent",
        "resolvedQuery": "hey",
        "speech": "",
        "action": "",
        "parameters": {
            "greeting": "hey"
        },
        "metadata": {
            "inputContexts": [],
            "outputContexts": [],
            "intentName": "greeting",
            "intentId": "zzz",
            "webhookUsed": "true",
            "webhookForSlotFillingUsed": "false",
            "contexts": []
        },
        "score": 1.0
    },
    "status": {
        "code": 200,
        "errorType": "success"
    },
    "sessionId": "1"
}

Does it mean that my app should call the webhook (which is the same app but different endpoint) itself?

5 Answers

Ideally, FB messenger will not talk to your webhook for messenger service. The workflow should be FB messenger->api.ai->webhook (where your actual logic resides) & even if in case you use, my app (webhook for Messenger) then you shouldn't be needing api.ai in the first place. It is because before taking any action, you need to know what user is saying & that can be done by NLP only! Using my app (webhook for Messenger) will replace NLP directly.

Second thing, We can not see fulfillment in api.ai json response. If at any point in time webhook call fails, your default response from api.ai will be posted as a reply to user's query & fallback will be called when api.ai itself does not understand the context of what user wants to say. Entering default response will help you in debugging as well.

enter image description here

Related