傳送 Google Chat 應用程式的應用程式首頁資訊卡訊息

本頁面說明如何建立及傳送 Chat 應用程式的應用程式首頁資訊卡訊息。應用程式首頁是可自訂的資訊卡訊息,當使用者透過 Chat 應用程式開啟即時訊息時,Chat 應用程式就會向使用者傳送該訊息。

顯示兩則訊息的應用程式首頁資訊卡。

舉例來說,您可以設定應用程式主畫面資訊卡訊息,加入使用斜線指令與 Chat 應用程式互動的提示。對使用者而言,只有在 Chat 應用程式的即時訊息中啟用功能時,使用者才能使用應用程式首頁。


使用資訊卡建構工具設計及預覽 Chat 應用程式的 JSON 資訊卡訊息:

開啟資訊卡建構工具

必要條件

Python

Apps Script

在 Google Cloud 控制台中設定

Python

  1. 在 Google Cloud 控制台中,依序點選「選單」圖示 >「更多產品」 >「Google Workspace」 >「產品程式庫」>「Google Chat API」

    前往 Google Chat API

  2. 依序按一下「管理」和「設定」分頁標籤。

  3. 啟用「支援應用程式首頁」

  4. 勾選「支援應用程式首頁」核取方塊。

  5. 在「應用程式首頁網址」欄位中新增網址。這個值通常與應用程式網址相同。系統會針對 APP_HOME 事件呼叫這個網址。

  6. 點按「儲存」

Apps Script

  1. 在 Google Cloud 控制台中,依序點選「選單」圖示 >「更多產品」 >「Google Workspace」 >「產品程式庫」>「Google Chat API」

    前往 Google Chat API

  2. 依序按一下「管理」和「設定」分頁標籤。

  3. 勾選「支援應用程式首頁」核取方塊。

  4. 點按「儲存」

設定 Chat 應用程式

設定 Chat 應用程式,傳送應用程式首頁的新資訊卡訊息。

Python

當使用者從 Chat 應用程式開啟即時訊息時,系統會將 APP_HOME 事件傳送至您的 Chat 應用程式。當 Chat 應用程式收到這個事件時,系統會透過 pushCard 導覽傳回 RenderActions 的 JSON 執行個體。

@app.route('/', methods=['POST'])
def on_event():
  event = request.get_json()
  chat = event.get('chat')
  if chat is not None:
    return handle_chat_event(event)

def handle_chat_event(event):
  if event['chat'].get('type') == 'APP_HOME':
    return get_app_home_card()

def get_app_home_card():
  return {
    "action": {
      "navigations": [
        {
          "pushCard": {
            "sections": [
              {
                "widgets": [
                  {
                    "buttonList": {
                      "buttons": [
                        {
                          "text": "Open documentation",
                          "onClick": {
                            "openLink": {
                              "url": "https://developers.google.com/chat"
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            ]
          }
        }
      ]
    }
  }

Apps Script

這個範例會傳回資訊卡 JSON,以傳送卡片訊息。您也可以使用 Apps Script 卡片服務

實作 onAppHome 函式,傳回使用 pushCard 導覽的 RenderActions JSON 執行個體:

// "onAppHome" is the pre-defined name of the callback that the Chat servers
// execute.
function onAppHome() {
  return {
    action: {
      navigations: [
        {
          pushCard: getCard()
        }
      ]
    }
  };
}

function getCard() {
  return {
    sections: [
      {
        widgets: [
          {
            buttonList: {
              buttons: [
                {
                  text: "Open documentation",
                  onClick: {
                    openLink: {
                      url: "https://developers.google.com/chat"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    ]
  };
}

更新應用程式首頁資訊卡訊息

當使用者透過資訊卡訊息提交資訊或關閉對話方塊時,可以更新應用程式首頁資訊卡訊息。舉例來說,初始應用程式首頁資訊卡訊息就是歡迎訊息,要求使用者填寫表單來提供資訊。使用者填寫表單後,系統就會傳送更新的應用程式首頁資訊卡訊息。這類更新必須透過包含 updateCard 導覽的 RenderActions 例項傳回。

Python

若是 HTTP 應用程式,更新應用程式首頁資訊卡訊息與處理使用者輸入的資訊類似,但您必須傳回 RenderActionsinvokedFunction 代表與 Card 小工具相關聯的叫用函式名稱。詳情請參閱 CommonEventObject。在以下範例中,submitForm 顯示使用者已提交表單資料:

@app.route('/', methods=['POST'])
def on_event():
  event = request.get_json()
  chat = event.get('chat')
  if chat is not None:
    return handle_chat_event(event)

def handle_chat_event(event):
  if event['chat'].get('type') == 'SUBMIT_FORM':
    event_object = event.get('commonEventObject')
    if event_object is not None:
      // Forms
      if 'submitForm' == event_object.get('invokedFunction'):
        return {
          'render_actions': {
            'action': {
              'navigations': [{
                'updateCard': get_update_card()
              }]
            }
          }
        }

def get_update_card():
  return {
      "action": {
          "navigations": [{
              "pushCard": {
                  "sections": [{
                      "widgets": [{
                          "buttonList": {
                              "buttons": [{
                                  "text": "Open documentation",
                                  "onClick": {
                                      "openLink": {
                                          "url": "https://developers.google.com/chat"
                                      }
                                  },
                              }]
                          }
                      }]
                  }]
              }
          }]
      }
  }

Apps Script

這個範例會傳回資訊卡 JSON,以傳送卡片訊息。您也可以使用 Apps Script 卡片服務

// Called from elsewhere (i.e. on button press).
function updateAppHomeCard(event) {
  return {
    render_actions: {
      action: {
        navigations: [
          {
            updateCard: getCard()
          }
        ]
      }
    }
  }
}

function getCard() {
  return {
    sections: [
      {
        widgets: [
          {
            buttonList: {
              buttons: [
                {
                  text: "Open documentation",
                  onClick: {
                    openLink: {
                      url: "https://developers.google.com/chat"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    ]
  };
}

限制

一般來說,navigation 不適用於 Chat 應用程式。您無法傳回堆疊的資訊卡。只有 pushCard (用於初始回應) 和 updateCard (用於更新) 適用於 Chat 應用程式。