Outcast
Outcast

Reputation: 5117

How can I trigger a `action.intent.INTENT_NAME` intent from my webhook?

I want to create a chatbot with Dialogflow and Google Assistant along with Google Transactions API for enabling a user to order a chocolate box. For now my agent contains the following four intents:

I am using Dialogflow Json instead of Node.js to connect my agent with Transactions API. I want to test that the user meets the transaction requirements (when ordering the chocolate box) by using the actions.intent.TRANSACTION_REQUIREMENTS_CHECK action of Google actions. For this reason, following Google docs, when Int1 is triggered I am using a webhook which connect Google Assistant to the following python script (back-end):

from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
import  requests

app = Flask(__name__)
CORS(app)

@app.route("/",  methods=['POST'])

def index():

    data = request.get_json()    
    intent = data["queryResult"]["intent"]["displayName"]

    if (intent == 'Int1'):

        return jsonify({ "data": {
                "google": {
                "expectUserResponse": True,
                "isSsml": False,
                "noInputPrompts": [],
                "systemIntent": {
                  "data": {
                    "@type": "type.googleapis.com/google.actions.v2.TransactionRequirementsCheckSpec",
                    "paymentOptions": {
                      "actionProvidedOptions": {
                        "displayName": "VISA-1234",
                        "paymentType": "PAYMENT_CARD"
                      }
                    }
                  },
                  "intent": "actions.intent.TRANSACTION_REQUIREMENTS_CHECK"
                                }
                        }
                                        }
                })
    else:
        return jsonify({'message': 'HERE'})

if __name__== "__main__":
    app.run(debug=True)

The json which I return above when intent = 'Int1' is the one specified at Google docs for "Checking requirements with your own payment method". According to Google docs, this must be done next:

Receiving the result of a requirements check

After the Assistant fulfills the intent, it sends your fulfillment a request with the actions.intent.TRANSACTION_REQUIREMENTS_CHECK intent with the result of the check.

To properly handle this request, declare a Dialogflow intent that's triggered by the actions_intent_TRANSACTION_REQUIREMENTS_CHECK event.

For this reason, I defined Int2 and as its event the actions_intent_TRANSACTION_REQUIREMENTS_CHECK.

However, I do not receive anything at my back-end like a result of the check and therefore I do not know if the action actions.intent.TRANSACTION_REQUIREMENTS_CHECK is really triggered. Why is this happening?

In general, how can I trigger one actions.intent.INTENT_NAME intent from my webhook/back-end?

When I am using the v2 version of Dialogflow, I am getting the following info/message about the webhook on Dialogflow when Int1 is triggered:

  "webhookStatus": {
    "code": 3,
    "message": "Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: data in message google.cloud.dialogflow.v2.WebhookResponse."
  }

In the same case, I am getting the following info/message about the webhook on Google Assistant simulator when Int1 is triggered:

  "responseMetadata": {
    "status": {
      "code": 14,
      "message": "Webhook error (206)"
    }

Finally, let me mention that I am testing all this with Python and ngrok at my local computer so perhaps this poses a problem because at the beginning of Google docs the following is mentioned:

Warning: The Actions Web Simulator should not be used to test an app with transactions. Please use an Assistant-enabled Android or iOS device to accurately test your app during development.

Upvotes: 4

Views: 3435

Answers (2)

Outcast
Outcast

Reputation: 5117

I finally solved this problem.

I had to replace the key "data" in the json which I was sending back when Int1 was triggered with the key "payload". In other words, I had to adjust my fulfilment response to the v2 version of Dialogflow.

Therefore, now I do get a second post request at my back-end which is sent because of the trigger of actions.intent.TRANSACTION_REQUIREMENTS_CHECK and of Int2.

Specifically, I get the following:

{
    "responseId": "*****************************",
    "queryResult": {
        "queryText": "actions_intent_TRANSACTION_REQUIREMENTS_CHECK",
        "parameters": {},
        "allRequiredParamsPresent": true,
        "fulfillmentText": "HERE",
        "fulfillmentMessages": [
            {
                "text": {
                    "text": [
                        "HERE"
                    ]
                }
            }
        ],
        "outputContexts": [
            {
                "name": "*****************************"
            },
            {
                "name": "*****************************"
            },
            {
                "name": "*****************************"
            },
            {
                "name": "*****************************"
            },
            {
                "name": "*****************************"
            },
            {
                "name": "*****************************",
                "parameters": {
                    "TRANSACTION_REQUIREMENTS_CHECK_RESULT": {
                        "@type": "type.googleapis.com/google.actions.v2.TransactionRequirementsCheckResult",
                        "resultType": "OK"
                    }
                }
            }
        ],
        "intent": {
            "name": "*****************************",
            "displayName": "Int2"
        },
        "intentDetectionConfidence": 1,
        "diagnosticInfo": {},
        "languageCode": "en-us"
    },
    "originalDetectIntentRequest": {
        "source": "google",
        "version": "2",
        "payload": {
            "isInSandbox": true,
            "surface": {
                "capabilities": [
                    {
                        "name": "actions.capability.WEB_BROWSER"
                    },
                    {
                        "name": "actions.capability.MEDIA_RESPONSE_AUDIO"
                    },
                    {
                        "name": "actions.capability.SCREEN_OUTPUT"
                    },
                    {
                        "name": "actions.capability.AUDIO_OUTPUT"
                    }
                ]
            },
            "inputs": [
                {
                    "rawInputs": [
                        {
                            "inputType": "KEYBOARD"
                        }
                    ],
                    "arguments": [
                        {
                            "extension": {
                                "@type": "type.googleapis.com/google.actions.v2.TransactionRequirementsCheckResult",
                                "resultType": "OK"
                            },
                            "name": "TRANSACTION_REQUIREMENTS_CHECK_RESULT"
                        }
                    ],
                    "intent": "actions.intent.TRANSACTION_REQUIREMENTS_CHECK"
                }
            ],
            "user": {
                "lastSeen": "2018-05-16T11:15:14Z",
                "locale": "en-US",
                "userId": "*****************************"
            },
            "conversation": {
                "conversationId": "1526470000479",
                "type": "ACTIVE",
                "conversationToken": "[]"
            },
            "availableSurfaces": [
                {
                    "capabilities": [
                        {
                            "name": "actions.capability.SCREEN_OUTPUT"
                        },
                        {
                            "name": "actions.capability.AUDIO_OUTPUT"
                        }
                    ]
                }
            ]
        }
    },
    "session": "*****************************"
}

Upvotes: 2

Tarun
Tarun

Reputation: 7

I think your response object is incorrect. the intent attribute should be inside the systemIntent object

"data": {
  "google": {
    "expectUserResponse": true,
    "isSsml": false,
    "noInputPrompts": [],
    "systemIntent": {
      "intent": "actions.intent.TRANSACTION_REQUIREMENTS_CHECK",
      "data": {
        "@type": "type.googleapis.com/google.actions.v2.TransactionRequirementsCheckSpec",
        "paymentOptions": {
          "actionProvidedOptions": {
            "displayName": "VISA-1234",
            "paymentType": "PAYMENT_CARD"
          }
        }
      }
    }
  }
}

Upvotes: -1

Related Questions