Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ from fastapi import FastAPI
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import requests
|
| 4 |
import logging
|
|
|
|
| 5 |
|
| 6 |
# Configure logging
|
| 7 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
@@ -41,6 +42,9 @@ class TaskData(BaseModel):
|
|
| 41 |
def create_task(task: TaskData):
|
| 42 |
logging.info(f"Received task data: {task}")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
description = f"Task Type: {task.task_type}\nCampaign: {task.campaign_name}\nPlatforms: {', '.join(task.platforms)}"
|
| 45 |
|
| 46 |
payload = {
|
|
@@ -64,15 +68,17 @@ def create_task(task: TaskData):
|
|
| 64 |
task_link = f"https://app.clickup.com/t/{task_id}"
|
| 65 |
|
| 66 |
whatsapp_payload = {
|
| 67 |
-
"chatId": "201092003112@c.us",
|
| 68 |
-
"message": f"🚀 *New Task Assigned!*\n\n📌 *Task:* {task.task_name}\n📅 *Due Date:* {
|
| 69 |
}
|
| 70 |
else:
|
| 71 |
# If ClickUp API fails, send an error message on WhatsApp mentioning the task name
|
| 72 |
error_message = response_data.get("err", "Unknown error")
|
|
|
|
|
|
|
| 73 |
whatsapp_payload = {
|
| 74 |
-
"chatId": "201092003112@c.us",
|
| 75 |
-
"message": f"⚠️ *Task Creation Failed!*\n\n📌 *Task:* {task.task_name}\n❌ *Error:* {error_message}\n\nPlease check the request and try again."
|
| 76 |
}
|
| 77 |
|
| 78 |
logging.info(f"Sending WhatsApp notification with payload: {whatsapp_payload}")
|
|
@@ -80,4 +86,4 @@ def create_task(task: TaskData):
|
|
| 80 |
|
| 81 |
logging.info(f"WhatsApp API Response: {whatsapp_response.status_code}, {whatsapp_response.json()}")
|
| 82 |
|
| 83 |
-
return {"status_code": response.status_code, "response": response_data}
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import requests
|
| 4 |
import logging
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
# Configure logging
|
| 8 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 42 |
def create_task(task: TaskData):
|
| 43 |
logging.info(f"Received task data: {task}")
|
| 44 |
|
| 45 |
+
# Convert Unix timestamp to a readable date
|
| 46 |
+
due_date_human = datetime.utcfromtimestamp(task.due_date / 1000).strftime('%Y-%m-%d %H:%M:%S')
|
| 47 |
+
|
| 48 |
description = f"Task Type: {task.task_type}\nCampaign: {task.campaign_name}\nPlatforms: {', '.join(task.platforms)}"
|
| 49 |
|
| 50 |
payload = {
|
|
|
|
| 68 |
task_link = f"https://app.clickup.com/t/{task_id}"
|
| 69 |
|
| 70 |
whatsapp_payload = {
|
| 71 |
+
"chatId": "201092003112@c.us", # Updated recipient number
|
| 72 |
+
"message": f"🚀 *New Task Assigned!*\n\n📌 *Task:* {task.task_name}\n📅 *Due Date:* {due_date_human}\n👤 *Assigned To:* {', '.join(map(str, task.assignees))}\n📝 *Description:* {description}\n\n🔗 *View Task:* {task_link}\n\nLet's get this done! ✅"
|
| 73 |
}
|
| 74 |
else:
|
| 75 |
# If ClickUp API fails, send an error message on WhatsApp mentioning the task name
|
| 76 |
error_message = response_data.get("err", "Unknown error")
|
| 77 |
+
failure_time = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
|
| 78 |
+
|
| 79 |
whatsapp_payload = {
|
| 80 |
+
"chatId": "201092003112@c.us", # Updated recipient number
|
| 81 |
+
"message": f"⚠️ *Task Creation Failed!*\n\n📌 *Task:* {task.task_name}\n❌ *Error:* {error_message}\n⏰ *Time:* {failure_time}\n\nPlease check the request and try again."
|
| 82 |
}
|
| 83 |
|
| 84 |
logging.info(f"Sending WhatsApp notification with payload: {whatsapp_payload}")
|
|
|
|
| 86 |
|
| 87 |
logging.info(f"WhatsApp API Response: {whatsapp_response.status_code}, {whatsapp_response.json()}")
|
| 88 |
|
| 89 |
+
return {"status_code": response.status_code, "response": response_data}
|