Update app.py
Browse files
app.py
CHANGED
|
@@ -93,39 +93,56 @@ async def task_update(request: Request):
|
|
| 93 |
data = await request.json()
|
| 94 |
logging.info(f"Received task update from ClickUp: {data}")
|
| 95 |
|
| 96 |
-
|
| 97 |
task_id = data.get("task_id", "Unknown ID")
|
| 98 |
task_name = data.get("task", {}).get("name", "Unknown Task")
|
| 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 |
return {"status": "Update received"}
|
|
|
|
| 93 |
data = await request.json()
|
| 94 |
logging.info(f"Received task update from ClickUp: {data}")
|
| 95 |
|
| 96 |
+
event_type = data.get("event")
|
| 97 |
task_id = data.get("task_id", "Unknown ID")
|
| 98 |
task_name = data.get("task", {}).get("name", "Unknown Task")
|
| 99 |
+
task_link = f"https://app.clickup.com/t/{task_id}"
|
| 100 |
+
|
| 101 |
+
if event_type == "taskUpdated":
|
| 102 |
+
# Extract the new status
|
| 103 |
+
history_items = data.get("history_items", [])
|
| 104 |
+
if history_items:
|
| 105 |
+
new_status = history_items[0].get("after", {}).get("status", "").lower()
|
| 106 |
+
action_timestamp = history_items[0].get("date", 0)
|
| 107 |
+
else:
|
| 108 |
+
logging.error("No history items found in taskUpdated event.")
|
| 109 |
+
return {"error": "No history items found."}
|
| 110 |
+
|
| 111 |
+
# Convert action timestamp to readable date
|
| 112 |
+
action_date_human = datetime.utcfromtimestamp(int(action_timestamp) / 1000).strftime('%Y-%m-%d %H:%M:%S') if action_timestamp else "Unknown Date"
|
| 113 |
+
|
| 114 |
+
logging.info(f"Task: {task_name}, New Status: {new_status}, Action Date: {action_date_human}")
|
| 115 |
+
|
| 116 |
+
if new_status == "ready for review":
|
| 117 |
+
whatsapp_payload = {
|
| 118 |
+
"chatId": "201092003112@c.us",
|
| 119 |
+
"message": f"📝 *Task Submitted for Review!*\n\n📌 *Task:* {task_name}\n📅 *Submitted On:* {action_date_human}\n\n🔗 *View Task:* {task_link}\n\nPlease review the task and provide feedback."
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
logging.info(f"Sending WhatsApp notification: {whatsapp_payload}")
|
| 123 |
+
whatsapp_response = requests.post(WHATSAPP_URL, json=whatsapp_payload, headers=WHATSAPP_HEADERS)
|
| 124 |
+
logging.info(f"WhatsApp API Response: {whatsapp_response.status_code} - {whatsapp_response.text}")
|
| 125 |
+
|
| 126 |
+
elif event_type == "taskTagUpdated":
|
| 127 |
+
# Extract tag information
|
| 128 |
+
history_items = data.get("history_items", [])
|
| 129 |
+
if history_items:
|
| 130 |
+
added_tags = history_items[0].get("after", [])
|
| 131 |
+
due_date_timestamp = data.get("task", {}).get("due_date", 0)
|
| 132 |
+
else:
|
| 133 |
+
logging.error("No history items found in taskTagUpdated event.")
|
| 134 |
+
return {"error": "No history items found."}
|
| 135 |
+
|
| 136 |
+
if "missed due date" in added_tags:
|
| 137 |
+
due_date_human = datetime.utcfromtimestamp(int(due_date_timestamp) / 1000).strftime('%Y-%m-%d %H:%M:%S') if due_date_timestamp else "Unknown Date"
|
| 138 |
+
|
| 139 |
+
whatsapp_payload = {
|
| 140 |
+
"chatId": "201092003112@c.us",
|
| 141 |
+
"message": f"⚠️ *Task Overdue!*\n\n📌 *Task:* {task_name}\n⏳ *Due Date:* {due_date_human}\n\n🔗 *View Task:* {task_link}\n\nPlease take action as soon as possible."
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
logging.info(f"Sending Overdue Task Notification: {whatsapp_payload}")
|
| 145 |
+
whatsapp_response = requests.post(WHATSAPP_URL, json=whatsapp_payload, headers=WHATSAPP_HEADERS)
|
| 146 |
+
logging.info(f"WhatsApp API Response: {whatsapp_response.status_code} - {whatsapp_response.text}")
|
| 147 |
|
| 148 |
return {"status": "Update received"}
|