将 Google Chat 应用构建为网络钩子

本页面介绍如何设置网络钩子,以使用外部触发器将异步消息发送到 Chat 聊天室。例如,您可以配置一个监控应用,以在服务器出现故障时通知 Chat 上的值班人员。如需使用 Chat 应用发送同步消息,请参阅发送消息

对于此类架构设计,用户无法与 webhook 或连接的外部应用进行交互,因为通信是单向的。网络钩子不是对话式的。 它们无法响应或接收来自用户的消息或 Chat 应用互动事件。如需响应消息,请构建 Chat 应用,而不是网络钩子。

虽然从技术上讲,网络钩子并不是 Chat 应用(即网络钩子使用标准 HTTP 请求来连接应用),但为简单起见,本页面将其称为 Chat 应用。每个网络钩子只能在注册它的 Chat 聊天室中使用。传入的网络钩子适用于私信,但仅适用于所有用户启用 Chat 应用的情况。您无法将网络钩子发布到 Google Workspace Marketplace。

下图显示了连接到 Chat 的 webhook 的架构:

用于向 Chat 发送异步消息的传入网络钩子的架构。

在上图中,Chat 应用具有以下信息流:

  1. Chat 应用逻辑从外部第三方服务(例如项目管理系统或工单工具)接收信息。
  2. Chat 应用逻辑托管在云端或本地系统中,该系统可以使用网络钩子网址将消息发送到特定 Chat 聊天室。
  3. 用户可以从该特定 Chat 聊天室中的 Chat 应用接收消息,但无法与 Chat 应用交互。

前提条件

Python

  • Python 3.10.7 或更高版本。
  • 有权访问 ChatGoogle Workspace 帐号。
  • 一个现有的 Chat 聊天室。
  • httplib2 库。如有必要,请运行以下命令行界面 (CLI) 命令,以使用 pip 安装该库:

    pip install httplib2
    

Node.js

Java

Apps 脚本

创建网络钩子

如需创建网络钩子,请在要接收消息的 Chat 聊天室中注册该网络钩子,然后编写用于发送消息的脚本。

注册传入的网络钩子

  1. 在浏览器中,打开 Chat。 您无法通过 Chat 移动应用配置网络钩子。
  2. 转到要添加网络钩子的聊天室。
  3. 在聊天室标题旁边,点击 “展开”箭头,然后点击应用和集成
  4. 点击 Add webhook
  5. Name 字段中,输入 Quickstart Webhook
  6. 头像网址字段中,输入 https://developers.google.com/chat/images/chat-product-icon.png
  7. 点击保存
  8. 如需复制网络钩子网址,请点击 更多,然后点击 复制链接

编写网络钩子脚本

示例 webhook 脚本通过向 webhook 网址发送 POST 请求,将消息发送到注册 webhook 的空间。Chat API 会返回一个 Message 实例作为响应。

选择一种语言,了解如何创建网络钩子脚本:

Python

  1. 在您的工作目录中,创建一个名为 quickstart.py 的文件。

  2. quickstart.py 中,粘贴以下代码:

    python/webhook/quickstart.py
    from json import dumps
    from httplib2 import Http
    
    # Copy the webhook URL from the Chat space where the webhook is registered.
    # The values for SPACE_ID, KEY, and TOKEN are set by Chat, and are included
    # when you copy the webhook URL.
    
    def main():
        """Google Chat incoming webhook quickstart."""
        url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN"
        app_message = {"text": "Hello from a Python script!"}
        message_headers = {"Content-Type": "application/json; charset=UTF-8"}
        http_obj = Http()
        response = http_obj.request(
            uri=url,
            method="POST",
            headers=message_headers,
            body=dumps(app_message),
        )
        print(response)
    
    
    if __name__ == "__main__":
        main()
  3. url 变量的值替换为您在注册网络钩子时复制的网络钩子网址。

Node.js

  1. 在您的工作目录中,创建一个名为 index.js 的文件。

  2. index.js 中,粘贴以下代码:

    node/webhook/index.js
    /**
     * Sends asynchronous message to Google Chat
     * @return {Object} response
     */
    async function webhook() {
      const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages"
      const res = await fetch(url, {
        method: "POST",
        headers: {"Content-Type": "application/json; charset=UTF-8"},
        body: JSON.stringify({text: "Hello from a Node script!"})
      });
      return await res.json();
    }
    
    webhook().then(res => console.log(res));
  3. url 变量的值替换为您在注册网络钩子时复制的网络钩子网址。

Java

  1. 在您的工作目录中,创建一个名为 pom.xml 的文件。

  2. pom.xml 中,复制并粘贴以下内容:

    java/webhook/pom.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.google.chat.webhook</groupId>
      <artifactId>java-webhook-app</artifactId>
      <version>0.1.0</version>
    
      <name>java-webhook-app</name>
      <url>https://github.com/googleworkspace/google-chat-samples/tree/main/java/webhook</url>
    
      <properties>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
      </properties>
    
      <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.9.1</version>
        </dependency>
      </dependencies>
    
      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>
  3. 在您的工作目录中,创建以下目录结构 src/main/java

  4. src/main/java 目录中,创建一个名为 App.java 的文件。

  5. App.java 中,粘贴以下代码:

    java/webhook/src/main/java/com/google/chat/webhook/App.java
    import com.google.gson.Gson;
    import java.net.http.HttpClient;
    import java.net.http.HttpRequest;
    import java.net.http.HttpResponse;
    import java.util.Map;
    import java.net.URI;
    
    public class App {
      private static final String URL = "https://chat.googleapis.com/v1/spaces/AAAAGCYeSRY/messages";
      private static final Gson gson = new Gson();
      private static final HttpClient client = HttpClient.newHttpClient();
    
      public static void main(String[] args) throws Exception {
        String message = gson.toJson(Map.of("text", "Hello from Java!"));
    
        HttpRequest request = HttpRequest.newBuilder(
            URI.create(URL))
            .header("accept", "application/json; charset=UTF-8")
            .POST(HttpRequest.BodyPublishers.ofString(message))
            .build();
    
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    
        System.out.println(response.body());
      }
    }
  6. URL 变量的值替换为您在注册网络钩子时复制的网络钩子网址。

Apps 脚本

  1. 在浏览器中,转到 Apps 脚本

  2. 点击 New Project

  3. 粘贴以下代码:

    apps-script/webhook/webhook.gs
    function webhook() {
      const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages"
      const options = {
        "method": "post",
        "headers": {"Content-Type": "application/json; charset=UTF-8"},
        "payload": JSON.stringify({"text": "Hello from Apps Script!"})
      };
      const response = UrlFetchApp.fetch(url, options);
      console.log(response);
    }
  4. url 变量的值替换为您在注册网络钩子时复制的网络钩子网址。

运行 webhook 脚本

在 CLI 中,运行以下脚本:

Python

  python3 quickstart.py

Node.js

  node index.js

Java

  mvn compile exec:java -Dexec.mainClass=App

Apps 脚本

  • 点击运行

运行代码时,webhook 将向您注册它的空间发送一条消息。

发起或回复消息串

  1. 在消息请求正文中指定 spaces.messages.thread.threadKey。根据您是发起还是回复线程,使用以下 threadKey 值:

    • 如果启动线程,请将 threadKey 设置为任意字符串,但请记下此值,以便向线程发布回复。

    • 如果是回复某个线程,请指定在启动该线程时设置的 threadKey。例如,如需向初始消息使用 MY-THREAD 的线程发布回复,请设置 MY-THREAD

  2. 定义未找到指定的 threadKey 时的线程行为:

    • 回复消息串或发起新消息串。将 messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD 参数添加到网络钩子网址。传递此网址参数会导致 Chat 使用指定的 threadKey 查找现有会话。如果找到消息,则该消息会回复该消息串。如果找不到任何消息,消息会启动与该 threadKey 对应的新线程。

    • 您可以回复会话或不执行任何操作。将 messageReplyOption=REPLY_MESSAGE_OR_FAIL 参数添加到网络钩子网址。传递此网址参数会导致 Chat 使用指定的 threadKey 查找现有会话。如果找到消息,则该消息会回复该消息串。如果未找到任何消息,则不会发送消息。

    如需了解详情,请参阅 messageReplyOption

以下代码示例启动或回复某个消息会话:

Python

python/webhook/thread-reply.py
from json import dumps
from httplib2 import Http

# Copy the webhook URL from the Chat space where the webhook is registered.
# The values for SPACE_ID, KEY, and TOKEN are set by Chat, and are included
# when you copy the webhook URL.
#
# Then, append messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD to the
# webhook URL.


def main():
    """Google Chat incoming webhook that starts or replies to a message thread."""
    url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"
    app_message = {
        "text": "Hello from a Python script!",
        # To start a thread, set threadKey to an arbitratry string.
        # To reply to a thread, specify that thread's threadKey value.
        "thread": {"threadKey": "THREAD_KEY_VALUE"},
    }
    message_headers = {"Content-Type": "application/json; charset=UTF-8"}
    http_obj = Http()
    response = http_obj.request(
        uri=url,
        method="POST",
        headers=message_headers,
        body=dumps(app_message),
    )
    print(response)


if __name__ == "__main__":
    main()

Node.js

node/webhook/thread-reply.js
/**
 * Sends asynchronous message to Google Chat
 * @return {Object} response
 */
async function webhook() {
  const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"
  const res = await fetch(url, {
    method: "POST",
    headers: {"Content-Type": "application/json; charset=UTF-8"},
    body: JSON.stringify({
      text: "Hello from a Node script!",
      thread: {threadKey: "THREAD_KEY_VALUE"}
    })
  });
  return await res.json();
}

webhook().then(res => console.log(res));

Apps 脚本

apps-script/webhook/thread-reply.gs
function webhook() {
  const url = "https://chat.googleapis.com/v1/spaces/SPACE_ID/messages?key=KEY&token=TOKEN&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD"
  const options = {
    "method": "post",
    "headers": {"Content-Type": "application/json; charset=UTF-8"},
    "payload": JSON.stringify({
      "text": "Hello from Apps Script!",
      "thread": {"threadKey": "THREAD_KEY_VALUE"}
    })
  };
  const response = UrlFetchApp.fetch(url, options);
  console.log(response);
}