Bu kılavuzda, Reaction
kaynağında create
yönteminin nasıl kullanılacağı açıklanmaktadır.
Bir mesaja tepki eklemek için 👍, 🚲 ve 🌞 gibi bir Google Chat API'si ekleyin.
İlgili içeriği oluşturmak için kullanılan
Reaction
kaynak
kullanıcıların mesajlara tepki vermek için kullanabileceği bir emojiyi temsil eder (ör. 👍, 🚲,
ve 🌞.
Ö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ığı.
-
Bir masaüstü uygulaması için OAuth istemci kimliği kimlik bilgileri oluşturun. Örneği bu
yoksa, kimlik bilgilerini
client_secrets.json
adlı bir JSON dosyası olarak yerel dizini oluşturun.
- Kullanıcı kimlik doğrulamasını destekleyen bir yetkilendirme kapsamı seçin.
Mesaja tepki ekleme
Bir mesaja tepki oluşturmak için aşağıdakini istek:
chat.messages.reactions.create
,chat.messages.reactions
veyachat.messages
yetkilendirme kapsamı.- Şunu çağırın:
create
yöntem uygulamasındaReaction
kaynak. - Tepki verilecek mesajın kaynak adına
parent
değerini ayarlayın. body
öğesini (istek gövdesi) öğesinin bir örneği olarak ayarlayınReaction
Buradaunicode
alanı, unicode ile gösterilen standart bir emojidir. dize.
Aşağıdaki örnek, 😀 emojisiyle mesaja tepki veriyor:
Python
- Çalışma dizininizde
chat_reaction_create.py
adlı bir dosya oluşturun. Aşağıdaki kodu
chat_reaction_create.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.reactions.create"] def main(): ''' Authenticates with Chat API via user credentials, then creates a reaction to 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) # Use the service endpoint to call Chat API. result = chat.spaces().messages().reactions().create( # The message to create a reaction to. # # 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. parent = 'spaces/SPACE/messages/MESSAGE', # The reaction to the message. body = { 'emoji': { # A standard emoji represented by a unicode string. 'unicode': '😀' } } ).execute() # Prints details about the created reaction. print(result) if __name__ == '__main__': main()
Bu kodda, aşağıdakini değiştirin:
SPACE
: alanınname
. mesajı gönderilir. Bu mesajıspaces.list
yöntemi 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_reaction_create.py
Chat API,
Reaction
oluşturulan tepkiyi ayrıntılı bir şekilde ifade eder.