Search code examples
google-cloud-platformgoogle-cloud-rundialogflow-cx

Webhook JSON return from Cloud Run Endpoint not showing in Dialogflow CX


I developed a chatbot solution with Dialogflow CX, where a question is made by the user, and Dialogflow uses an unauthenticated webhook (temporary) to call a Cloud Run endpoint. Cloud Run runs a Python code with a Flask application that jasonifies the output of the algorithm.

Locally I can call the Cloud Run endpoint successfully (200) with the following script:

import requests
url='https://container-xxx-acc12345.run.app/predict'
r = requests.post(url, json={"text": "which is the deadline provided by the law?"})
print(r.json())

Which returns me:

{'prediction': 'Second year after initial deadline'} 

Cloud Run Metrics

Then when I go back to Dialogflow, it successfully calls the Cloud Run Endpoint (200 above), but I do not get a valid text response in the chat:

Chatbot no response

Cloud Run allows unauthenticated calls and the webhook is well configured. The webhook was added at the parameters session of the Ask Question page, whose tag is prediction.

As configured at the event handlers, the chatbot does not show any webhook error, but the result of the prediction does not show up in the chat.

Cloud Logging data:

httpRequest: {
latency: "0.010078946s"
protocol: "HTTP/1.1"
remoteIp: "22.222.222.164"
requestMethod: "POST"
requestSize: "2327"
requestUrl: "https://container-xxx-1234abc.run.app/predict"
responseSize: "959"
serverIp: "222.222.34.33"
status: 200
userAgent: "Google-Dialogflow"
}

This is the return of Flask application:

return jsonify({ 
    "fulfillment_response": { 
        "messages": [{ 
            "text":  
                predict(data)
            
        }] 
    } 
})

Any ideas on how to solve this issue are welcome.


Solution

  • I solved the problem. My return from Flask app in Cloud Run is fixed to:

    return jsonify({ 
        "fulfillment_response": { 
            "messages": [{ 
                "text": {
                     "text": [
                    predict(data)
                    ] 
                }
            }] 
        } 
    })
    

    I also found out you can define the body request payload also.