Google Chat 앱을 웹훅으로 빌드하기

이 페이지에서는 외부 트리거를 사용하여 Chat 스페이스에 비동기 메시지를 보내도록 웹훅을 설정하는 방법을 설명합니다. 예를 들어 서버가 다운되면 Chat의 긴급 대기 직원에게 알리도록 모니터링 애플리케이션을 구성할 수 있습니다. 채팅 앱으로 동기 메시지를 보내려면 메시지 보내기를 참고하세요.

이러한 유형의 아키텍처 설계에서는 통신이 단방향이므로 사용자가 웹훅 또는 연결된 외부 애플리케이션과 상호작용할 수 없습니다. 웹훅은 대화형이 아닙니다. 사용자 또는 Chat 앱 상호작용 이벤트에 응답하거나 이러한 이벤트의 메시지를 수신할 수 없습니다. 메시지에 응답하려면 웹훅 대신 채팅 앱을 빌드하세요.

웹훅은 엄밀히 말해 채팅 앱이 아니지만(웹훅은 표준 HTTP 요청을 사용하여 애플리케이션을 연결함) 이 페이지에서는 웹훅을 편의상 채팅 앱이라고 합니다. 각 웹훅은 등록된 Chat 스페이스에서만 작동합니다. 수신 웹훅은 채팅 메시지에서 작동하지만 모든 사용자가 채팅 앱을 사용 설정한 경우에만 작동합니다. Google Workspace Marketplace에 웹훅을 게시할 수 없습니다.

다음 다이어그램은 Chat에 연결된 웹훅의 아키텍처를 보여줍니다.

Chat에 비동기 메시지를 보내기 위한 수신 웹훅의 아키텍처

앞의 다이어그램에서 채팅 앱에는 다음과 같은 정보 흐름이 있습니다.

  1. 채팅 앱 로직은 프로젝트 관리 시스템 또는 티켓 판매 도구와 같은 외부 서드 파티 서비스로부터 정보를 수신합니다.
  2. 채팅 앱 로직은 웹훅 URL을 사용하여 특정 Chat 스페이스에 메시지를 보낼 수 있는 클라우드 또는 온프레미스 시스템에서 호스팅됩니다.
  3. 사용자는 특정 Chat 스페이스의 채팅 앱에서 메시지를 수신할 수 있지만 채팅 앱과 상호작용할 수는 없습니다.

기본 요건

Python

  • Chat에 액세스할 수 있는 Google Workspace 계정 Google Workspace 조직에서 사용자가 수신 웹훅을 추가하고 사용할 수 있도록 허용해야 합니다.
  • Python 3.10.7 이상
  • httplib2 라이브러리. 필요한 경우 다음 명령줄 인터페이스 (CLI) 명령어를 실행하여 pip를 사용하는 라이브러리를 설치합니다.

    pip install httplib2
    
  • 기존 Chat 스페이스

Node.js

Java

Apps Script

웹훅 만들기

웹훅을 만들려면 메시지를 수신하려는 Chat 스페이스에 등록한 후 메시지를 전송하는 스크립트를 작성하세요.

수신 웹훅 등록

  1. 브라우저에서 Chat을 엽니다. Chat 모바일 앱에서는 웹훅을 구성할 수 없습니다.
  2. 웹훅을 추가할 스페이스로 이동합니다.
  3. 스페이스 제목 옆에 있는 를 클릭하고 더보기 화살표를 클릭한 다음 앱 및 통합을 클릭합니다.
  4. Add webhooks(웹훅 추가)를 클릭합니다.

  5. 이름 필드에 Quickstart Webhook를 입력합니다.

  6. 아바타 URL 필드에 https://developers.google.com/chat/images/chat-product-icon.png를 입력합니다.

  7. 저장을 클릭합니다.

  8. 웹훅 URL을 복사하려면 더보기를 클릭한 후 링크 복사를 클릭합니다.

웹훅 스크립트 작성

웹훅 스크립트 예시는 웹훅 URL에 POST 요청을 전송하여 웹훅이 등록된 공간에 메시지를 전송합니다. 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 변수의 값을 웹훅을 등록할 때 복사한 웹훅 URL로 바꿉니다.

Node.js

  1. 작업 디렉터리에 index.js라는 파일을 만듭니다.

  2. index.js에 다음 코드를 붙여넣습니다.

    노드/웹훅/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 변수의 값을 웹훅을 등록할 때 복사한 웹훅 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 변수의 값을 웹훅을 등록할 때 복사한 웹훅 URL로 바꿉니다.

Apps Script

  1. 브라우저에서 Apps Script로 이동합니다.

  2. 새 프로젝트를 클릭합니다.

  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 변수의 값을 웹훅을 등록할 때 복사한 웹훅 URL로 바꿉니다.

웹훅 스크립트 실행

CLI에서 스크립트를 실행합니다.

Python

  python3 quickstart.py

Node.js

  node index.js

Java

  mvn compile exec:java -Dexec.mainClass=App

Apps Script

  • 실행을 클릭합니다.

코드를 실행하면 웹훅이 코드가 등록된 스페이스로 메시지를 전송합니다.

메시지 대화목록 시작 또는 답장

  1. spaces.messages.thread.threadKey를 메시지 요청 본문의 일부로 지정합니다. 스레드를 시작하는지 또는 스레드에 응답하는지에 따라 threadKey에 다음 값을 사용합니다.

    • 스레드를 시작하는 경우 threadKey를 임의의 문자열로 설정하지만 이 값을 기록하여 스레드에 회신을 게시합니다.

    • 스레드에 응답하는 경우 스레드가 시작되었을 때 설정된 threadKey를 지정합니다. 예를 들어 초기 메시지에서 MY-THREAD를 사용한 스레드에 답장을 게시하려면 MY-THREAD를 설정합니다.

  2. 지정된 threadKey를 찾을 수 없는 경우 스레드 동작을 정의합니다.

    • 대화목록에 답장하거나 새 대화목록을 시작합니다. 웹훅 URL에 messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD 매개변수를 추가합니다. 이 URL 매개변수를 전달하면 Chat에서 지정된 threadKey를 사용하여 기존 스레드를 찾습니다. 일치하는 대화목록이 있으면 메시지가 이 대화목록에 답장으로 게시됩니다. 스레드가 발견되지 않으면 메시지는 threadKey에 상응하는 새 스레드를 시작합니다.

    • 대화목록에 답장하거나 아무 작업도 하지 않습니다. 웹훅 URL에 messageReplyOption=REPLY_MESSAGE_OR_FAIL 매개변수를 추가합니다. 이 URL 매개변수를 전달하면 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 Script

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);
}