জিমেইল বার্তা তালিকা, জিমেইল বার্তা তালিকা

এই পৃষ্ঠাটি ব্যাখ্যা করে কিভাবে Gmail API-এর users.messages.list পদ্ধতিতে কল করতে হয়।

পদ্ধতিটি Gmail Message সংস্থানগুলির একটি অ্যারে প্রদান করে যাতে বার্তা id এবং threadId থাকে। বার্তার সম্পূর্ণ বিবরণ পুনরুদ্ধার করতে, users.messages.get পদ্ধতি ব্যবহার করুন।

পূর্বশর্ত

পাইথন

Gmail API সক্ষম সহ একটি Google ক্লাউড প্রকল্প। পদক্ষেপের জন্য, Gmail API Python দ্রুতস্টার্ট সম্পূর্ণ করুন।

তালিকা বার্তা

users.messages.list পদ্ধতিটি বার্তাগুলি ফিল্টার করার জন্য বেশ কয়েকটি ক্যোয়ারী প্যারামিটার সমর্থন করে:

  • maxResults : ফেরত দেওয়ার জন্য সর্বাধিক সংখ্যক বার্তা (ডিফল্ট 100, সর্বোচ্চ 500)।
  • pageToken : ফলাফলের একটি নির্দিষ্ট পৃষ্ঠা পুনরুদ্ধার করতে টোকেন।
  • q : বার্তাগুলি ফিল্টার করার জন্য ক্যোয়ারী স্ট্রিং, যেমন from:someuser@example.com is:unread"
  • labelIds : শুধুমাত্র লেবেল সহ বার্তা ফেরত দেয় যা সমস্ত নির্দিষ্ট লেবেল আইডির সাথে মেলে।
  • includeSpamTrash : ফলাফলগুলিতে SPAM এবং TRASH থেকে বার্তাগুলি অন্তর্ভুক্ত করুন৷

কোড নমুনা

পাইথন

নিম্নলিখিত কোড নমুনা প্রমাণিত Gmail ব্যবহারকারীর জন্য বার্তা তালিকা কিভাবে দেখায়. কোয়েরির সাথে মিলে যাওয়া সমস্ত বার্তা পুনরুদ্ধার করতে কোডটি পৃষ্ঠা সংখ্যা পরিচালনা করে।

gmail/snippet/list_messages.py
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]


def main():
    """Shows basic usage of the Gmail API.
    Lists the user's Gmail messages.
    """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists("token.json"):
        creds = Credentials.from_authorized_user_file("token.json", SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open("token.json", "w") as token:
            token.write(creds.to_json())

    try:
        # Call the Gmail API
        service = build("gmail", "v1", credentials=creds)
        results = (
            service.users().messages().list(userId="me", labelIds=["INBOX"]).execute()
        )
        messages = results.get("messages", [])

        if not messages:
            print("No messages found.")
            return

        print("Messages:")
        for message in messages:
            print(f'Message ID: {message["id"]}')
            msg = (
                service.users().messages().get(userId="me", id=message["id"]).execute()
            )
            print(f'  Subject: {msg["snippet"]}')

    except HttpError as error:
        # TODO(developer) - Handle errors from gmail API.
        print(f"An error occurred: {error}")


if __name__ == "__main__":
    main()

users.messages.list পদ্ধতি একটি প্রতিক্রিয়া বডি প্রদান করে যাতে নিম্নলিখিতগুলি রয়েছে:

  • messages[] : Message সংস্থানগুলির একটি অ্যারে।
  • nextPageToken : ফলাফলের একাধিক পৃষ্ঠার অনুরোধের জন্য, একটি টোকেন যা পরবর্তী কলগুলির সাথে আরও বার্তা তালিকাভুক্ত করতে ব্যবহার করা যেতে পারে।
  • resultSizeEstimate : ফলাফলের আনুমানিক মোট সংখ্যা।

সম্পূর্ণ বার্তা সামগ্রী এবং মেটাডেটা আনতে, users.messages.get পদ্ধতিতে কল করতে message.id ক্ষেত্রটি ব্যবহার করুন।