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

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

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

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

Python

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

    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 يوضّح التفاعل الذي يتم إنشاؤه.