حذف یک واکنش از یک پیام

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

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

پیش نیازها

پایتون

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

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

یک واکنش را حذف کنید

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

  • chat.messages.reactions یا محدوده مجوز chat.messages مشخص کنید.
  • متد delete را در منبع Reaction فراخوانی کنید.
  • name به نام منبع واکنش برای حذف تنظیم کنید.

مثال زیر واکنش 😀 را از یک پیام حذف می کند:

پایتون

  1. در پوشه کاری خود، فایلی با نام chat_reaction_delete.py ایجاد کنید.
  2. کد زیر را در chat_reaction_delete.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"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then deletes 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().delete(
    
            # The reaction to delete.
            #
            # 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.
            #
            # Replace REACTION with a reaction name.
            # Obtain the reaction name from the reaction resource of Chat API.
            name = 'spaces/SPACE/messages/MESSAGE/reactions/REACTION'
    
        ).execute()
    
    if __name__ == '__main__':
        main()
    
  3. در کد زیر را جایگزین کنید:

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

    python3 chat_reaction_delete.py
    

در صورت موفقیت آمیز بودن، بدنه پاسخ خالی است، که نشان می دهد واکنش حذف شده است.