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

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

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

পূর্বশর্ত

পাইথন

Gmail API সক্ষম থাকা একটি Google ক্লাউড প্রকল্প। ধাপগুলির জন্য, Gmail API Python quickstart সম্পূর্ণ করুন।

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

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

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

কোড নমুনা

পাইথন

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

জিমেইল/স্নিপেট/লিস্ট_মেসেজ.পি
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 : ফলাফলের আনুমানিক মোট সংখ্যা।

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