CoolFace
Apppublic

Mr-Help/Tash-Create-Contractor-List

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
main.py98 linesDownload Raw Back to root
1from fastapi import FastAPI, Request2import requests3 4app = FastAPI()5 6# Constants7ACCESS_TOKEN = "2585415_877e3999ec927cff5d896269c348106949c50f9b8ad54fdaeac07ad00687ce3e"8TEAM_ID = "9018441024"9 10# Video Contractor Settings11VIDEO_FOLDER_ID = "90182650380"12VIDEO_TEMPLATE_ID = "t-901804880155"13 14# Image Contractor Settings15IMAGE_FOLDER_ID = "90182896356"16IMAGE_TEMPLATE_ID = "t-901809661788"17 18GOOGLE_SHEET_WEBHOOK = "https://script.google.com/macros/s/AKfycbw7Ognf2qKo-nhlVsiQkbb21vYqNp6VV3IfriBadQ06--x2VYn7KZIQuKRW9BPyjmGO/exec"19 20def create_list_from_template(folder_id, template_id, list_name):21    url = f"https://api.clickup.com/api/v2/folder/{folder_id}/list_template/{template_id}"22    headers = {23        "Authorization": ACCESS_TOKEN,24        "Content-Type": "application/json"25    }26    payload = {27        "name": list_name,28        "options": {29            "return_immediately": True30        }31    }32 33    response = requests.post(url, headers=headers, json=payload)34 35    if response.status_code == 200:36        result = response.json()37        list_id = result.get("id")38        list_url = f"https://app.clickup.com/{TEAM_ID}/v/li/{list_id}"39        return {"message": "✅ List created successfully.", "list_url": list_url}40    else:41        return {42            "message": "❌ Failed to create list.",43            "status_code": response.status_code,44            "error": response.text45        }46 47@app.post("/video")48async def create_video_contractor_list(request: Request):49    data = await request.json()50    list_name = data.get("listName")51 52    if not list_name:53        return {"error": "Missing 'listName' in request"}54 55    return create_list_from_template(VIDEO_FOLDER_ID, VIDEO_TEMPLATE_ID, list_name)56 57 58@app.post("/image")59async def create_image_contractor_list(request: Request):60    data = await request.json()61    list_name = data.get("listName")62 63    if not list_name:64        return {"error": "Missing 'listName' in request"}65 66    return create_list_from_template(IMAGE_FOLDER_ID, IMAGE_TEMPLATE_ID, list_name)67 68 69@app.post("/registerContractor")70async def register_contractor(request: Request):71    data = await request.json()72    print("📥 Received contractor data from content.js:", data)73 74    name = data.get("contractorName")75    email = data.get("contractorEmail")76    contractor_type = data.get("contractorType")77 78    if not name or not email or not contractor_type:79        return {"error": "Missing one or more required fields"}80 81    sheet_payload = {82        "name": name,83        "email": email,84        "type": contractor_type85    }86 87    try:88        sheet_response = requests.post(GOOGLE_SHEET_WEBHOOK, json=sheet_payload)89        print("📤 Sent to Google Sheet:", sheet_payload)90        return {91            "message": "✅ Contractor registered and sent to sheet.",92            "sheet_response": sheet_response.json()93        }94    except Exception as e:95        print("❌ Failed to send to Google Sheet:", str(e))96        return {"error": "Failed to send to sheet", "details": str(e)}97 98