Mr-Help commited on
Commit
3f88aac
·
verified ·
1 Parent(s): f4d9313

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -58
app.py CHANGED
@@ -107,25 +107,30 @@ async def create_task(request: Request):
107
 
108
  # Create ClickUp task according to task type
109
  description_text = ""
110
-
111
  task_type_lower = task_type.lower()
112
-
113
- if task_type_lower == "strategy":
114
- status = "to do"
115
- description_text = description
116
-
117
- elif task_type_lower == "content":
 
 
 
 
 
 
118
  status = "backlog"
119
- description_text = f"{goal.strip()}".strip()
120
  if platforms:
121
  description_text += "\n\nPlatforms: " + ", ".join(platforms)
122
-
123
  elif task_type_lower == "creative":
124
  status = "to do"
125
- description_text = f"{creative_type.strip()}".strip()
126
  if platforms:
127
  description_text += "\n\nPlatforms: " + ", ".join(platforms)
128
-
129
  elif task_type_lower == "ads":
130
  status = "ready"
131
  parts = []
@@ -136,56 +141,89 @@ async def create_task(request: Request):
136
  if platforms:
137
  parts.append("Platforms: " + ", ".join(platforms))
138
  description_text = "\n\n".join(parts)
139
-
140
- elif task_type_lower == "posting":
141
- status = "ready for posting"
142
- parts = []
143
- if posting_content:
144
- parts.append(posting_content.strip())
145
- if platforms:
146
- parts.append("Platforms: " + ", ".join(platforms))
147
- if attachment_link:
148
- parts.append(f"Attachment: {attachment_link.strip()}")
149
- description_text = "\n\n".join(parts)
150
-
151
- elif task_type_lower == "ads report":
152
- status = "ready"
153
- platforms_text = ", ".join(platforms) if platforms else ""
154
- description_text = f"Prepare an ads report for {platforms_text}"
155
- if start_date and end_date:
156
- description_text += f" from {start_date} to {end_date}"
157
-
158
- else:
159
- return {"error": f"Unsupported task type: {task_type}"}
160
 
161
- # Construct ClickUp task payload
162
- payload = {
163
- "name": task_title,
164
- "description": description_text,
165
- "assignees": [int(uid) for uid in assignee_ids],
166
- "status": status
167
- }
168
-
169
- # Handle due date
170
- if deadline:
171
- try:
172
- due_timestamp = int(datetime.strptime(deadline, "%Y-%m-%d").timestamp() * 1000)
173
- payload["due_date"] = due_timestamp
174
- print(f"Due Date (timestamp): {due_timestamp}")
175
- except ValueError:
176
- print("Invalid deadline format. Skipping due_date.")
177
-
178
- # Send request to ClickUp
179
- create_url = f"{CLICKUP_URL_BASE}/list/{list_id}/task"
180
- clickup_response = requests.post(create_url, headers=headers, json=payload)
181
- clickup_data = clickup_response.json()
182
- print(f"ClickUp Response: {clickup_response.status_code}, {clickup_data}")
183
-
184
 
185
- # WhatsApp notification part is commented out for now
186
- # We'll handle WhatsApp notification later
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
 
188
- return {"status": "Task data received and parsed"}
189
 
190
  # Function to get task name by task ID
191
  def get_task_name(task_id):
 
107
 
108
  # Create ClickUp task according to task type
109
  description_text = ""
 
110
  task_type_lower = task_type.lower()
111
+
112
+ # Template and field setup
113
+ template_ids = {
114
+ "content": "t-8698wwx4z",
115
+ "creative": "t-8698wwx4z",
116
+ "ads": "t-8698wwx4z"
117
+ }
118
+ custom_field_id = "64b6898b-eaee-4dd1-b819-a5b142226f69"
119
+ team_id = "9012303718"
120
+
121
+ # Shared description builder
122
+ if task_type_lower == "content":
123
  status = "backlog"
124
+ description_text = goal.strip()
125
  if platforms:
126
  description_text += "\n\nPlatforms: " + ", ".join(platforms)
127
+
128
  elif task_type_lower == "creative":
129
  status = "to do"
130
+ description_text = creative_type.strip()
131
  if platforms:
132
  description_text += "\n\nPlatforms: " + ", ".join(platforms)
133
+
134
  elif task_type_lower == "ads":
135
  status = "ready"
136
  parts = []
 
141
  if platforms:
142
  parts.append("Platforms: " + ", ".join(platforms))
143
  description_text = "\n\n".join(parts)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
 
145
+ if task_type_lower in template_ids:
146
+ # Create from template
147
+ template_id = template_ids[task_type_lower]
148
+ template_url = f"{CLICKUP_URL_BASE}/list/{list_id}/taskTemplate/{template_id}"
149
+ template_payload = {
150
+ "name": task_title
151
+ }
152
+
153
+ response = requests.post(template_url, headers=headers, json=template_payload)
154
+ print("Template Creation Status:", response.status_code)
155
+
156
+ if response.ok:
157
+ new_task = response.json()
158
+ new_task_id = new_task.get("id")
159
+ print("✅ Task created from template:", new_task_id)
160
+
161
+ # Update custom field (used as description)
162
+ update_field_url = f"{CLICKUP_URL_BASE}/task/{new_task_id}/field/{custom_field_id}?custom_task_ids=true&team_id={team_id}"
163
+ field_payload = {"value": description_text}
164
+ field_update = requests.post(update_field_url, headers=headers, json=field_payload)
165
+
166
+ print("📥 Field Update Status:", field_update.status_code)
167
+ print("📥 Field Update Response:", field_update.text)
168
 
169
+ return {
170
+ "status": "Template-based task created",
171
+ "task_id": new_task_id
172
+ }
173
+ else:
174
+ print("❌ Failed to create task from template")
175
+ return {"error": "Template task creation failed"}
176
+
177
+ # If not using template, fallback to normal creation
178
+ elif task_type_lower in ["strategy", "posting", "ads report"]:
179
+ if task_type_lower == "strategy":
180
+ status = "to do"
181
+ description_text = description
182
+
183
+ elif task_type_lower == "posting":
184
+ status = "ready for posting"
185
+ parts = []
186
+ if posting_content:
187
+ parts.append(posting_content.strip())
188
+ if platforms:
189
+ parts.append("Platforms: " + ", ".join(platforms))
190
+ if attachment_link:
191
+ parts.append(f"Attachment: {attachment_link.strip()}")
192
+ description_text = "\n\n".join(parts)
193
+
194
+ elif task_type_lower == "ads report":
195
+ status = "ready"
196
+ platforms_text = ", ".join(platforms) if platforms else ""
197
+ description_text = f"Prepare an ads report for {platforms_text}"
198
+ if start_date and end_date:
199
+ description_text += f" from {start_date} to {end_date}"
200
+
201
+ payload = {
202
+ "name": task_title,
203
+ "description": description_text,
204
+ "assignees": [int(uid) for uid in assignee_ids],
205
+ "status": status
206
+ }
207
+
208
+ # Handle due date
209
+ if deadline:
210
+ try:
211
+ due_timestamp = int(datetime.strptime(deadline, "%Y-%m-%d").timestamp() * 1000)
212
+ payload["due_date"] = due_timestamp
213
+ print(f"Due Date (timestamp): {due_timestamp}")
214
+ except ValueError:
215
+ print("Invalid deadline format. Skipping due_date.")
216
+
217
+ create_url = f"{CLICKUP_URL_BASE}/list/{list_id}/task"
218
+ clickup_response = requests.post(create_url, headers=headers, json=payload)
219
+ clickup_data = clickup_response.json()
220
+ print(f"ClickUp Response: {clickup_response.status_code}, {clickup_data}")
221
+
222
+ return {"status": "Standard task created", "clickup": clickup_data}
223
+
224
+ else:
225
+ return {"error": f"Unsupported task type: {task_type}"}
226
 
 
227
 
228
  # Function to get task name by task ID
229
  def get_task_name(task_id):