इस गाइड में बताया गया है कि किसी स्पेस में मैसेज की क्रम में लगाई गई और फ़िल्टर की जा सकने वाली सूची देखने के लिए, Google Chat API के Message
संसाधन पर list
तरीके का इस्तेमाल कैसे करें.
Message
रिसॉर्स, Google Chat में टेक्स्ट या कार्ड मैसेज दिखाता है. आप इनसे जुड़े तरीकों का इस्तेमाल करके, Google Chat API में create
, get
, update
या delete
को मैसेज भेज सकते हैं. मैसेज और कार्ड मैसेज के बारे में ज़्यादा जानने के लिए, Google Chat मैसेज की खास जानकारी देखें.
ज़रूरी शर्तें
Python
- Python 3.6 या इससे नया वर्शन
- pip पैकेज मैनेजमेंट टूल
Python के लिए नई Google क्लाइंट लाइब्रेरी. उन्हें इंस्टॉल या अपडेट करने के लिए, अपने कमांड-लाइन इंटरफ़ेस में नीचे दिया गया कमांड चलाएं:
pip3 install --upgrade google-api-python-client google-auth-oauthlib
- ऐसा Google Cloud प्रोजेक्ट जिसमें Google Chat API चालू हो और उसे कॉन्फ़िगर किया गया हो. तरीका जानने के लिए, Google Chat ऐप्लिकेशन बनाना देखें.
Chat ऐप्लिकेशन के लिए अनुमति कॉन्फ़िगर की गई. लिस्टिंग मैसेज के लिए,
chat.messages.readonly
याchat.messages
के अनुमति वाले दायरे के साथ उपयोगकर्ता की पुष्टि करना ज़रूरी है.
मैसेज की सूची बनाएं
उपयोगकर्ता की पुष्टि करने की सुविधा से मैसेज की सूची बनाने के लिए, अपने अनुरोध में इसे पास करें:
- अनुमति देने के लिए,
chat.messages.readonly
याchat.messages
का स्कोप बताएं. Message
संसाधन परlist
वाला तरीका कॉल करें.
यहां दिए गए उदाहरण में, 16 मार्च, 2023 के बाद भेजे गए चैट स्पेस के मैसेज की सूची दी गई है:
Python
- अपनी वर्किंग डायरेक्ट्री में,
chat_messages_list.py
नाम की एक फ़ाइल बनाएं. chat_messages_list.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.readonly"] def main(): ''' Authenticates with Chat API via user credentials, then lists messages in a space sent after March 16, 2023. ''' # 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().list( # The space for which to list messages. parent = 'spaces/SPACE', # An optional filter that returns messages # created after March 16, 2023. filter = 'createTime > "2023-03-16T00:00:00-00:00"' ).execute() # Prints the list of messages. print(result) if __name__ == '__main__': main()
कोड में,
SPACE
की जगह स्पेस का नाम डालें. इसे स्पेस के यूआरएल या Chat API में,spaces.list
तरीके से लिया जा सकता है.अपनी वर्किंग डायरेक्ट्री में, यह सैंपल बनाएं और चलाएं:
python3 chat_messages_list.py
Google Chat API, 16 मार्च, 2023 के बाद तय किए गए स्पेस में भेजे गए मैसेज की सूची दिखाता है.
मिलते-जुलते विषय
- मैसेज फ़ॉर्मैट करना.
- मैसेज मिटाना.
- किसी मैसेज के बारे में जानकारी पाना.
- मैसेज अपडेट करें.
- मैसेज भेजें.