Search code examples
google-chat-api

Google Chat API: How do I switch between Cards v1 and Cards v2?


According to the docs, Cards v1 is deprecated, and Cards v2 seems to be much more feature-rich.

But when I use the Cards v2 syntax, the API complains about unknown or missing fields. How can I tell the Google servers that I want to use the v2 API? The documentation does not seem to tell anything about that.

All tutorials that I can find seem to use Cards v1 only. I'm mostly following the official tutorial in Python.


Solution

  • I tried to send message with card v2 with this syntax:

    "cards_v2": [[{
          "card_id": "addContact",
          "card": {}
    }]
    

    Request:

    POST https://chat.googleapis.com/v1/spaces/space_name/messages
    {
        
        "cards_v2": [{
          "card_id": "addContact",
          "card": {
            "header": {
              "title": "Rolodex",
              "subtitle": "Manage your contacts!",
              "imageUrl": "https://www.gstatic.com/images/branding/product/2x/contacts_48dp.png",
              "imageType": "CIRCLE"
            },
            "sections": [
              {
                "widgets": [
                  {
                    "buttonList": {
                      "buttons": [
                        {
                          "text": "Add Contact",
                          "onClick": {
                            "action": {
                              "function": "openDialog",
                              "interaction": "OPEN_DIALOG"
                            }
                          }
                        }
                      ]
                    },
                    "horizontalAlignment": "CENTER"
                  }
                ]
              }
            ]
          }
        }]
      ,
        "thread": {
         "name": "spaces/space_name/threads/thread_name"
      }
    }
    

    Taken from here: https://developers.google.com/chat/how-tos/dialogs?hl=en