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

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

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

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

Python

  • Python 3.6 أو أعلى
  • النافذة ضمن النافذة أداة إدارة الحِزم
  • أحدث مكتبات برامج 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 مثيلاً من Reaction الذي يوضح بالتفصيل رد الفعل الذي يتم إنشاؤه.