واکنش‌ها را برای یک پیام فهرست کنید

این راهنما نحوه استفاده از روش list را در منبع Reaction در Google Chat API برای فهرست کردن واکنش‌های یک پیام توضیح می‌دهد - مانند 👍، 🚲، و 🌞.

منبع Reaction نشان‌دهنده شکلک‌هایی است که افراد می‌توانند از آن برای واکنش به پیامی مانند 👍، 🚲 و 🌞 استفاده کنند.

پیش نیازها

پایتون

  • پایتون 3.6 یا بالاتر
  • ابزار مدیریت بسته پیپ
  • جدیدترین کتابخانه های مشتری گوگل برای پایتون. برای نصب یا به روز رسانی آنها، دستور زیر را در رابط خط فرمان خود اجرا کنید:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • یک پروژه Google Cloud با Google Chat API فعال و پیکربندی شده است. برای مراحل، به ساخت برنامه گپ Google مراجعه کنید.
  • مجوز برای برنامه چت پیکربندی شد. واکنش‌های فهرست‌شده به تأیید اعتبار کاربر با محدوده مجوز chat.messages.reactions.readonly ، chat.messages.reactions ، chat.messages.readonly یا chat.messages نیاز دارد.

واکنش ها را فهرست کنید

برای فهرست کردن واکنش‌های یک پیام، موارد زیر را در درخواست خود بنویسید:

  • محدوده مجوز chat.messages.reactions.readonly ، chat.messages.reactions ، chat.messages.readonly ، یا chat.messages را مشخص کنید.
  • [ list method]/workspace(/chat/api/reference/rest/v1/spaces.messages.reactions/list) را در منبع Reaction فراخوانی کنید.

مثال زیر واکنش های یک پیام مشخص را فهرست می کند:

پایتون

  1. در پوشه کاری خود، فایلی با نام chat_reactions_list.py ایجاد کنید.
  2. کد زیر را در chat_reactions_list.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.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists reactions 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().list(
    
            # The message to list reactions 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'
    
        ).execute()
    
        # Prints details about the created reactions.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. در کد زیر را جایگزین کنید:

    • SPACE : نام فضایی است که می توانید از روش spaces.list در Chat API یا از URL یک فضا دریافت کنید.
    • MESSAGE : نام پیامی است که می توانید آن را از بدنه پاسخی که پس از ایجاد پیام به طور ناهمزمان با Chat API یا با نام سفارشی اختصاص داده شده به پیام در هنگام ایجاد، بازگردانده شده است، دریافت کنید.
  4. در پوشه کاری خود، نمونه را بسازید و اجرا کنید:

    python3 chat_reactions_list.py
    

Chat API یک آرایه صفحه بندی شده از واکنش ها را برمی گرداند.