Bu kılavuzda, patch
yönteminin Message
kaynağında nasıl kullanılacağı açıklanmaktadır.
Google Chat API'yi kullanarak alandaki kısa mesaj veya kart mesajını güncelleyin. Bir
mesajının içeriği veya mesajın içeriği gibi mesaj özelliklerini
kartını inceleyebilirsiniz. İsterseniz
kart gösterebilir veya kısa mesaja kart ekleyebilirsiniz.
Chat API,
update
yöntem,
ancak
patch
yöntemi
çünkü aynı zamanda PATCH
update
şunları kullanır:
PUT
HTTP isteği. Daha fazla bilgi edinmek için
AIP-134'ün PATCH
ve PUT
bölümü.
Chat API'de bir Chat mesajı
Message
kaynak.
Chat kullanıcıları yalnızca metin içeren mesajlar gönderebilir.
Chat uygulamaları, aşağıdakiler de dahil olmak üzere pek çok mesajlaşma özelliğini kullanabilir:
statik veya etkileşimli kullanıcı arayüzleri gösterme,
ve mesajları gizli olarak teslim edebilirsiniz. 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
Python
- İşletme veya Kuruluş Şuna erişimi olan Google Workspace hesabı: Google Chat.
- Ortamınızı ayarlayın:
- Bir Google Cloud projesi oluşturun.
- OAuth izin ekranını yapılandırın.
- Google Chat API'yi etkinleştirin ve yapılandırın. Bunun için bir ad girin, Chat uygulamanızın simgesi ve açıklaması.
- Şunu yükleyin: Python Google API İstemci Kitaplığı.
- Google Chat API'nizde nasıl kimlik doğrulama yapmak istediğinize bağlı olarak erişim kimlik bilgilerini oluşturun
istek:
- Chat kullanıcısı olarak kimliğinizi doğrulamak için:
OAuth istemci kimliği oluştur
kimlik bilgilerini iki kez tıklayın ve kimlik bilgilerini
client_secrets.json
dosyasını yerel dizininize ekleyin. - Chat uygulaması olarak kimliğinizi doğrulamak için:
hizmet hesabı oluştur
kimlik bilgilerini iki kez tıklayın ve kimlik bilgilerini
credentials.json
.
- Chat kullanıcısı olarak kimliğinizi doğrulamak için:
OAuth istemci kimliği oluştur
kimlik bilgilerini iki kez tıklayın ve kimlik bilgilerini
- Kimlik doğrulamasını kullanıcı olarak mı yoksa Chat uygulaması.
Kullanıcı kimlik doğrulamasıyla kısa mesajı güncelleme veya kart mesajına kısa mesaj ekleme
Bir kısa mesaj şununla: kullanıcı kimlik doğrulaması, geçiş şunları belirtin:
chat.messages
yetkilendirme kapsamı.- Güncellenecek mesajın
name
öğesi. updateMask='text'
- Güncellenen mesajı belirten
body
.
Güncellenen mesaj kart mesajı, Kart mesajının başına kısa mesaj eklenir (göstermeye devam eder).
Bir kısa mesaj veya şuna bir kısa mesaj ekle: kart mesajı şununla: kullanıcı kimlik doğrulaması:
Python
- Çalışma dizininizde şu ada sahip bir dosya oluşturun:
chat_update_text_message_user.py
Aşağıdaki kodu
chat_update_text_message_user.py
bölümüne ekleyin:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Define your app's authorization scopes. # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.messages"] def main(): ''' Authenticates with Chat API via user credentials, then updates a message. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MESSAGE with a message name. # Obtain the message name from the response body returned # after creating a message asynchronously with Chat REST API. name='spaces/SPACE/messages/MESSAGE', updateMask='text', body={'text': 'Updated message!'} ).execute() # Prints details about the updated message. print(result) if __name__ == '__main__': main()
Bu kodda, aşağıdakini değiştirin:
SPACE
: şuradan alabileceğiniz bir alan adı: "the"spaces.list
yöntem API'den veya bir alanın URL'sinden.MESSAGE
: edinebileceğiniz bir mesaj adı eşzamansız olarak bir ileti oluşturduktan sonra döndürülen yanıt gövdesinden veya özel ad , oluşturma sırasında mesaja atanır.
Çalışma dizininizde örneği derleyin ve çalıştırın:
python3 chat_update_text_message_user.py
Uygulama kimlik doğrulaması ile kısa mesajı güncelleme veya kart mesajına kısa mesaj ekleme
Bir kısa mesaj şununla: uygulama kimlik doğrulaması, isteğinizde aşağıdakileri iletin:
chat.bot
yetkilendirme kapsamı.- Güncellenecek mesajın
name
öğesi. updateMask='text'
- Güncellenen mesajı belirten
body
.
Güncellenmiş mesaj bir kart mesajıysa Kart mesajının başına kısa mesaj eklenir (göstermeye devam eder).
Bir kısa mesaj bir metin mesajının başına veya kart mesajı şununla: uygulama kimlik doğrulaması:
Python
- Çalışma dizininizde şu ada sahip bir dosya oluşturun:
chat_update_text_message_app.py
Aşağıdaki kodu
chat_update_text_message_app.py
bölümüne ekleyin:from google.oauth2 import service_account from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', credentials=CREDENTIALS) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MESSAGE with a message name. # Obtain the message name from the response body returned # after creating a message asynchronously with Chat REST API. name='spaces/SPACE/messages/MESSAGE', updateMask='text', body={'text': 'Updated message!'} ).execute() # Print Chat API's response in your command line interface. print(result)
Bu kodda, aşağıdakini değiştirin:
SPACE
: şuradan alabileceğiniz bir alan adı: "the"spaces.list
yöntem API'den veya bir alanın URL'sinden.MESSAGE
: edinebileceğiniz bir mesaj adı eşzamansız olarak bir ileti oluşturduktan sonra döndürülen yanıt gövdesinden veya özel ad , oluşturma sırasında mesaja atanır.
Çalışma dizininizde örneği derleyin ve çalıştırın:
python3 chat_update_text_message_app.py
Kart mesajını güncelleme veya kısa mesaja kart mesajı ekleme
Bir kart mesajı, isteğinizde aşağıdakileri iletin:
chat.bot
yetkilendirme kapsamı. Kart mesajı güncellemek için uygulama kimlik doğrulaması.- Güncellenecek mesajın
name
öğesi. updateMask='cardsV2'
- Güncellenen mesajı belirten
body
.
Güncellenen mesaj kısa mesaj, ardından kısa mesaja bir kart eklenir (göstermeye devam eder). Öğe güncellenmiş mesajın kendisi card ise gösterilen kart güncellendi.
Bir mesajı kart mesajı:
Python
- Çalışma dizininizde şu ada sahip bir dosya oluşturun:
chat_update_card_message.py
Aşağıdaki kodu
chat_update_card_message.py
bölümüne ekleyin:from google.oauth2 import service_account from apiclient.discovery import build # Specify required scopes. SCOPES = ['https://www.googleapis.com/auth/chat.bot'] # Specify service account details. CREDENTIALS = ( service_account.Credentials.from_service_account_file('credentials.json') .with_scopes(SCOPES) ) # Build the URI and authenticate with the service account. chat = build('chat', 'v1', credentials=CREDENTIALS) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MESSAGE with a message name. # Obtain the message name from the response body returned # after creating a message asynchronously with Chat REST API. name='spaces/SPACE/messages/MESSAGE', updateMask='cardsV2', body= { 'cardsV2': [{ 'cardId': 'updateCardMessage', 'card': { 'header': { 'title': 'An Updated Card Message!', 'subtitle': 'Updated with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() # Print Chat API's response in your command line interface. print(result)
Bu kodda, aşağıdakini değiştirin:
SPACE
: şuradan alabileceğiniz bir alan adı: "the"spaces.list
yöntem API'den veya bir alanın URL'sinden.MESSAGE
: edinebileceğiniz bir mesaj adı eşzamansız olarak bir ileti oluşturduktan sonra döndürülen yanıt gövdesinden veya özel ad , oluşturma sırasında mesaja atanır.
Çalışma dizininizde örneği derleyin ve çalıştırın:
python3 chat_update_card_message.py
Chat API,
Message
güncelleme mesajının ayrıntılarını içerir.
Bir mesajı birden çok alan yoluyla aynı anda güncelleme
Bir mesaj güncellendiğinde birden fazla mesaj alanı yolunu
gerekir. Örneğin, bir güncelleme mesajı isteğinde,
text
ve cardsv2
alan yollarını aynı anda günceller. Bu işlem hem
mesajın metnini ve kartını değiştirin. Mesajda yalnızca metin varsa ve kart yoksa bir kart
eklenir. Desteklenen alan yolları hakkında daha fazla bilgi için
bkz.
updateMask
parametreleri.
Hem
text
ve
card
şu mesajı içeren:
kullanıcı kimlik doğrulaması,
isteğinizde aşağıdakileri iletin:
chat.messages
yetkilendirme kapsamı.- Güncellenecek mesajın
name
öğesi. Güncellenecek mesaj alanı yollarını belirten
updateMask
virgülle ayrılmış:updateMask='text', 'cardsV2'
.Güncellenen tüm alanlar dahil olmak üzere, güncellenen mesajı belirten bir
body
yollar.
Şurada text
ve cardsV2
alan yollarının nasıl güncelleneceği aşağıda açıklanmıştır:
şununla mesaj gönder:
kullanıcı kimlik doğrulaması:
Python
- Çalışma dizininizde şu ada sahip bir dosya oluşturun:
chat_update_text_message_user.py
Aşağıdaki kodu
chat_update_text_message_user.py
bölümüne ekleyin:from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient.discovery import build # Define your app's authorization scopes. # When modifying these scopes, delete the file token.json, if it exists. SCOPES = ["https://www.googleapis.com/auth/chat.messages"] def main(): ''' Authenticates with Chat API via user credentials, then updates a message. ''' # Authenticate with Google Workspace # and get user authorization. flow = InstalledAppFlow.from_client_secrets_file( 'client_secrets.json', SCOPES) creds = flow.run_local_server() # Build a service endpoint for Chat API. chat = build('chat', 'v1', credentials=creds) # Update a Chat message. result = chat.spaces().messages().patch( # The message to update, and the updated message. # # Replace SPACE with a space name. # Obtain the space name from the spaces resource of Chat API, # or from a space's URL. # # Replace MESSAGE with a message name. # Obtain the message name from the response body returned # after creating a message asynchronously with Chat REST API. name='spaces/SPACE/messages/MESSAGE', updateMask='text,cardsV2', body= {'text': 'Updated message!', 'cardsV2': [{ 'cardId': 'updateCardMessage', 'card': { 'header': { 'title': 'An Updated Card Message!', 'subtitle': 'Updated with Chat REST API', 'imageUrl': 'https://developers.google.com/chat/images/chat-product-icon.png', 'imageType': 'CIRCLE' }, 'sections': [ { 'widgets': [ { 'buttonList': { 'buttons': [ { 'text': 'Read the docs!', 'onClick': { 'openLink': { 'url': 'https://developers.google.com/chat' } } } ] } } ] } ] } }] } ).execute() # Prints details about the updated message. print(result) if __name__ == '__main__': main()
Bu kodda, aşağıdakini değiştirin:
SPACE
: şuradan alabileceğiniz bir alan adı: "the"spaces.list
yöntem API'den veya bir alanın URL'sinden.MESSAGE
: edinebileceğiniz bir mesaj adı eşzamansız olarak bir ileti oluşturduktan sonra döndürülen yanıt gövdesinden veya özel ad , oluşturma sırasında mesaja atanır.
Çalışma dizininizde örneği derleyin ve çalıştırın:
python3 chat_update_text_message_user.py
İlgili konular
- İletiyi biçimlendirme
- İletiyi silme
- Bir mesajla ilgili ayrıntıları öğrenme
- Bir alandaki mesajları listeleyin.
- Mesaj gönderin.