Repository navigation
Expand file tree
/
Copy pathmail.py
More file actions
416 lines (364 loc) · 15.6 KB
/
Copy pathmail.py
File metadata and controls
416 lines (364 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""
Feishu Mail API wrapper — read, compose, reply, forward, manage drafts.
API format verified against lark CLI dry-run output.
All operations require user_access_token (UAT).
API surface:
list → GET /user_mailboxes/me/messages?folder_id=INBOX&page_size=N
batch_get → POST /user_mailboxes/me/messages/batch_get {message_ids:[...]}
get → GET /user_mailboxes/me/messages/{id}
thread → GET /user_mailboxes/me/threads/{id}
drafts → GET /user_mailboxes/me/drafts?page_size=N
draft → GET /user_mailboxes/me/drafts/{id}
create → POST /user_mailboxes/me/drafts {raw:"base64url-EML"}
send → POST /user_mailboxes/me/drafts/{id}/send
delete → DELETE /user_mailboxes/me/drafts/{id}
modify → PUT /user_mailboxes/me/messages/{id} {read:true}
profile → GET /user_mailboxes/me/profile
"""
import base64
import logging
import time
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from typing import Optional
from feishu_bridge.api.client import FeishuAPI
log = logging.getLogger("feishu-mail")
class FeishuMail(FeishuAPI):
"""Feishu Mail API v1 wrapper — full read/write."""
SCOPES = [
"mail:user_mailbox.message:readonly",
"mail:user_mailbox.message:send",
"mail:user_mailbox.message:modify",
"mail:user_mailbox.message.body:read",
"mail:user_mailbox.message.subject:read",
"mail:user_mailbox.message.address:read",
"mail:user_mailbox.folder:read",
]
BASE_PATH = "/open-apis/mail/v1"
DEFAULT_MAILBOX = "me"
DEFAULT_PAGE_SIZE = 20
# ── dispatch ──────────────────────────────────────────────────────
_ACTIONS = {
# Read
"triage", "get_message", "get_thread",
"list_drafts", "get_draft", "profile",
# Write
"compose", "reply", "send_draft", "delete_draft",
"mark_read",
}
def dispatch(self, action: str, chat_id: str, sender_id: str,
**kwargs) -> dict:
"""统一入口,归一化 {ok, data/error}."""
try:
if action not in self._ACTIONS:
return {"ok": False, "error": "unsupported_action",
"message": f"支持: {', '.join(sorted(self._ACTIONS))}"}
action_map = {
"triage": self.triage,
"get_message": self.get_message,
"get_thread": self.get_thread,
"list_drafts": self.list_drafts,
"get_draft": self.get_draft,
"profile": self.profile,
"compose": self.compose,
"reply": self.reply,
"send_draft": self.send_draft,
"delete_draft": self.delete_draft,
"mark_read": self.mark_read,
}
method = action_map[action]
result = method(chat_id, sender_id, **kwargs)
if result is None:
return {"ok": False, "error": "auth_failed"}
if isinstance(result, dict) and "error" in result:
return {"ok": False, "error": result["error"],
"data": result}
return {"ok": True, "data": result}
except Exception as e:
log.exception("Mail dispatch error: action=%s", action)
return {"ok": False, "error": "internal_error",
"message": str(e)}
# ── Read operations ───────────────────────────────────────────────
def triage(self, chat_id: str, user_open_id: str,
query: str = None, folder_id: str = "INBOX",
page_size: int = DEFAULT_PAGE_SIZE,
page_token: str = None) -> Optional[dict]:
"""List message summaries (list IDs → batch_get details)."""
token = self.get_token(chat_id, user_open_id)
if not token:
return None
params = {"page_size": min(page_size, 50), "folder_id": folder_id}
if query:
params["query"] = query
if page_token:
params["page_token"] = page_token
list_data = self.request(
"GET", "/user_mailboxes/me/messages", token, params=params,
)
msg_ids = list_data.get("items", [])
if not msg_ids:
return {"items": [], "has_more": False, "page_token": ""}
batch_data = self.request(
"POST", "/user_mailboxes/me/messages/batch_get", token,
json_body={"message_ids": msg_ids},
)
return {
"items": [self._summarize(m) for m in
batch_data.get("items", [])],
"has_more": list_data.get("has_more", False),
"page_token": list_data.get("page_token", ""),
}
def get_message(self, chat_id: str, user_open_id: str,
message_id: str) -> Optional[dict]:
"""Get single message with full body."""
token = self.get_token(chat_id, user_open_id)
if not token:
return None
data = self.request(
"GET", "/user_mailboxes/me/messages/{message_id}", token,
)
items = data.get("items", [data])
msg = items[0] if items else data
return self._summarize(msg, include_body=True)
def get_thread(self, chat_id: str, user_open_id: str,
thread_id: str) -> Optional[dict]:
"""Get all messages in a thread."""
token = self.get_token(chat_id, user_open_id)
if not token:
return None
data = self.request(
"GET", "/user_mailboxes/me/threads/{thread_id}", token,
)
return {
"thread_id": thread_id,
"messages": [self._summarize(m, include_body=True)
for m in data.get("items", [])],
}
def list_drafts(self, chat_id: str, user_open_id: str,
page_size: int = 20) -> Optional[dict]:
"""List draft messages."""
token = self.get_token(chat_id, user_open_id)
if not token:
return None
data = self.request(
"GET", "/user_mailboxes/me/drafts", token,
params={"page_size": min(page_size, 50)},
)
return {
"items": [{"draft_id": d.get("draft_id", ""),
"subject": d.get("subject", ""),
"to": self._extract_address_list(d.get("to", []))}
for d in data.get("items", [])],
"has_more": data.get("has_more", False),
}
def get_draft(self, chat_id: str, user_open_id: str,
draft_id: str) -> Optional[dict]:
"""Get single draft."""
token = self.get_token(chat_id, user_open_id)
if not token:
return None
data = self.request(
"GET", "/user_mailboxes/me/drafts/{draft_id}", token,
)
items = data.get("items", [data])
d = items[0] if items else data
return {
"draft_id": d.get("draft_id", draft_id),
"subject": d.get("subject", ""),
"to": self._extract_address_list(d.get("to", [])),
"cc": self._extract_address_list(d.get("cc", [])),
"body_text": d.get("body_plain_text", ""),
}
def profile(self, chat_id: str, user_open_id: str) -> Optional[dict]:
"""Get current user's email profile."""
token = self.get_token(chat_id, user_open_id)
if not token:
return None
data = self.request(
"GET", "/user_mailboxes/me/profile", token,
)
return {"email": data.get("primary_email_address", ""),
"name": data.get("user_name", "")}
# ── Write operations ──────────────────────────────────────────────
def compose(self, chat_id: str, user_open_id: str, *,
to: str, subject: str, body: str,
cc: str = None, html: bool = True,
send: bool = False) -> Optional[dict]:
"""Compose and save as draft (optionally send).
Args:
to: comma-separated recipient addresses
subject: email subject
body: email body (HTML if html=True)
cc: comma-separated CC addresses
html: True for HTML body, False for plain text
send: if True, send immediately after creating draft
"""
token = self._get_write_token(chat_id, user_open_id)
if not token:
return None
try:
eml = self._build_eml(to, subject, body, cc=cc, html=html)
except Exception as e:
return {"error": "eml_build_failed", "message": str(e)}
data = self.request(
"POST", "/user_mailboxes/me/drafts", token,
json_body={"raw": self._b64url(eml)},
)
items = data.get("items", [data])
draft = items[0] if items else data
draft_id = draft.get("draft_id", "")
result = {"draft_id": draft_id, "sent": False}
if send and draft_id:
self.request(
"POST",
"/user_mailboxes/me/drafts/{draft_id}/send", token,
)
result["sent"] = True
return result
def reply(self, chat_id: str, user_open_id: str, *,
message_id: str, body: str,
html: bool = True, reply_all: bool = False,
send: bool = False) -> Optional[dict]:
"""Reply to a message, save as draft (optionally send)."""
token = self._get_write_token(chat_id, user_open_id)
if not token:
return None
# Fetch original message for subject/to
try:
data = self.request(
"GET", "/user_mailboxes/me/messages/{message_id}", token,
)
items = data.get("items", [data])
orig = items[0] if items else data
except Exception:
return {"error": "fetch_failed", "message": "无法获取原始邮件"}
subject = orig.get("subject", "")
if not subject.lower().startswith("re:"):
subject = f"Re: {subject}"
# Build reply body with quoting (plain text)
orig_body = orig.get("body_plain_text", "")
orig_date = orig.get("internal_date", "")
orig_from = self._extract_from(orig)
orig_name = orig_from.get("name", orig_from.get("address", ""))
quoted = "\n".join(f"> {line}" for line in orig_body.split("\n"))
reply_body = f"{body}\n\nOn {orig_date}, {orig_name} wrote:\n{quoted}"
# Collect recipients
to_addr = self._extract_address_list(orig.get("reply_to", []))
if not to_addr:
to_addr = self._extract_address_list(orig.get("from", []))
to_str = ", ".join(a["address"] for a in to_addr if a.get("address"))
cc_str = ""
if reply_all:
cc_addrs = self._extract_address_list(orig.get("cc", []))
cc_str = ", ".join(a["address"] for a in cc_addrs
if a.get("address"))
try:
eml = self._build_eml(to_str, subject, reply_body,
cc=cc_str or None, html=html)
except Exception as e:
return {"error": "eml_build_failed", "message": str(e)}
data = self.request(
"POST", "/user_mailboxes/me/drafts", token,
json_body={"raw": self._b64url(eml)},
)
items = data.get("items", [data])
draft = items[0] if items else data
draft_id = draft.get("draft_id", "")
result = {"draft_id": draft_id, "sent": False}
if send and draft_id:
self.request(
"POST",
"/user_mailboxes/me/drafts/{draft_id}/send", token,
)
result["sent"] = True
return result
def send_draft(self, chat_id: str, user_open_id: str,
draft_id: str) -> Optional[dict]:
"""Send an existing draft."""
token = self._get_write_token(chat_id, user_open_id)
if not token:
return None
self.request(
"POST",
"/user_mailboxes/me/drafts/{draft_id}/send", token,
)
return {"draft_id": draft_id, "sent": True}
def delete_draft(self, chat_id: str, user_open_id: str,
draft_id: str) -> Optional[dict]:
"""Delete a draft."""
token = self._get_write_token(chat_id, user_open_id)
if not token:
return None
self.request(
"DELETE", "/user_mailboxes/me/drafts/{draft_id}", token,
)
return {"draft_id": draft_id, "deleted": True}
def mark_read(self, chat_id: str, user_open_id: str,
message_id: str, read: bool = True) -> Optional[dict]:
"""Mark a message as read or unread."""
token = self._get_write_token(chat_id, user_open_id)
if not token:
return None
self.request(
"PUT", "/user_mailboxes/me/messages/{message_id}", token,
json_body={"read": read},
)
return {"message_id": message_id, "read": read}
# ── Helpers ───────────────────────────────────────────────────────
@staticmethod
def _summarize(msg: dict, include_body: bool = False) -> dict:
result = {
"message_id": msg.get("message_id", ""),
"thread_id": msg.get("thread_id", ""),
"subject": msg.get("subject", ""),
"date": msg.get("internal_date", ""),
"from": FeishuMail._extract_from(msg),
"to": FeishuMail._extract_address_list(msg.get("to", [])),
"cc": FeishuMail._extract_address_list(msg.get("cc", [])),
"has_attachments": bool(msg.get("has_attachment")),
"is_read": msg.get("read", False),
}
if include_body:
result["body_text"] = msg.get("body_plain_text", "")
return result
@staticmethod
def _extract_from(msg: dict) -> dict:
frm = msg.get("from", [])
if not frm:
return {}
addr = frm[0] if isinstance(frm, list) else frm
if not isinstance(addr, dict):
return {}
return {"name": addr.get("name", ""),
"address": addr.get("mail_address",
addr.get("address", ""))}
@staticmethod
def _extract_address_list(lst: list) -> list:
if not lst or not isinstance(lst, list):
return []
result = []
for a in lst:
if isinstance(a, dict):
result.append({
"name": a.get("name", ""),
"address": a.get("mail_address", a.get("address", "")),
})
return result
@staticmethod
def _build_eml(to: str, subject: str, body: str, *,
cc: str = None, html: bool = True) -> str:
"""Build a MIME email message (RFC 2822)."""
msg = MIMEMultipart()
msg["To"] = to
msg["Subject"] = subject
msg["Date"] = formatdate(time.time(), localtime=True)
if cc:
msg["Cc"] = cc
subtype = "html" if html else "plain"
msg.attach(MIMEText(body, subtype, "utf-8"))
return msg.as_string()
@staticmethod
def _b64url(data: str) -> str:
"""Base64url-encode a string (no padding)."""
return base64.urlsafe_b64encode(data.encode()).rstrip(b"=").decode()