การจัดการชุดข้อความ
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Gmail API ใช้ทรัพยากร Thread
เพื่อจัดกลุ่มการตอบกลับอีเมลกับข้อความต้นฉบับไว้ในการสนทนาหรือ
ชุดข้อความเดียว ซึ่งจะช่วยให้คุณดึงข้อความทั้งหมดในการสนทนาตามลำดับได้
ทำให้มีบริบทสำหรับข้อความหรือปรับแต่งผลการค้นหาได้ง่ายขึ้น
เช่นเดียวกับข้อความ เธรดอาจมีป้ายกำกับ
ติดไว้ด้วย แต่คุณจะสร้างชุดข้อความไม่ได้
ทำได้เพียงลบเท่านั้น แต่คุณสามารถแทรกข้อความลงในชุดข้อความได้
เนื้อหา
กำลังดึงข้อมูลชุดข้อความ
ชุดข้อความช่วยให้คุณดึงข้อความในการสนทนาตามลำดับได้อย่างง่ายดาย
การแสดงชุดเธรดจะช่วยให้คุณเลือกจัดกลุ่มข้อความตามการสนทนา
และระบุบริบทเพิ่มเติมได้ คุณสามารถดึงรายการเธรดได้โดยใช้เมธอด
threads.list
หรือดึงเธรดที่เฉพาะเจาะจงด้วย
threads.get
นอกจากนี้ คุณยังกรองเธรดได้โดยใช้พารามิเตอร์การค้นหาเดียวกันกับMessage
หากข้อความใดก็ตามในชุดข้อความตรงกับคำค้นหา ระบบจะแสดงชุดข้อความนั้นในผลลัพธ์
ตัวอย่างโค้ดด้านล่างแสดงวิธีใช้ทั้ง 2 วิธีในตัวอย่างที่
แสดงเธรดที่มีการแชทมากที่สุดในกล่องจดหมาย threads.list
method
จะดึงข้อมูลรหัสชุดข้อความทั้งหมด จากนั้น threads.get
จะดึงข้อมูลข้อความทั้งหมดในแต่ละชุดข้อความ
สำหรับข้อความที่มีการตอบกลับ 3 รายการขึ้นไป เราจะดึงSubject
บรรทัดและแสดงข้อความที่ไม่ว่าง รวมถึงจำนวนข้อความในชุดข้อความ นอกจากนี้ คุณยังจะเห็น
ตัวอย่างโค้ดนี้ในวิดีโอ DevByte ที่เกี่ยวข้องด้วย
การเพิ่มฉบับร่างและข้อความลงในชุดข้อความ
หากคุณส่งหรือย้ายข้อมูลข้อความที่เป็นการตอบกลับอีเมลอื่น
หรือเป็นส่วนหนึ่งของการสนทนา แอปพลิเคชันของคุณควรเพิ่มข้อความนั้นลงในชุดข้อความที่
เกี่ยวข้อง ซึ่งจะช่วยให้ผู้ใช้ Gmail ที่เข้าร่วมการสนทนา
สามารถดูข้อความในบริบทได้ง่ายขึ้น
คุณเพิ่มฉบับร่างลงในเธรดได้ในขั้นตอนสร้าง อัปเดต หรือส่งข้อความฉบับร่าง
นอกจากนี้ คุณยังเพิ่มข้อความไปยังเธรดเป็นส่วนหนึ่งของ
การแทรกหรือ
การส่งข้อความได้ด้วย
ข้อความหรือฉบับร่างต้องมีคุณสมบัติตรงตามเกณฑ์ต่อไปนี้จึงจะเป็นส่วนหนึ่งของเธรดได้
- ต้องระบุ
threadId
ที่ขอใน Message
หรือ
Draft.Message
ที่คุณส่งมาพร้อมกับคำขอ
- ต้องตั้งค่าส่วนหัว
References
และ In-Reply-To
ตามมาตรฐาน RFC 2822
- ส่วนหัว
Subject
ต้องตรงกัน
ดูตัวอย่างการสร้างฉบับร่างหรือการส่งข้อความ ในทั้ง 2 กรณี คุณเพียงแค่
เพิ่มคีย์ threadId
ที่จับคู่กับรหัสเธรดลงในข้อมูลเมตาของข้อความ ซึ่งก็คือออบเจ็กต์ message
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-29 UTC
[null,null,["อัปเดตล่าสุด 2025-08-29 UTC"],[],[],null,["# Managing Threads\n\nThe Gmail API uses [`Thread` resources](/workspace/gmail/api/v1/reference/users/threads)\nto group email replies with their original message into a single conversation or\nthread. This allows you to retrieve all messages in a conversation, in order,\nmaking it easier to have context for a message or to refine search results.\n\nLike [messages](/workspace/gmail/api/v1/reference/users/messages), threads may also have\nlabels applied to them. However, unlike messages, threads cannot be created,\nonly deleted. Messages can, however, be inserted into a thread.\n\nContents\n--------\n\nRetrieving threads\n------------------\n\nThreads provide a simple way of retrieving messages in a conversation in order.\nBy listing a set of threads you can choose to group messages by conversation\nand provide additional context. You can retrieve a list of threads using the\n[`threads.list`](/workspace/gmail/api/v1/reference/users/threads/list) method, or retrieve\na specific thread with\n[`threads.get`](/workspace/gmail/api/v1/reference/users/threads/list). You can also\n[filter threads](/workspace/gmail/api/guides/filtering) using the same query parameters as\nfor the [`Message` resource](/workspace/gmail/api/v1/reference/users/messages). If any\nmessage in a thread matches the query, that thread is returned in the result. \n\nThe code sample below demonstrates how to use both methods in a sample that\ndisplays the most chatty threads in your inbox. The `threads.list` method\nfetches all thread IDs, then `threads.get` grabs all messages in each thread.\nFor those with 3 or more replies, we extract the `Subject` line and display the\nnon-empty ones plus the number of messages in the thread. You'll also find this\ncode sample featured in the corresponding DevByte video.\n\n### Python\n\ngmail/snippet/thread/threads.py \n[View on GitHub](https://github.com/googleworkspace/python-samples/blob/main/gmail/snippet/thread/threads.py) \n\n```python\nimport google.auth\nfrom googleapiclient.discovery import build\nfrom googleapiclient.errors import HttpError\n\n\ndef show_chatty_threads():\n \"\"\"Display threads with long conversations(\u003e= 3 messages)\n Return: None\n\n Load pre-authorized user credentials from the environment.\n TODO(developer) - See https://developers.google.com/identity\n for guides on implementing OAuth2 for the application.\n \"\"\"\n creds, _ = google.auth.default()\n\n try:\n # create gmail api client\n service = build(\"gmail\", \"v1\", credentials=creds)\n\n # pylint: disable=maybe-no-member\n # pylint: disable:R1710\n threads = (\n service.users().threads().list(userId=\"me\").execute().get(\"threads\", [])\n )\n for thread in threads:\n tdata = (\n service.users().threads().get(userId=\"me\", id=thread[\"id\"]).execute()\n )\n nmsgs = len(tdata[\"messages\"])\n\n # skip if \u003c3 msgs in thread\n if nmsgs \u003e 2:\n msg = tdata[\"messages\"][0][\"payload\"]\n subject = \"\"\n for header in msg[\"headers\"]:\n if header[\"name\"] == \"Subject\":\n subject = header[\"value\"]\n break\n if subject: # skip if no Subject line\n print(f\"- {subject}, {nmsgs}\")\n return threads\n\n except HttpError as error:\n print(f\"An error occurred: {error}\")\n\n\nif __name__ == \"__main__\":\n show_chatty_threads()\n```\n\nAdding drafts and messages to threads\n-------------------------------------\n\nIf you are sending or migrating messages that are a response to another email\nor part of a conversation, your application should add that message to the\nrelated thread. This makes it easier for Gmail users who are participating in\nthe conversation to keep the message in context.\n\nA draft can be added to a thread as part of\n[creating](/workspace/gmail/api/v1/reference/users/drafts/create),\n[updating](/workspace/gmail/api/v1/reference/users/drafts/update), or\n[sending](/workspace/gmail/api/v1/reference/users/drafts/send) a draft message.\nYou can also add a message to a thread as part of\n[inserting](/workspace/gmail/api/v1/reference/users/messages/insert) or\n[sending](/workspace/gmail/api/v1/reference/users/messages/send) a message.\n\nIn order to be part of a thread, a message or draft must meet the following\ncriteria:\n\n1. The requested `threadId` must be specified on the `Message` or `Draft.Message` you supply with your request.\n2. The `References` and `In-Reply-To` headers must be set in compliance with the [RFC 2822](https://tools.ietf.org/html/rfc2822#appendix-A.2) standard.\n3. The `Subject` headers must match.\n\nTake a look at the [creating a draft](/workspace/gmail/api/guides/drafts) or [sending a\nmessage](/workspace/gmail/api/guides/sending) examples. In both cases, you would simply\nadd a `threadId` key paired with a thread ID to a message's metadata, the\n`message` object."]]