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

এই ডকুমেন্টটিতে জিমেইল এপিআই-এর messages.list মেথডটি কীভাবে কল করতে হয় তা ব্যাখ্যা করা হয়েছে।

এই মেথডটি Gmail messages অবজেক্টের একটি অ্যারে রিটার্ন করে, যেটিতে মেসেজ id এবং threadId থাকে। মেসেজের সম্পূর্ণ বিবরণ পেতে messages.get মেথডটি ব্যবহার করুন।

পূর্বশর্ত

পাইথন

জিমেইল এপিআই সক্রিয় করা একটি গুগল ক্লাউড প্রজেক্ট। এর ধাপগুলো জানতে, জিমেইল এপিআই পাইথন কুইকস্টার্টটি সম্পূর্ণ করুন।

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

messages.list মেথডটি মেসেজ ফিল্টার করার জন্য একাধিক কোয়েরি প্যারামিটার সমর্থন করে:

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

কোডের নমুনা

পাইথন

নিম্নলিখিত কোড নমুনাটি প্রমাণীকৃত জিমেইল ব্যবহারকারীর বার্তাগুলি কীভাবে তালিকাভুক্ত করতে হয় তা দেখায়। কোডটি কোয়েরির সাথে মেলে এমন সমস্ত বার্তা পুনরুদ্ধার করার জন্য পেজিনেশন পরিচালনা করে।

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()

messages.list মেথডটি একটি রেসপন্স বডি রিটার্ন করে, যাতে নিম্নলিখিত বিষয়গুলো থাকে:

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

বার্তার সম্পূর্ণ বিষয়বস্তু ও মেটাডেটা পেতে, message.id ফিল্ড ব্যবহার করে messages.get মেথডটি কল করুন।