为 Google Chat 应用构建首页

本页将介绍如何为 Google Chat 应用。应用首页是一个可自定义的卡片界面 用户打开直接 消息。

包含两个微件的应用首页卡片。

例如,您可以 配置应用首页卡片消息,以包含与 Chat 扩展应用 斜杠命令。对于最终用户,应用主屏幕是 只有在 Chat 应用的私信中 应用开发者启用相应功能


使用卡片构建器设计和预览聊天应用的 JSON 卡片消息:

打开卡片制作工具

前提条件

Python

Apps 脚本

在 Google Cloud 控制台中配置

Python

  1. 在 Google Cloud 控制台中,点击菜单 > 更多产品 > Google Workspace > 产品库 > Google Chat API

    前往 Google Chat API

  2. 点击管理,然后点击配置标签页。

  3. 启用支持应用主屏幕

  4. 选中 Support App Home 复选框。

  5. App Home 网址(应用主屏幕网址)字段中,添加网址。这个值通常与 作为应用网址的网址。此网址是针对 APP_HOME 事件

  6. 点击保存

Apps 脚本

  1. 在 Google Cloud 控制台中,点击菜单 > 更多产品 > Google Workspace > 产品库 > Google Chat API

    前往 Google Chat API

  2. 点击管理,然后点击配置标签页。

  3. 选中 Support App Home 复选框。

  4. 点击保存

配置 Chat 应用

在应用主屏幕中配置 Chat 应用。

Python

用户通过 Chat 应用打开私信时, APP_HOME 事件会发送到您的 Chat 应用。当 Chat 应用收到此事件,它是 RenderActions 通过 pushCard 导航返回。

@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 脚本

此示例通过返回 卡片 JSON。 您还可以使用 Apps 脚本卡片服务

实现 onAppHome 函数以返回以下对象的 JSON 实例: RenderActions 使用 pushCard 导航:

// "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"
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    ]
  };
}

更新应用首页卡片

应用首页卡片可以在用户提交信息或关闭 对话框。例如,初始应用首页卡片包含欢迎辞 要求用户在表单中填写信息。在用户完成 表单,应用首页卡片会更新。更新必须随实例一起返回 共 RenderActions 个 包含 updateCard 导航。

Python

对于 HTTP 应用,更新应用首页卡片的过程类似于 处理用户输入的信息, 但您必须返回 RenderActionsinvokedFunction 表示 与 Card widget 关联的已调用函数。如需更多信息 请参阅 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 脚本

此示例通过返回 卡片 JSON。 您还可以使用 Apps 脚本卡片服务

// 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(针对更新) 。