Mr-Help's picture
Update app.py
33be296 verified
raw
history blame
2.75 kB
from fastapi import FastAPI
from pydantic import BaseModel
import requests
app = FastAPI()
# ClickUp API URL for creating a task (replace with your list ID)
LIST_ID = "901208781277"
URL = f"https://api.clickup.com/api/v2/list/{LIST_ID}/task"
# Your access token
ACCESS_TOKEN = "2144425825_36f2249dc27c5aca075ac5442b1bbcdf01c3a29b9e41b86bda46a6cf651acd0f"
# Headers for authorization
headers = {
"Authorization": ACCESS_TOKEN,
"Content-Type": "application/json"
}
class TaskData(BaseModel):
task_name: str
task_type: str # "Reel" or "Design"
campaign_name: str
platforms: list[str]
assignees: list[int] # List of user IDs
due_date: int # Unix timestamp in milliseconds
@app.post("/singetask")
def create_task(task: TaskData):
description = f"Task Type: {task.task_type}\nCampaign: {task.campaign_name}\nPlatforms: {', '.join(task.platforms)}"
payload = {
"name": task.task_name,
"description": description,
"assignees": task.assignees,
"priority": 3, # Priority levels: 1 (Low), 2 (Normal), 3 (High), 4 (Urgent)
"due_date": task.due_date, # Unix timestamp in milliseconds
"status": "backlog" # Adjust based on your workflow
}
response = requests.post(URL, headers=headers, json=payload)
response_data = response.json()
print(f"Received response from ClickUp: {response.status_code}, {response_data}")
# Prepare WhatsApp notification
if response.status_code == 200:
task_id = response_data.get("id", "Unknown ID")
task_link = f"https://app.clickup.com/t/{task_id}"
whatsapp_payload = {
"chatId": "201040516950@c.us",
"message": f"🚀 *New Task Assigned!*\n\n📌 *Task:* {task.task_name}\n📅 *Due Date:* {task.due_date}\n👤 *Assigned To:* {', '.join(map(str, task.assignees))}\n📝 *Description:* {description}\n\n🔗 *View Task:* {task_link}\n\nLet's get this done! ✅"
}
else:
# If ClickUp API fails, send an error message on WhatsApp mentioning the task name
error_message = response_data.get("err", "Unknown error")
whatsapp_payload = {
"chatId": "201040516950@c.us",
"message": f"⚠️ *Task Creation Failed!*\n\n📌 *Task:* {task.task_name}\n❌ *Error:* {error_message}\n\nPlease check the request and try again."
}
print(f"Sending WhatsApp notification with payload: {whatsapp_payload}")
whatsapp_response = requests.post(WHATSAPP_URL, json=whatsapp_payload, headers=whatsapp_headers)
print(f"WhatsApp API Response: {whatsapp_response.status_code}, {whatsapp_response.json()}")
return {"status_code": response.status_code, "response": response_data}