Home / DialogFlow ES / Using the text responses from Dialogflow console in your webhook code
DialogFlow ES

Using the text responses from Dialogflow console in your webhook code

First published: Jan 2018 | Last updated: May 2022

This is another question which came to me from a client, and I think the answer might be helpful for a lot of folks.

Here is the challenge: You want to use the text response you write in your Dialogflow console to be available to the webhook code.

Here are three reasons for doing this (there may be others too):

  1. maintain the WYSIWYG (what you see is what you get) for your copy editors, who are usually not programmers. They write the copy inside the Dialogflow console, and you just use it from your webhook code
  2. implement some kind of templating system where you write the copy in the text response and populate the dynamic stuff in the webhook
  3. let the copy editors choose the error messages which should be displayed if the webhook cannot do its job

What is sent to the webhook

Before we can discuss how to use the copy from the Dialogflow console from inside your webhook, you should first understand exactly what is sent to the webhook.

In my Quiz Bot, for example, I use a template in the response block inside the Dialogflow console.

This is what it looks like:

Here is the JSON which is sent to the webhook

{
    "responseId": "146c2c7e-6f52-4351-8de2-0e1c5f0475de-cf898478",
    "queryResult": {
        "queryText": "10th Dec 2015",
        "parameters": {
            "any": "10th Dec 2015",
            "score5": "0"
        },
        "allRequiredParamsPresent": true,
        "fulfillmentText": "Incorrect, the correct answer is A23B2346",
        "fulfillmentMessages": [
            {
                "text": {
                    "text": [
                        "Incorrect, the correct answer is A23B2346"
                    ]
                }
            },
            {
                "text": {
                    "text": [
                        "You scored _SCORE_ out of 5"
                    ]
                }
            },
            {
                "payload": {
                    "richContent": [
                        [
                            {
                                "type": "chips",
                                "options": [
                                    {
                                        "text": "Start Over"
                                    }
                                ]
                            }
                        ]
                    ]
                }
            }
        ],
        "outputContexts": [
            {
                "name": "projects/quickstarttemplates-umel/agent/sessions/dfMessenger-56041339/contexts/await_answer_5",
                "parameters": {
                    "any": "10th Dec 2015",
                    "any.original": "10th Dec 2015",
                    "score4": "0",
                    "score4.original": "",
                    "score5": "0",
                    "score5.original": ""
                }
            },
            {
                "name": "projects/quickstarttemplates-umel/agent/sessions/dfMessenger-56041339/contexts/session-vars",
                "lifespanCount": 45,
                "parameters": {
                    "any": "10th Dec 2015",
                    "any.original": "10th Dec 2015",
                    "score1": "0",
                    "score1.original": "",
                    "score2": "0",
                    "score2.original": "",
                    "score3": "0",
                    "score3.original": "",
                    "score4": "0",
                    "score4.original": "",
                    "score5": "0",
                    "score5.original": ""
                }
            },
            {
                "name": "projects/quickstarttemplates-umel/agent/sessions/dfMessenger-56041339/contexts/__system_counters__",
                "parameters": {
                    "no-input": 0.0,
                    "no-match": 0.0,
                    "any": "10th Dec 2015",
                    "any.original": "10th Dec 2015",
                    "score5": "0",
                    "score5.original": ""
                }
            }
        ],
        "intent": {
            "name": "projects/quickstarttemplates-umel/agent/intents/d7d39d3d-9b21-4284-954d-19b3ea797503",
            "displayName": "answer5.INCORRECT"
        },
        "intentDetectionConfidence": 0.3,
        "languageCode": "en"
    },
    "originalDetectIntentRequest": {
        "payload": {}
    },
    "session": "projects/quickstarttemplates-umel/agent/sessions/dfMessenger-56041339"
}

Now in the webhook code, I use this template and replace _SCORE_ with the computed value

def calculate_score(req):
    query_result = req.get('queryResult')
    session_vars = get_session_vars(query_result)
    sum_of_scores = get_sum_of_scores(session_vars)

    fulfillment_messages = json.dumps(query_result.get('fulfillmentMessages'))
    return_msg = fulfillment_messages.replace('_SCORE_', str(sum_of_scores))

    json_formatted = json.loads(return_msg)
    result = {
        "fulfillmentMessages": json_formatted
    }
    with open('result.json', 'w', encoding='utf-8') as f:
        json.dump(result, f, ensure_ascii=False, indent=4)
    return result

That’s how you can use the text response from inside your Dialogflow ES console in your webhook code.

<— End of article —>


This website contains affiliate links. See the disclosure page for more details. 
"The magic key I needed as a non-programmer"

The custom payload generator was the magic key I needed (as a non-programmer) to build a good demo with rich responses in DialogFlow Messenger. I've only used it for 30 minutes and am thrilled. I've spent hours trying to figure out some of the intricacies of DialogFlow on my own. Over and over, I kept coming back to Aravind's tutorials available on-line. I trust the other functionalities I learn to use in the app will save me additional time and heartburn.

- Kathleen R
Cofounder, gathrHealth
"Much clearer than the official documentation to be honest"

Thanks a lot for the advice (of buying and following your videos)! They helped a lot indeed. Everything is very clear when you explain, much clearer than the official documentation to be honest 🙂

Neuraz T
Review for Learn Dialogflow CX
"I will strongly recommend this course because even I can learn how to design chatbot (no programming background)"

I think Aravind really did a great job to introduce dialogflow to people like me, without programming background. He organizes his course in very clear manner since I have been a college professor for 20 years. It is very easy for me to recognize how great Aravind’s course is! Very use-friend and very easy to follow. He doesn’t have any strong accent when he gives the lectures. It is so easy for me to understand. Really appreciate it.

Yes, I will strongly recommend this course because even I can learn how to design chatbot (no programming background) after studying Avarind’s course, you definitely can!

Ann Cai
Review for Learn Dialogflow ES

Similar Posts

  • Hi,
    I create simple web site with node.js and I wanna connect this site to dialogflow for taking kind of information so I need little help for this connection.
    Thank you.

    • I have updated the post to answer your question. The main thing that you need to understand is the structure of the JSON sent over to your webhook, and the rest will fall in place.

  • thank for the article, please, in V2 version, how can I firstly change the text reponse in dialogFlow? and secondly make the response comming from a dataBase of my ow api?
    thanks!

  • Another related question is about the data you can send from the webhook to the web chatbot. I find if you send from the webhook a value of “payload”. For exemple, “payload” : {“my_payload” : “my_value”}, is received in your web client as a “data” : {“my_payload” : “my_value”}. Do you think, is it the correct way to send information from webhook to web chatbot? (for example, to send url images, links, rich text, …). Thank you!

    • It depends on your end goal. Given that it is a website chatbot, generally speaking, it would be easier for you to use the Google Assistant response types because they give you a way to preview your content right inside the Dialogflow console, plus it means your JSON received by website chatbot is already correctly formatted. I have written about the basic idea here. Also consider that if you actually end up putting raw HTML into your custom payload, you may need to escape it and it will make the text harder to read.