تثبيت الرسائل أو إزالة تثبيتها في مساحات Google Chat

باستخدام مورد MessagePin في Chat API، يمكن لتطبيقك تثبيت الرسائل وإلغاء تثبيتها والحصول على قائمة بجميع الرسائل المثبّتة في مساحات Google Chat، إذ تظهر الرسائل المثبّتة في واجهة Chat لجميع أعضاء المساحة، ويساعد هذا التطبيق في إدارة الرسائل المثبّتة نيابةً عن المستخدم.

للحصول على معلومات حول كيفية تثبيت المستخدمين للرسائل في المساحات، يُرجى الاطّلاع على تثبيت الرسائل والملفات والروابط في المساحات والرسائل.

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

Node.js

الاعتبارات

  • يمكنك فقط تثبيت الرسائل المتوفّرة من قبل في مساحة. لا يمكنك إنشاء رسالة جديدة وتثبيتها في الطلب نفسه.
  • لا يمكنك تثبيت الرسائل التي تظهر لك فقط، مثل الرسائل الخاصة التي تتلقّاها من أحد التطبيقات.
  • يمكن أن تحتوي كل مساحة Chat على 100 رسالة مثبّتة كحد أقصى. إذا حاول تطبيقك تثبيت الرسالة رقم 101، ستعرض واجهة برمجة التطبيقات رسالة خطأ.

تثبيت رسالة

لتثبيت رسالة تتضمّن مصادقة المستخدم، يجب تضمين ما يلي في طلبك:

  • حدِّد نطاق تفويض chat.spaces.pins أو chat.spaces.
  • اتّصِل بالرقم messagePins.create.
  • حدِّد parent (اسم المساحة) وقدِّم body مع message اسم مورد الرسالة الأصلية.

يعرض المثال التالي تثبيت رسالة في مساحة:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_pin_message.py.
  2. أدرِج الرمز التالي في chat_pin_message.py:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then pins a message in a space.
        '''
    
        # Authenticate with Google Workspace and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
    
        # Pin a message.
        result = service.spaces().messagePins().create(
            # The space to pin the message in.
            #
            # Replace SPACE with a space ID or name.
            # Obtain the space name from the spaces resource of Chat API,
            # or from a space's URL.
            parent='spaces/SPACE',
    
            # The message to pin.
            body={
                'message': 'spaces/SPACE/messages/MESSAGE'
            }
        ).execute()
    
        # Print Chat API's response in your command line interface.
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في الرمز، استبدِل ما يلي:

    • SPACE: رقم التعريف من name الخاص بالمساحة
    • MESSAGE: المعرّف من name الخاص بالرسالة
  4. في دليل العمل، أنشئ النموذج وشغِّله:

    python3 chat_pin_message.py

إزالة تثبيت رسالة

لإلغاء تثبيت رسالة تتطلّب مصادقة المستخدم، مرِّر ما يلي في طلبك:

  • حدِّد نطاق تفويض chat.spaces.pins أو chat.spaces.
  • اتّصِل بالرقم messagePins.delete.
  • اضبط name على اسم المورد الخاص بـ MessagePin المطلوب حذفه.

إليك كيفية إلغاء تثبيت رسالة:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_unpin_message.py.
  2. أدرِج الرمز التالي في chat_unpin_message.py:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then unpins a message from a space.
        '''
    
        # Authenticate with Google Workspace and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
    
        # Unpin a message.
        result = service.spaces().messagePins().delete(
            # The resource name of the message pin to delete.
            #
            # Replace SPACE with a space ID or name, and MESSAGE with the message ID.
            name='spaces/SPACE/messagePins/MESSAGE'
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في الرمز، استبدِل ما يلي:

    • SPACE: رقم التعريف من name الخاص بالمساحة
    • MESSAGE: المعرّف من name الخاص بالرسالة
  4. في دليل العمل، أنشئ النموذج وشغِّله:

    python3 chat_unpin_message.py

الحصول على قائمة بالرسائل المثبَّتة

للحصول على قائمة بالرسائل المثبّتة في المساحات التي يمكنك الوصول إليها، استخدِم مصادقة المستخدم، وقدِّم ما يلي في طلبك:

  • حدِّد أحد نطاقات القراءة: chat.spaces.pins.readonly أو chat.spaces.readonly أو chat.spaces.pins أو chat.spaces.
  • اتّصِل بالرقم messagePins.list.
  • حدِّد parent كاسم المساحة التي تريد استرداد الرسائل المثبّتة منها.

إليك كيفية عرض الرسائل المثبّتة:

Python

  1. في دليل العمل، أنشئ ملفًا باسم chat_list_pinned_messages.py.
  2. أدرِج الرمز التالي في chat_list_pinned_messages.py:

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    
    # Define your app's authorization scopes.
    SCOPES = ["https://www.googleapis.com/auth/chat.spaces.pins.readonly"]
    
    def main():
        '''
        Authenticates with Chat API via user credentials,
        then lists pinned messages in a space.
        '''
    
        # Authenticate with Google Workspace and get user authorization.
        flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
        creds = flow.run_local_server()
    
        # Build a service endpoint for Chat API.
        service = build('chat', 'v1', credentials=creds)
    
        # List pinned messages.
        result = service.spaces().messagePins().list(
            # The space to list pinned messages from.
            #
            # Replace SPACE with a space ID or name.
            parent='spaces/SPACE'
        ).execute()
    
        print(result)
    
    if __name__ == '__main__':
        main()
    
  3. في الرمز، استبدِل SPACE برقم تعريف المساحة من name.

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

    python3 chat_list_pinned_messages.py