Team Ai
Apppublic

Ameer1606/PostgresPro-Support

sourceHugging Facemitupdated 4mo agoView on Hugging Face
0likes
tools.py76 linesDownload Raw Back to src
1import psycopg22from langchain_core.tools import tool3from src.config import PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, PG_DATABASE, PG_SSL4 5def notify_admin(ticket_id: int, title: str):6    """7    Triggered once a row is inserted successfully.8    Prints a secure console alert layout and executes a mock notification payload.9    """10    print("\n" + "="*50)11    print("🚨 [ADMIN ALERT] NEW SUPPORT TICKET CREATED 🚨")12    print("="*50)13    print(f"Ticket ID : {ticket_id}")14    print(f"Title     : {title}")15    print(f"Status    : Awaiting assignment")16    print("="*50)17    print("Sending mock Telegram Bot webhook payload...")18    print("Payload: {'chat_id': 'admin_group', 'text': 'New Ticket #" + str(ticket_id) + " - " + title + "'}")19    print("Notification Dispatched Successfully.\n")20 21@tool22def create_database_ticket(name: str, email: str, title: str, description: str) -> str:23    """24    Creates a support ticket for the user in the PostgreSQL database.25    Use this tool when the user's issue cannot be resolved by the documentation or when they explicitly request to open a ticket.26    You must extract the user's name, email, a brief title for the issue, and a detailed description.27    """28    try:29        # Connect to PostgreSQL30        conn = psycopg2.connect(31            host=PG_HOST,32            port=PG_PORT,33            user=PG_USER,34            password=PG_PASSWORD,35            dbname=PG_DATABASE, 36            sslmode=PG_SSL37        )38        conn.autocommit = True39        cursor = conn.cursor()40 41        # Create table if it doesn't exist42        cursor.execute("""43            CREATE TABLE IF NOT EXISTS tickets (44                id SERIAL PRIMARY KEY,45                customer_name VARCHAR(255) NOT NULL,46                customer_email VARCHAR(255) NOT NULL,47                issue_title VARCHAR(255) NOT NULL,48                issue_description TEXT NOT NULL,49                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP50            )51        """)52 53        # Insert new row with extracted user details54        insert_query = """55            INSERT INTO tickets (customer_name, customer_email, issue_title, issue_description)56            VALUES (%s, %s, %s, %s)57            RETURNING id;58        """59        cursor.execute(insert_query, (name, email, title, description))60        61        ticket_id = cursor.fetchone()[0]62        63        # Close connection64        cursor.close()65        conn.close()66 67        # Trigger admin notification68        notify_admin(ticket_id, title)69 70        return f"Successfully created support ticket #{ticket_id} securely in our database. A support agent will contact you at {email} soon."71    72    except psycopg2.Error as e:73        return f"Failed to create support ticket due to database connectivity fault: {str(e)}"74    except Exception as e:75        return f"Failed to create support ticket due to internal error: {str(e)}"76