Mesajla ilgili ayrıntıları alma

Bu kılavuzda, get() yöntemindeki ayrıntıları döndürmek için Google Chat API'nin Message kaynağında kısa mesaj veya kart mesajıyla gösterilir.

Chat API'de bir Chat mesajı Message kaynak. Chat kullanıcıları yalnızca metin içeren mesajlar gönderebilirken Chat uygulamaları statik veya etkileşimli kullanıcı arayüzleri gösterme, kullanıcılardan bilgi toplama ve mesajları gizli olarak yayınlama gibi birçok mesajlaşma özelliğini kullanabilir. Mesajlaşma hakkında daha fazla bilgi edinmek için özellikleri hakkında daha fazla bilgi için Google Chat mesajlarına genel bakış

Ön koşullar

Node.js

  • Kimliği doğrulanmış kullanıcının veya Chat uygulamasını arayan kullanıcılar üye oldu. Chat uygulaması olarak kimlik doğrulama yapmak için Chat uygulamasını alana ekleyin.

Python

  • Kimliği doğrulanmış kullanıcının veya Chat uygulamasını arayan kullanıcılar üye oldu. Chat uygulaması olarak kimlik doğrulama yapmak için Chat uygulamasını alana ekleyin.

Java

  • Kimliği doğrulanmış kullanıcının veya Chat uygulamasını arayan kullanıcılar üye oldu. Chat uygulaması olarak kimlik doğrulama yapmak için Chat uygulamasını alana ekleyin.

Apps Komut Dosyası

Kullanıcı kimlik doğrulaması yapılmış bir mesaj alma

Şununla bir mesajla ilgili ayrıntıları almak için: kullanıcı kimlik doğrulaması, isteğinizde aşağıdakileri iletin:

  • chat.messages.readonly veya chat.messages yetkilendirme kapsamını belirtin.
  • GetMessage() yöntemini çağırın.
  • name değerini, alınacak mesajın kaynak adı olarak ayarlayın.

Aşağıdaki örnekte, kullanıcı kimlik doğrulaması içeren bir mesaj alınır:

Node.js

chat/client-libraries/cloud/get-message-user-cred.js
import {createClientWithUserCredentials} from './authentication-utils.js';

const USER_AUTH_OAUTH_SCOPES = ['https://www.googleapis.com/auth/chat.messages.readonly'];

// This sample shows how to get message with user credential
async function main() {
  // Create a client
  const chatClient = await createClientWithUserCredentials(USER_AUTH_OAUTH_SCOPES);

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME'
  };

  // Make the request
  const response = await chatClient.getMessage(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/get_message_user_cred.py
from authentication_utils import create_client_with_user_credentials
import google.oauth2.credentials

from google.apps import chat_v1 as google_chat

SCOPES = ["https://www.googleapis.com/auth/chat.messages.readonly"]

# This sample shows how to get message with user credential
def get_message_with_user_cred():
    # Create a client
    client = create_client_with_user_credentials(SCOPES)

    # Initialize request argument(s)
    request = google_chat.GetMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = "spaces/SPACE_NAME/messages/MESSAGE_NAME",
    )

    # Make the request
    response = client.get_message(request)

    # Handle the response
    print(response)

get_message_with_user_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMessageUserCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMessageRequest;
import com.google.chat.v1.Message;

// This sample shows how to get message with user credential.
public class GetMessageUserCred {

  private static final String SCOPE =
    "https://www.googleapis.com/auth/chat.messages.readonly";

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithUserCredentials(
          ImmutableList.of(SCOPE))) {
      GetMessageRequest.Builder request = GetMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/members/MESSAGE_NAME");
      Message response = chatServiceClient.getMessage(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Apps Komut Dosyası

chat/advanced-service/Main.gs
/**
 * This sample shows how to get message with user credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.messages.readonly'
 * referenced in the manifest file (appsscript.json).
 */
function getMessageUserCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';

  // Make the request
  const response = Chat.Spaces.Messages.get(name);

  // Handle the response
  console.log(response);
}

Bu örneği çalıştırmak için aşağıdakileri değiştirin:

  • SPACE_NAME: Alanın name kimliği. Kimliği öğrenmek için ListSpaces() yöntemini veya alanın URL'sini kullanın.
  • MESSAGE_NAME: İletinin name kimliği. Kimliği, aşağıdaki URL'yi oluşturduktan sonra döndürülen yanıt gövdesinden Chat API ile eşzamansız olarak veya özel ad , oluşturma sırasında mesaja atanır.

Chat API, Message öğesini tıklayın.

Uygulama kimlik doğrulaması içeren bir mesaj alma

Uygulama kimlik doğrulaması içeren bir mesajla ilgili ayrıntıları almak için isteğinizde aşağıdakileri iletin:

  • chat.bot yetkilendirme kapsamını belirtin.
  • GetMessage() yöntemini çağırın.
  • Alınacak mesajın kaynak adına name değerini ayarlayın.

Aşağıdaki örnekte, uygulama kimlik doğrulaması içeren bir mesaj alınmaktadır:

Node.js

chat/client-libraries/cloud/get-message-app-cred.js
import {createClientWithAppCredentials} from './authentication-utils.js';

// This sample shows how to get message with app credential
async function main() {
  // Create a client
  const chatClient = createClientWithAppCredentials();

  // Initialize request argument(s)
  const request = {
    // Replace SPACE_NAME and MESSAGE_NAME here
    name: 'spaces/SPACE_NAME/messages/MESSAGE_NAME'
  };

  // Make the request
  const response = await chatClient.getMessage(request);

  // Handle the response
  console.log(response);
}

main().catch(console.error);

Python

chat/client-libraries/cloud/get_message_app_cred.py
from authentication_utils import create_client_with_app_credentials
from google.apps import chat_v1 as google_chat

# This sample shows how to get message with app credential
def get_message_with_app_cred():
    # Create a client
    client = create_client_with_app_credentials()

    # Initialize request argument(s)
    request = google_chat.GetMessageRequest(
        # Replace SPACE_NAME and MESSAGE_NAME here
        name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME',
    )

    # Make the request
    response = client.get_message(request=request)

    # Handle the response
    print(response)

get_message_with_app_cred()

Java

chat/client-libraries/cloud/src/main/java/com/google/workspace/api/chat/samples/GetMessageAppCred.java
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetMessageRequest;
import com.google.chat.v1.Message;

// This sample shows how to get message with app credential.
public class GetMessageAppCred {

  public static void main(String[] args) throws Exception {
    try (ChatServiceClient chatServiceClient =
        AuthenticationUtils.createClientWithAppCredentials()) {
      GetMessageRequest.Builder request = GetMessageRequest.newBuilder()
        // replace SPACE_NAME and MESSAGE_NAME here
        .setName("spaces/SPACE_NAME/members/MESSAGE_NAME");
      Message response = chatServiceClient.getMessage(request.build());

      System.out.println(JsonFormat.printer().print(response));
    }
  }
}

Apps Komut Dosyası

chat/advanced-service/Main.gs
/**
 * This sample shows how to get message with app credential
 * 
 * It relies on the OAuth2 scope 'https://www.googleapis.com/auth/chat.bot'
 * used by service accounts.
 */
function getMessageAppCred() {
  // Initialize request argument(s)
  // TODO(developer): Replace SPACE_NAME and MESSAGE_NAME here
  const name = 'spaces/SPACE_NAME/messages/MESSAGE_NAME';
  const parameters = {};

  // Make the request
  const response = Chat.Spaces.Messages.get(name, parameters, getHeaderWithAppCredentials());

  // Handle the response
  console.log(response);
}

Bu örneği çalıştırmak için aşağıdaki kodu değiştirin:

  • SPACE_NAME: Alanın name kimliği. Kimliği öğrenmek için ListSpaces() yöntemini veya alanın URL'sini kullanın.
  • MESSAGE_NAME: İletinin name bölümündeki kimlik. Kimliği, Chat API ile mesaj oluşturduktan sonra döndürülen yanıt gövdesinden veya mesaj oluşturulurken mesaja atanan özel addan elde edebilirsiniz.

Chat API, Message öğesini tıklayın.