Repository navigation
Expand file tree
/
Copy pathtasks.py
More file actions
561 lines (482 loc) · 23.4 KB
/
Copy pathtasks.py
File metadata and controls
561 lines (482 loc) · 23.4 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
"""
Feishu Task API v2 wrapper with auto-auth via Device Flow OAuth.
All Task API v2 endpoints require user_access_token (UAT).
Usage:
tasks = FeishuTasks(app_id, app_secret, lark_client)
result = tasks.list_tasks(chat_id, user_open_id)
result = tasks.list_tasklists(chat_id, user_open_id)
result = tasks.get_tasklist_tasks(chat_id, user_open_id, tasklist_guid)
"""
import datetime
import logging
import time
from feishu_bridge.api.client import FeishuAPI, FeishuAPIError
log = logging.getLogger("feishu-tasks")
DEFAULT_PAGE_SIZE = 50
class FeishuTasks(FeishuAPI):
"""Feishu Task API v2 with auto-auth."""
SCOPES = ["task:task:read", "task:task:write",
"task:tasklist:read", "task:tasklist:write",
"task:section:read", "task:section:write"]
BASE_PATH = "/open-apis/task/v2"
# -------------------------------------------------------------------
# Task endpoints
# -------------------------------------------------------------------
def list_tasks(self, chat_id: str, user_open_id: str,
completed: bool = None,
page_size: int = DEFAULT_PAGE_SIZE,
page_token: str = None) -> dict:
"""List tasks visible to the user."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"items": [], "has_more": False, "page_token": None,
"error": "auth_failed"}
params = {"page_size": page_size, "user_id_type": "open_id"}
if completed is not None:
params["completed"] = str(completed).lower()
if page_token:
params["page_token"] = page_token
return self.request("GET", "/tasks", token, params=params)
def get_task(self, chat_id: str, user_open_id: str,
task_guid: str) -> dict:
"""Get a single task by GUID."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
return self.request("GET", f"/tasks/{task_guid}", token,
params={"user_id_type": "open_id"})
def list_all_tasks_result(self, chat_id: str, user_open_id: str,
completed: bool = None,
max_pages: int = 10) -> dict:
"""List all tasks with pagination metadata and explicit error state."""
all_items = []
page_token = None
for _ in range(max_pages):
data = self.list_tasks(chat_id, user_open_id,
completed=completed,
page_token=page_token)
if "error" in data:
return {
"items": all_items,
"error": data["error"],
"truncated": False,
}
all_items.extend(data.get("items", []))
if not data.get("has_more"):
return {"items": all_items, "error": None, "truncated": False}
page_token = data.get("page_token")
if not page_token:
break
return {
"items": all_items,
"error": None,
"truncated": bool(page_token),
}
def list_all_tasks(self, chat_id: str, user_open_id: str,
completed: bool = None,
max_pages: int = 10) -> list[dict]:
"""Backward-compatible list-only wrapper."""
result = self.list_all_tasks_result(
chat_id, user_open_id, completed=completed, max_pages=max_pages)
return result["items"]
def find_task_by_id(self, chat_id: str, user_open_id: str,
task_id: str, completed: bool = None,
max_pages: int = 50) -> dict:
"""Find a task by task_id or guid with explicit truncation/error state."""
page_token = None
for _ in range(max_pages):
data = self.list_tasks(chat_id, user_open_id,
completed=completed,
page_token=page_token)
if "error" in data:
return {"task": None, "error": data["error"], "truncated": False}
for item in data.get("items", []):
if item.get("task_id") == task_id or item.get("guid") == task_id:
return {"task": item, "error": None, "truncated": False}
if not data.get("has_more"):
return {"task": None, "error": None, "truncated": False}
page_token = data.get("page_token")
if not page_token:
break
return {"task": None, "error": None, "truncated": bool(page_token)}
# -------------------------------------------------------------------
# TaskList endpoints
# -------------------------------------------------------------------
def list_tasklists(self, chat_id: str, user_open_id: str,
page_size: int = DEFAULT_PAGE_SIZE,
page_token: str = None) -> dict:
"""List task lists visible to the user."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"items": [], "has_more": False, "page_token": None,
"error": "auth_failed"}
params = {"page_size": page_size, "user_id_type": "open_id"}
if page_token:
params["page_token"] = page_token
return self.request("GET", "/tasklists", token, params=params)
def get_tasklist(self, chat_id: str, user_open_id: str,
tasklist_guid: str) -> dict:
"""Get a single task list by GUID."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
return self.request("GET", f"/tasklists/{tasklist_guid}", token,
params={"user_id_type": "open_id"})
def get_tasklist_tasks(self, chat_id: str, user_open_id: str,
tasklist_guid: str,
completed: bool = None,
page_size: int = DEFAULT_PAGE_SIZE,
page_token: str = None) -> dict:
"""List tasks within a specific task list."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"items": [], "has_more": False, "page_token": None,
"error": "auth_failed"}
params = {"page_size": page_size, "user_id_type": "open_id"}
if completed is not None:
params["completed"] = str(completed).lower()
if page_token:
params["page_token"] = page_token
return self.request("GET", f"/tasklists/{tasklist_guid}/tasks",
token, params=params)
def complete_task(self, chat_id: str, user_open_id: str,
task_guid: str) -> dict:
"""Mark a task as completed (idempotent — already-completed tasks succeed)."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
try:
return self.request("PATCH", f"/tasks/{task_guid}", token,
json_body={
"task": {"completed_at": str(int(time.time() * 1000))},
"update_fields": ["completed_at"],
})
except FeishuAPIError as e:
if e.code == 1470400:
log.info("Task %s already completed", task_guid)
return self.request("GET", f"/tasks/{task_guid}", token,
params={"user_id_type": "open_id"})
raise
# -------------------------------------------------------------------
# Subtask endpoints
# -------------------------------------------------------------------
def list_subtasks(self, chat_id: str, user_open_id: str,
task_guid: str,
page_size: int = DEFAULT_PAGE_SIZE,
page_token: str = None) -> dict:
"""List subtasks of a given task."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"items": [], "has_more": False, "page_token": None,
"error": "auth_failed"}
params = {"page_size": page_size, "user_id_type": "open_id"}
if page_token:
params["page_token"] = page_token
return self.request("GET", f"/tasks/{task_guid}/subtasks",
token, params=params)
def create_subtask(self, chat_id: str, user_open_id: str,
parent_guid: str, summary: str,
description: str = None,
due_timestamp: str = None) -> dict:
"""Create a subtask under the given parent task.
Args:
parent_guid: GUID of the parent task.
summary: Title of the new subtask.
description: Optional description text.
due_timestamp: Optional Unix timestamp in **milliseconds** as string.
Returns:
API response data (contains the created subtask object).
"""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
body = {"summary": summary}
if description:
body["description"] = description
if due_timestamp:
body["due"] = {"timestamp": due_timestamp}
return self.request("POST", f"/tasks/{parent_guid}/subtasks",
token, json_body=body)
def create_task(self, chat_id: str, user_open_id: str,
summary: str, description: str = None,
due_timestamp: str = None,
tasklist_guid: str = None,
section_guid: str = None) -> dict:
"""Create a new task.
Args:
summary: task title (max 3000 chars)
description: optional description
due_timestamp: optional Unix timestamp in **milliseconds** as string
tasklist_guid: optional tasklist to add the task to
section_guid: optional section within the tasklist
Returns:
API response (contains the created task object).
"""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
body = {"summary": summary}
if description:
body["description"] = description
if due_timestamp:
body["due"] = {"timestamp": due_timestamp}
if tasklist_guid:
tl_entry = {"tasklist_guid": tasklist_guid}
if section_guid:
tl_entry["section_guid"] = section_guid
body["tasklists"] = [tl_entry]
return self.request("POST", "/tasks", token,
params={"user_id_type": "open_id"},
json_body=body)
def update_task(self, chat_id: str, user_open_id: str,
task_guid: str, *,
summary: str = None,
description: str = None,
due_timestamp: str = None,
completed_at: str = None) -> dict:
"""Update a task via PATCH.
Args:
task_guid: GUID of the task to update.
summary: New title (optional).
description: New description (optional).
due_timestamp: New due date as Unix ms timestamp string (optional).
completed_at: Completion timestamp in ms, or "0" to uncomplete (optional).
Returns:
API response (contains the updated task object).
"""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
task_data = {}
if summary is not None:
task_data["summary"] = summary
if description is not None:
task_data["description"] = description
if due_timestamp is not None:
task_data["due"] = {"timestamp": due_timestamp}
if completed_at is not None:
task_data["completed_at"] = completed_at
if not task_data:
return {"error": "no fields to update"}
# Split completed_at from other fields to avoid silent-drop on 1470400:
# If completed_at is mixed with other fields and the task is already
# completed, the API rejects the whole PATCH — losing the other updates.
other_data = {k: v for k, v in task_data.items() if k != "completed_at"}
# Step 1: apply non-completion fields first (if any)
result = None
if other_data:
result = self.request("PATCH", f"/tasks/{task_guid}", token,
params={"user_id_type": "open_id"},
json_body={
"task": other_data,
"update_fields": list(other_data.keys()),
})
# Step 2: apply completed_at separately (idempotent)
if completed_at is not None:
try:
result = self.request(
"PATCH", f"/tasks/{task_guid}", token,
params={"user_id_type": "open_id"},
json_body={
"task": {"completed_at": completed_at},
"update_fields": ["completed_at"],
})
except FeishuAPIError as e:
# 1470400: task already completed — treat as success
if e.code == 1470400 and completed_at != "0":
log.info("Task %s already completed, treating as success",
task_guid)
if not result:
result = self.request(
"GET", f"/tasks/{task_guid}", token,
params={"user_id_type": "open_id"})
else:
raise
return result
# -------------------------------------------------------------------
# TaskList CRUD endpoints
# -------------------------------------------------------------------
def create_tasklist(self, chat_id: str, user_open_id: str,
name: str) -> dict:
"""Create a new task list."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
return self.request("POST", "/tasklists", token,
params={"user_id_type": "open_id"},
json_body={"name": name})
def update_tasklist(self, chat_id: str, user_open_id: str,
tasklist_guid: str, name: str) -> dict:
"""Rename a task list."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
return self.request("PATCH", f"/tasklists/{tasklist_guid}", token,
params={"user_id_type": "open_id"},
json_body={
"tasklist": {"name": name},
"update_fields": ["name"],
})
def delete_tasklist(self, chat_id: str, user_open_id: str,
tasklist_guid: str) -> dict:
"""Delete a task list."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
return self.request("DELETE", f"/tasklists/{tasklist_guid}", token,
params={"user_id_type": "open_id"})
def add_task_to_tasklist(self, chat_id: str, user_open_id: str,
task_guid: str, tasklist_guid: str) -> dict:
"""Add an existing task to a task list."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
return self.request("POST", f"/tasks/{task_guid}/add_tasklist", token,
params={"user_id_type": "open_id"},
json_body={"tasklist_guid": tasklist_guid})
def remove_task_from_tasklist(self, chat_id: str, user_open_id: str,
task_guid: str, tasklist_guid: str) -> dict:
"""Remove a task from a task list."""
token = self.get_token(chat_id, user_open_id)
if not token:
return {"error": "auth_failed"}
return self.request("POST", f"/tasks/{task_guid}/remove_tasklist", token,
params={"user_id_type": "open_id"},
json_body={"tasklist_guid": tasklist_guid})
# -------------------------------------------------------------------
# Dispatch (full read+write)
# -------------------------------------------------------------------
_READ_ACTIONS = {"list_tasks", "get_task", "list_subtasks",
"list_tasklists", "summary"}
_WRITE_ACTIONS = {"create_task", "update_task", "complete_task",
"create_subtask", "create_tasklist",
"update_tasklist", "delete_tasklist",
"add_task_to_tasklist", "remove_task_from_tasklist"}
_ALL_ACTIONS = _READ_ACTIONS | _WRITE_ACTIONS
def dispatch(self, action: str, chat_id: str, sender_id: str,
**kwargs) -> dict:
"""统一入口,归一化返回 {ok, data/error}."""
try:
if action not in self._ALL_ACTIONS:
return {"ok": False, "error": "unsupported_action",
"message": f"支持的操作: "
f"{', '.join(sorted(self._ALL_ACTIONS))}"}
# Read
if action == "list_tasks":
result = self.list_tasks(chat_id, sender_id, **kwargs)
elif action == "get_task":
result = self.get_task(
chat_id, sender_id, kwargs.get("task_guid", ""))
elif action == "list_subtasks":
result = self.list_subtasks(
chat_id, sender_id, kwargs.get("task_guid", ""))
elif action == "list_tasklists":
result = self.list_tasklists(chat_id, sender_id)
elif action == "summary":
result = self.summary(chat_id, sender_id)
# Write
elif action == "create_task":
result = self.create_task(
chat_id, sender_id,
summary=kwargs.get("summary", ""),
description=kwargs.get("description"),
due_timestamp=kwargs.get("due_timestamp"),
tasklist_guid=kwargs.get("tasklist_guid"),
section_guid=kwargs.get("section_guid"))
elif action == "update_task":
result = self.update_task(
chat_id, sender_id,
task_guid=kwargs.get("task_guid", ""),
summary=kwargs.get("summary"),
description=kwargs.get("description"),
due_timestamp=kwargs.get("due_timestamp"),
completed_at=kwargs.get("completed_at"))
elif action == "complete_task":
result = self.complete_task(
chat_id, sender_id,
kwargs.get("task_guid", ""))
elif action == "create_subtask":
result = self.create_subtask(
chat_id, sender_id,
parent_guid=kwargs.get("parent_guid", ""),
summary=kwargs.get("summary", ""),
description=kwargs.get("description"),
due_timestamp=kwargs.get("due_timestamp"))
elif action == "create_tasklist":
result = self.create_tasklist(
chat_id, sender_id, kwargs.get("name", ""))
elif action == "update_tasklist":
result = self.update_tasklist(
chat_id, sender_id,
tasklist_guid=kwargs.get("tasklist_guid", ""),
name=kwargs.get("name", ""))
elif action == "delete_tasklist":
result = self.delete_tasklist(
chat_id, sender_id,
kwargs.get("tasklist_guid", ""))
elif action == "add_task_to_tasklist":
result = self.add_task_to_tasklist(
chat_id, sender_id,
task_guid=kwargs.get("task_guid", ""),
tasklist_guid=kwargs.get("tasklist_guid", ""))
elif action == "remove_task_from_tasklist":
result = self.remove_task_from_tasklist(
chat_id, sender_id,
task_guid=kwargs.get("task_guid", ""),
tasklist_guid=kwargs.get("tasklist_guid", ""))
else:
return {"ok": False, "error": "unsupported_action",
"message": f"未知 action: {action}"}
if isinstance(result, dict) and "error" in result:
return {"ok": False, "error": result["error"],
"data": result}
if result is None:
return {"ok": False, "error": "auth_failed"}
return {"ok": True, "data": result}
except Exception as e:
log.exception("Tasks dispatch error: action=%s", action)
return {"ok": False, "error": "internal_error",
"message": str(e)}
# -------------------------------------------------------------------
# Convenience
# -------------------------------------------------------------------
def summary(self, chat_id: str, user_open_id: str) -> str:
"""Human-readable summary of all tasks and lists."""
lines = []
tasklists = self.list_tasklists(chat_id, user_open_id)
if "error" in tasklists:
return self._auth_failed_message()
tl_items = tasklists.get("items", [])
if tl_items:
lines.append(f"📋 **任务清单** ({len(tl_items)} 个)")
for tl in tl_items:
name = tl.get("name", "未命名清单")
guid = tl.get("guid", "")
lines.append(f" • {name} (`{guid[:8]}…`)")
else:
lines.append("📋 没有找到任务清单")
lines.append("")
tasks_result = self.list_all_tasks_result(
chat_id, user_open_id, completed=False)
if tasks_result["error"]:
return self._auth_failed_message()
tasks = tasks_result["items"]
if tasks:
lines.append(f"📌 **待办任务** ({len(tasks)} 个)")
for t in tasks[:20]:
summary_text = t.get("summary", "无标题")
due = t.get("due")
due_str = ""
if due and due.get("timestamp"):
ts_raw = int(due["timestamp"])
# Feishu Task API stores timestamps in milliseconds
ts = ts_raw // 1000 if ts_raw > 9_999_999_999 else ts_raw
due_str = f" — 截止 {datetime.datetime.fromtimestamp(ts).strftime('%m/%d')}"
lines.append(f" • {summary_text}{due_str}")
if len(tasks) > 20:
lines.append(f" …还有 {len(tasks) - 20} 个任务")
if tasks_result["truncated"]:
lines.append(" …任务结果过多,仅显示搜索上限内的部分任务")
else:
lines.append("📌 没有待办任务")
return "\n".join(lines)