Search code examples
chatbotdialogflow-esapi-ai

Set Dialogflow response object based on user platform


I have a webhook server that responds to Dialogflow with rich message objects, which work on the Google Assistant.

However, in the case the user chatting with my bot from a different platform other than Google Assitant, such as the web or Amazon Alexa, I would like to send a basic text response.

After looking through the docs, I am not sure how I can send a response message object that will display a rich message object when the user uses Google Assistant, and a plain text response on other platforms as a fallback.

This is the format of my current rich message response object, taken from the docs: https://dialogflow.com/docs/reference/agent/message-objects#basic_card_response

{
    "messages": [
      {
        "buttons": [
          {
            "openUrlAction": {
              "url": "https://linkUrl.com"
            },
            "title": "AoG Card Link title"
          }
        ],
        "formattedText": "AoG Card Description",
        "image": {
          "url": "http://imageUrl.com"
        },
        "platform": "google",
        "subtitle": "AoG Card Subtitle",
        "title": "AoG Card Title",
        "type": "basic_card"
      }
    ]
}

Solution

  • To do this, you'll just include a regular text/speech response in the messages object.

    In the /query doc, check out the POST response sample.

    Your JSON should look something like this:

    {
        "messages": [
          {
            "buttons": [
              {
                "openUrlAction": {
                  "url": "https://linkUrl.com"
                },
                "title": "AoG Card Link title"
              }
            ],
            "formattedText": "AoG Card Description",
            "image": {
              "url": "http://imageUrl.com"
            },
            "platform": "google",
            "subtitle": "AoG Card Subtitle",
            "title": "AoG Card Title",
            "type": "basic_card"
          },
          {
            "speech": "text response",
            "type": 0            
          }
        ]
    }