إضافة تفاعل إلى رسالة

يشرح هذا الدليل طريقة استخدام طريقة create على مورد Reaction في Google Chat API من أجل إضافة تفاعل إلى رسالة، مثل 👍 و🚲 و لتجميع المُعلِن.

يمثّل مورد Reaction رمزًا تعبيريًا يمكن للمستخدمين استخدامه للتفاعل مع رسالة، مثل 👍 و🚲 وأو 😲.

المتطلّبات الأساسية

Python

  • Python 3.6 أو أعلى
  • تتيح لك أداة pip إدارة الحزم
  • أحدث مكتبات برامج Google للغة بايثون. لتثبيتها أو تحديثها، قم بتشغيل الأمر التالي في واجهة سطر الأوامر لديك:

    pip3 install --upgrade google-api-python-client google-auth-oauthlib
    
  • مشروع على Google Cloud مع تفعيل وإعداد Google Chat API لمعرفة الخطوات، راجِع إنشاء تطبيق Google Chat.
  • تم ضبط التفويض لتطبيق Chat. لإنشاء التفاعل، يتطلّب إنشاء التفاعل مصادقة المستخدم مع نطاق التفويض chat.messages.reactions.create أو chat.messages.reactions أو chat.messages.

إضافة تفاعل إلى رسالة

لإنشاء تفاعل مع رسالة، أدخِل ما يلي في طلبك:

  • حدِّد نطاق التفويض chat.messages.reactions.create أو chat.messages.reactions أو chat.messages.
  • استدعِ طريقة create في مرجع Reaction.
  • اضبط parent على اسم مورد الرسالة التي تريد التفاعل معها.
  • اضبط body (نص الطلب) على مثيل Reaction الذي يكون فيه الحقل 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 للمساحة التي يتم نشر الرسالة فيها، والتي يمكنك الحصول عليها من خلال طريقة spaces.list في Chat API أو من عنوان URL للمساحة.

    • MESSAGE: اسم رسالة يمكنك الحصول عليه من نص الاستجابة الذي تم عرضه بعد إنشاء رسالة بشكل غير متزامن باستخدام Chat API، أو باستخدام الاسم المخصّص الذي تم تحديده للرسالة عند إنشائها

  4. في دليل العمل، أنشئ النموذج وشغِّله:

    python3 chat_reaction_create.py
    

تعرض واجهة Chat API مثالاً على Reaction يفصّل التفاعل الذي تم إنشاؤه.