forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclickup_client.py
More file actions
370 lines (313 loc) · 13.8 KB
/
Copy pathclickup_client.py
File metadata and controls
370 lines (313 loc) · 13.8 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
import os
import requests
from typing import Optional, List, Dict
from dotenv import load_dotenv
load_dotenv()
class ClickUpClient:
"""Handles ClickUp API interactions."""
def __init__(self):
self.client_id = os.getenv("CLICKUP_CLIENT_ID")
self.client_secret = os.getenv("CLICKUP_CLIENT_SECRET")
self.base_url = "https://api.clickup.com/api/v2"
def get_authorization_url(self, redirect_uri: str, state: str) -> str:
"""Generate ClickUp OAuth authorization URL."""
auth_url = (
f"https://app.clickup.com/api?"
f"client_id={self.client_id}&"
f"redirect_uri={redirect_uri}&"
f"state={state}"
)
return auth_url
def exchange_code_for_token(self, code: str) -> dict:
"""Exchange authorization code for access token."""
try:
response = requests.post(
"https://api.clickup.com/api/v2/oauth/token",
params={
"client_id": self.client_id,
"client_secret": self.client_secret,
"code": code
}
)
if response.status_code == 200:
data = response.json()
print(f"🔍 OAuth Response: {data}", flush=True)
return {
"access_token": data.get("access_token"),
"token_type": data.get("token_type", "Bearer")
}
else:
raise Exception(f"Token exchange failed: {response.status_code} - {response.text}")
except Exception as e:
print(f"❌ Token exchange error: {e}", flush=True)
raise
def get_authorized_user(self, access_token: str) -> dict:
"""Get the authenticated user's information."""
try:
headers = {"Authorization": access_token}
response = requests.get(
f"{self.base_url}/user",
headers=headers
)
if response.status_code == 200:
data = response.json()
user = data.get("user", {})
return {
"id": user.get("id"),
"username": user.get("username"),
"email": user.get("email")
}
else:
print(f"❌ Error getting user: {response.status_code}", flush=True)
return {}
except Exception as e:
print(f"❌ Error getting user: {e}", flush=True)
return {}
def get_workspaces(self, access_token: str) -> List[Dict]:
"""Get all teams/workspaces the user has access to."""
try:
headers = {"Authorization": access_token}
response = requests.get(
f"{self.base_url}/team",
headers=headers
)
if response.status_code == 200:
data = response.json()
teams = data.get("teams", [])
workspaces = []
for team in teams:
workspaces.append({
"id": team.get("id"),
"name": team.get("name"),
"color": team.get("color"),
"avatar": team.get("avatar")
})
return workspaces
else:
print(f"❌ Error getting workspaces: {response.status_code}", flush=True)
return []
except Exception as e:
print(f"❌ Error getting workspaces: {e}", flush=True)
return []
def get_spaces(self, access_token: str, team_id: str) -> List[Dict]:
"""Get all spaces in a workspace."""
try:
headers = {"Authorization": access_token}
response = requests.get(
f"{self.base_url}/team/{team_id}/space",
headers=headers,
params={"archived": "false"}
)
if response.status_code == 200:
data = response.json()
spaces = data.get("spaces", [])
space_list = []
for space in spaces:
space_list.append({
"id": space.get("id"),
"name": space.get("name"),
"private": space.get("private", False),
"color": space.get("color")
})
return space_list
else:
print(f"❌ Error getting spaces: {response.status_code}", flush=True)
return []
except Exception as e:
print(f"❌ Error getting spaces: {e}", flush=True)
return []
def get_lists(self, access_token: str, space_id: str) -> List[Dict]:
"""Get all lists in a space."""
try:
headers = {"Authorization": access_token}
response = requests.get(
f"{self.base_url}/space/{space_id}/list",
headers=headers,
params={"archived": "false"}
)
if response.status_code == 200:
data = response.json()
lists = data.get("lists", [])
list_data = []
for lst in lists:
list_data.append({
"id": lst.get("id"),
"name": lst.get("name"),
"space_id": space_id,
"folder_id": lst.get("folder", {}).get("id") if lst.get("folder") else None
})
return list_data
else:
print(f"❌ Error getting lists: {response.status_code}", flush=True)
return []
except Exception as e:
print(f"❌ Error getting lists: {e}", flush=True)
return []
def get_all_lists(self, access_token: str, team_id: str) -> List[Dict]:
"""Get all lists across all spaces in a workspace."""
all_lists = []
# Get all spaces
spaces = self.get_spaces(access_token, team_id)
# Get lists for each space
for space in spaces:
lists = self.get_lists(access_token, space["id"])
for lst in lists:
lst["space_name"] = space["name"]
all_lists.append(lst)
return all_lists
def get_workspace_members(self, access_token: str, team_id: str) -> List[Dict]:
"""Get all members in a workspace."""
try:
headers = {"Authorization": access_token}
response = requests.get(
f"{self.base_url}/team/{team_id}",
headers=headers
)
if response.status_code == 200:
data = response.json()
team = data.get("team", {})
members_data = team.get("members", [])
members = []
for member in members_data:
user = member.get("user", {})
members.append({
"id": user.get("id"),
"username": user.get("username"),
"email": user.get("email"),
"initials": user.get("initials", ""),
"color": user.get("color"),
"profilePicture": user.get("profilePicture")
})
print(f"✅ Found {len(members)} workspace members", flush=True)
return members
else:
print(f"❌ Error getting members: {response.status_code}", flush=True)
return []
except Exception as e:
print(f"❌ Error getting members: {e}", flush=True)
return []
async def create_task(
self,
access_token: str,
list_id: str,
name: str,
description: Optional[str] = None,
priority: Optional[int] = None,
status: Optional[str] = None,
due_date: Optional[str] = None,
timezone: str = "UTC",
assignees: Optional[List[str]] = None
) -> Optional[dict]:
"""
Create a task in ClickUp.
Args:
access_token: User's OAuth token
list_id: ID of the list to create task in
name: Task name/title
description: Optional task description
priority: Priority (1=urgent, 2=high, 3=normal, 4=low)
status: Optional status name
due_date: Optional due date in ISO format or Unix timestamp (milliseconds)
Returns:
Task data if successful, None otherwise
"""
try:
headers = {
"Authorization": access_token,
"Content-Type": "application/json"
}
# Build task data
task_data = {
"name": name
}
# Add description with "Created via Omi" footer
if description:
task_data["description"] = f"{description}\n\n--\nCreated via Omi"
else:
task_data["description"] = "Created via Omi"
if priority:
task_data["priority"] = priority
if status:
task_data["status"] = status
if assignees and len(assignees) > 0:
# ClickUp expects list of user IDs as integers
task_data["assignees"] = [int(user_id) for user_id in assignees if user_id]
print(f"👥 Assignees: {assignees}", flush=True)
if due_date:
# Convert ISO date string to Unix timestamp in milliseconds
# ClickUp expects Unix timestamp in milliseconds
# IMPORTANT: Also need to set due_date_time=true for time to show!
from datetime import datetime
try:
# Try to use timezone if pytz is available
try:
import pytz
tz = pytz.timezone(timezone)
except (ImportError, Exception):
# Fallback to no timezone (naive datetime)
tz = None
# Check if time is included in the date string
has_time = 'T' in due_date
# Try parsing as ISO format
if has_time:
# Full datetime - parse the time component
dt_naive = datetime.fromisoformat(due_date.replace('Z', ''))
if tz:
dt = tz.localize(dt_naive)
else:
dt = dt_naive
else:
# Just date, set time to end of day
dt_naive = datetime.fromisoformat(due_date + 'T23:59:59')
if tz:
dt = tz.localize(dt_naive)
else:
dt = dt_naive
# Convert to Unix timestamp in milliseconds
due_timestamp = int(dt.timestamp() * 1000)
task_data["due_date"] = due_timestamp
# CRITICAL: Set due_date_time=true when time component exists
# This tells ClickUp to display the time, not just the date
if has_time:
task_data["due_date_time"] = True
print(f"📅 Due date with TIME: {due_date} ({timezone if tz else 'system'}) → {due_timestamp}", flush=True)
else:
task_data["due_date_time"] = False
print(f"📅 Due date (no time): {due_date} ({timezone if tz else 'system'}) → {due_timestamp}", flush=True)
except Exception as e:
print(f"⚠️ Could not parse due date '{due_date}': {e}", flush=True)
import traceback
traceback.print_exc()
print(f"📤 Creating task: {name} in list {list_id}", flush=True)
response = requests.post(
f"{self.base_url}/list/{list_id}/task",
headers=headers,
json=task_data
)
if response.status_code == 200:
data = response.json()
task = data
print(f"✅ Task created: {task.get('id')}", flush=True)
return {
"success": True,
"task_id": task.get("id"),
"task_name": task.get("name"),
"task_url": task.get("url"),
"status": task.get("status", {}).get("status"),
"list_id": list_id
}
else:
error_msg = f"{response.status_code} - {response.text}"
print(f"❌ Error creating task: {error_msg}", flush=True)
return {
"success": False,
"error": error_msg
}
except Exception as e:
print(f"❌ Error creating task: {e}", flush=True)
import traceback
traceback.print_exc()
return {
"success": False,
"error": str(e)
}