Mr-Help commited on
Commit
f194d83
·
verified ·
1 Parent(s): d0435d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -32
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
- # Extract necessary fields
97
  task_id = data.get("task_id", "Unknown ID")
98
  task_name = data.get("task", {}).get("name", "Unknown Task")
99
-
100
- # Extract the new status after update
101
- history_items = data.get("history_items", [])
102
- if history_items:
103
- new_status = history_items[0].get("after", {}).get("status", "").lower()
104
- action_timestamp = history_items[0].get("date", 0)
105
- else:
106
- logging.error("No history items found in the update payload.")
107
- return {"error": "No history items found."}
108
-
109
- # Convert action timestamp to readable format
110
- action_date_human = datetime.utcfromtimestamp(int(action_timestamp) / 1000).strftime('%Y-%m-%d %H:%M:%S') if action_timestamp else "Unknown Date"
111
-
112
- logging.info(f"Extracted Status: {new_status}, Task: {task_name}, Date: {action_date_human}")
113
-
114
- # If status is "Ready for Review", send WhatsApp notification
115
- if new_status == "ready for review":
116
- task_link = f"https://app.clickup.com/t/{task_id}"
117
-
118
- whatsapp_payload = {
119
- "chatId": "201092003112@c.us",
120
- "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."
121
- }
122
-
123
- logging.info(f"Sending WhatsApp notification with payload: {whatsapp_payload}")
124
-
125
- # Send WhatsApp request
126
- whatsapp_response = requests.post(WHATSAPP_URL, json=whatsapp_payload, headers=whatsapp_headers)
127
-
128
- logging.info(f"WhatsApp API Response Code: {whatsapp_response.status_code}")
129
- logging.info(f"WhatsApp API Response: {whatsapp_response.text}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"}