Path: blob/master/webhooks/webhook_lambda/lambda_function.py
1072 views
import base641import json2from urllib.parse import unquote_plus345def print_vehicle_info(results):6for result in results:7print("Vehicle detected:")8print("Plate: ", result["candidates"][0]["plate"])9print("Color: ", result["color"][0]["color"])10print(11"Make - Model: ",12result["model_make"][0]["make"] + " - " + result["model_make"][0]["model"],13)14print("Orientation: ", result["orientation"][0]["orientation"])151617def handle_payload_with_image(body, boundary):18# Split the body into parts using the boundary19parts = body.split(b"--" + boundary.encode("utf-8"))2021# Filter out empty parts22parts = [part for part in parts if part.strip()]2324# Process each part25for part in parts:26try:27# Separate headers and content28headers, content = part.split(b"\r\n\r\n", 1)29except ValueError:30# Skip if the part is not valid31continue3233# Handle JSON data34if b"json" in headers:35data = json.loads(content.decode("utf-8"))36results = data["data"]["results"]37print_vehicle_info(results)3839# Handle image40if b"upload" in headers:41# Add logic to handle image42pass434445def handle_payload(body):46# Handle JSON data47if b"json" in body:48content = unquote_plus(body.split(b"json=")[1].decode("utf-8"))49data = json.loads(content)50results = data["data"]["results"]51print_vehicle_info(results)525354def lambda_handler(event, context):55# Retrieve the raw request body from the Lambda event56body = base64.b64decode(event["body"])5758# Extract content type and boundary from headers59content_type = event["headers"]["content-type"]6061split_by_boundary = content_type.split("boundary=")6263if len(split_by_boundary) > 1:64# There is image included in payload65boundary = split_by_boundary[1]66handle_payload_with_image(body, boundary)6768else:69handle_payload(body)7071# Return a response72response = {"statusCode": 200, "body": "Webhook processing successful"}73return response747576