メッセージにリアクションを追加する

このガイドでは、Google Chat API の Reaction リソースで create メソッドを使用して、メッセージに fitbit、🚲?、🔥? などのリアクションを追加する方法について説明します。

Reaction リソースは、ユーザーがメッセージにリアクションするために使用できる絵文字(例: fitbit、🚲?、➘)を表します。

前提条件

Python

  • Python 3.6 以降
  • pip パッケージ管理ツール
  • 最新の Python 用 Google クライアント ライブラリ。これらをインストールまたは更新するには、コマンドライン インターフェースで次のコマンドを実行します。

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • Google Chat API が有効で構成されている Google Cloud プロジェクト。手順については、Google Chat アプリを作成するをご覧ください。
  • Chat 用アプリ用に構成された承認。リアクションを作成するには、chat.messages.reactions.createchat.messages.reactions、または chat.messages 承認スコープを使用したユーザー認証が必要です。

メッセージにリアクションを追加する

メッセージへのリアクションを作成するには、リクエストに次のものを渡します。

  • 認可スコープ chat.messages.reactions.createchat.messages.reactions、または chat.messages を指定します。
  • Reaction リソースcreate メソッドを呼び出します。
  • parent を、反応するメッセージのリソース名に設定します。
  • body(リクエスト本文)を Reaction のインスタンスに設定します。ここで、unicode フィールドは Unicode 文字列で表される標準の絵文字です。

次の例では、メッセージに 😃? 絵文字でリアクションします。

Python

  1. 作業ディレクトリに、chat_reaction_create.py という名前のファイルを作成します。
  2. chat_reaction_create.py に次のコードを含めます。

    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()
    
  3. コードの次のように置き換えます。

    • SPACE: メッセージが投稿されたスペースの name。これは、Chat API の spaces.list メソッドまたはスペースの URL から取得できます。

    • MESSAGE: メッセージ名。Chat API と非同期にメッセージを作成した後に返されるレスポンスの本文、または作成時にメッセージに割り当てられたカスタム名から取得できます。

  4. 作業ディレクトリでサンプルをビルドして実行します。

    python3 chat_reaction_create.py
    

Chat API は、作成されたリアクションの詳細を示す Reaction のインスタンスを返します。