> ## Documentation Index
> Fetch the complete documentation index at: https://prismeai-legacy.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Contact Routing

> Build an intelligent contact form that automatically routes inquiries to the right department using Gen.AI

This tutorial guides you through creating an intelligent contact form powered by generative AI. The system automatically analyzes the content of submitted inquiries and routes them to the appropriate department within your organization—be it sales, support, or careers—enhancing operational efficiency and customer response times.

## What You'll Build

A complete AI-powered contact routing system with:

* A customizable contact form for your website or application
* Intelligent content analysis using OpenAI
* Automated routing to department-specific email addresses
* Attachment handling capabilities
* Easy maintenance and monitoring

<Note>
  This solution demonstrates how Prisme.ai can combine AI models with automation workflows to create intelligent business processes that scale with your organization's needs.
</Note>

## Prerequisites

Before starting this tutorial, make sure you have:

* An active Prisme.ai account
* The SendMail app installed in your workspace

## Step 1: Creating Your Workspace

First, let's set up a dedicated workspace for our contact routing system:

<Steps>
  <Step title="Access AI Builder">
    Log in to your Prisme.ai account and navigate to the AI Builder product
  </Step>

  <Step title="Create a New Workspace">
    Click the "Create Workspace" button to start a new project
  </Step>

  <Step title="Configure Workspace Settings">
    * Name your workspace "AI Contact Routing" (or a name of your choice)
    * Add a description like "Intelligent contact form with AI-based routing"
    * Select an appropriate icon for your workspace
    * Configure any additional settings as needed
  </Step>
</Steps>

## Step 2: Building the Contact Form

Now, let's create a user-friendly contact form that will collect information from your visitors:

<Steps>
  <Step title="Create a New Page">
    In your workspace, navigate to the Pages section and click "Create Page"
  </Step>

  <Step title="Configure Page Properties">
    * Name your page "Contact Us Form"
    * Set the slug to "contact-us" (this will be used in the URL)
  </Step>

  <Step title="Add a Form Block">
    Add a Form block to your page for inquiries with the following configuration:

    ```yaml theme={null}
    slug: contact-us
    name: Contact Us Form
    blocks:
      - slug: Form
        schema:
          type: object
          properties:
            name:
              type: string
              title: Name
              description: Fill your first name please
              placeholder: John doe
            message:
              type: string
              title: Message
              description: Fill your message
              placeholder: your message
              ui:widget: textarea
            email:
              type: string
              placeholder: john.doe@example.com
              description: your email
            attachement:
              type: string
              ui:widget: upload
              title: Attachement
              description: Attach a file if needed
              placeholder: Your file
        onSubmit: formSubmit
    ```
  </Step>

  <Step title="Set the Form Submission Event">
    In the form configuration, set the "onSubmit" field to "formSubmit" - this event name will trigger our automation
  </Step>

  <Step title="Save Your Page">
    Save your page configuration. Your form should now be ready to collect user inquiries
  </Step>

  <Step title="Get the Page URL">
    Click the share icon to get your page's URL. The format will be: `workspace-slug.pages.host/lang/contact-us`
  </Step>
</Steps>

## Step 3: Installing Required Apps

Before creating our automation, we need to install the necessary apps from the Prisme.ai App Store:

<Steps>
  <Step title="Access the App Store">
    Navigate to the "Apps" section in your workspace
  </Step>

  <Step title="Install the SendMail App">
    * Click the "+" button to browse available apps
    * Search for "SendMail" and click to install it
    * Follow the on-screen instructions to complete the installation
  </Step>

  <Step title="Install the OpenAI App">
    * Again, click the "+" button in the Apps section
    * Search for "OpenAI" and click to install it
    * During configuration, enter your OpenAI API key
  </Step>
</Steps>

## Step 4: Creating the Form Submission Handler

Now, let's build the automation that will process form submissions and route them based on AI analysis:

<Steps>
  <Step title="Create a New Automation">
    * Navigate to the "Automations" section of your workspace
    * Click "Create Automation"
    * Name it "Form Submission Handler"
    * Set the slug to "form-submission-handler"
  </Step>

  <Step title="Configure the Event Trigger">
    Set up the automation to trigger on the "formSubmit" event (the same event we configured in our form)
  </Step>

  <Step title="Set Default Recipient">
    Add a "Set var" instruction:

    * Variable name: "recipient"
    * Value: "[hello@example.com](mailto:hello@example.com)" (a default email in case routing fails)
  </Step>

  <Step title="Add OpenAI Analysis">
    Add an "OpenAI.chat-completion" instruction:

    * Model: Select "gpt-4" (or another appropriate model)
    * Messages:
      * System role: "Given the following customer inquiry, categorize it as either sales, support, or careers. Provide your categorization based on the content. Answer only with category nothing else."
      * User role: `"{{payload.message}}"`
    * Output: "result"
  </Step>

  <Step title="Extract AI Response">
    Add another "Set var" instruction:

    * Variable name: "routingDecision"
    * Value: `"{{result.choices[0].message.content}}"`
  </Step>

  <Step title="Create Conditional Routing">
    Add a "conditions" instruction with the following branches:

    * Condition: `{{routingDecision}} = "sales"`
      * Action: Set recipient to "[sales@example.com](mailto:sales@example.com)"
    * Condition: `{{routingDecision}} = "support"`
      * Action: Set recipient to "[support@example.com](mailto:support@example.com)"
    * Condition: `{{routingDecision}} = "careers"`
      * Action: Set recipient to "[careers@example.com](mailto:careers@example.com)"
  </Step>

  <Step title="Configure Email Notification">
    Add a "SendMail.sendMail" instruction:

    * To: `"{{recipient}}"` (the dynamically set department email)
    * ReplyTo: `"{{payload.email}}"` (the submitter's email)
    * Subject: "New Contact Form Submission"
    * Body: "Message: `{{payload.message}}`, Name: `{{payload.name}}`, Attachment: `{{payload.attachment}}`"
  </Step>

  <Step title="Set Output and Save">
    * Set the automation output to `{{routingDecision}}`
    * Save your automation
  </Step>
</Steps>

## Step 5: Testing Your Contact Routing System

Now it's time to test your AI contact routing system:

<Steps>
  <Step title="Access Your Contact Form">
    Navigate to your contact form using the URL you obtained earlier
  </Step>

  <Step title="Submit Test Inquiries">
    Fill out the form with different types of inquiries to test the routing:

    * A sales-related inquiry (e.g., "I'm interested in pricing for your enterprise plan")
    * A support-related inquiry (e.g., "I'm having trouble logging into my account")
    * A careers-related inquiry (e.g., "I'd like to apply for the marketing position")
  </Step>

  <Step title="Verify Email Routing">
    Check that each test inquiry is routed to the correct department email
  </Step>

  <Step title="Test Attachment Handling">
    Submit a form with an attachment to ensure files are properly included in the emails
  </Step>
</Steps>

## Step 6: Version Control and Deployment

To finalize your contact routing system:

<Steps>
  <Step title="Pull Your Changes">
    Use the "Pull" button in your workspace to create a new version
  </Step>

  <Step title="Set Up Access Controls">
    Configure Role-Based Access Control (RBAC) to determine who can access and modify your contact routing system
  </Step>
</Steps>

## Monitoring and Optimization

After deployment, regularly check the system's performance:

<Steps>
  <Step title="Monitor Activity Logs">
    Review the activity logs to track form submissions and routing decisions
  </Step>

  <Step title="Review Classification Accuracy">
    Periodically check if inquiries are being routed correctly and refine your system as needed
  </Step>

  <Step title="Optimize the AI Prompt">
    If necessary, adjust the OpenAI prompt to improve classification accuracy for your specific use case
  </Step>
</Steps>

## Extending Your Contact Routing System

Consider these enhancements to make your system even more powerful:

* **Multi-level Classification**: Add subcategories to route inquiries to specific teams within departments
* **Priority Detection**: Use AI to identify urgent inquiries and flag them for immediate attention
* **Sentiment Analysis**: Detect the emotional tone of inquiries to handle frustrated customers appropriately
* **Automated Responses**: Send automated acknowledgment emails based on the inquiry type
* **CRM Integration**: Connect with your CRM system to log inquiries as leads or support tickets
* **Slack Integration**: Send notifications to Slack channels for real-time team collaboration

## Complete YAML Configuration

Here's the complete YAML for the Form Submission Handler automation:

```yaml theme={null}
slug: form-submission-handler
name: Form Submission Handler
do:
  - set:
      name: recipient
      value: hello@example.com
  - OpenAI.chat-completion:
      stream:
        options:
          persist: true
      model: mistral-small
      messages:
        - role: user
          content: '{{payload.message}}'
        - role: system
          content: Given the following customer inquiry, categorize it as either sales, support, or careers. Provide your categorization based on the content. Answer only with category nothing else.
      output: result
  - set:
      name: routingDecision
      value: '{{result.choices[0].message.content}}'
  - conditions:
      '{{routingDecision}} = "sales"':
        - set:
            name: recipient
            value: sales@example.com
      '"{{routingDecision}}" = "support"':
        - set:
            name: recipient
            value: support@example.com
      '"{{routingDecision}}" = "careers"':
        - set:
            name: recipient
            value: careers@example.com
      default: []
  - SendMail.sendMail:
      to: '{{recipient}}'
      replyTo: '{{payload.email}}'
      subject: New Contact Form Submission
      body: 'Message: {{payload.message}}, Name: {{payload.name}}, Attachment: {{payload.attachment}}'
output: '{{routingDecision}}'
when:
  events:
    - formSubmit
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Build a Document Classification System" icon="file" href="/resources/tutorials/data-classification-agent">
    Learn how to classify and organize documents with AI
  </Card>

  <Card title="Create Webhook Integrations" icon="webhook" href="/resources/tutorials/webhook-builder">
    Connect your contact system to external services using webhooks
  </Card>

  <Card title="Implement RAG Agents" icon="database" href="/resources/tutorials/no-code-rag-agent">
    Build a knowledge base to provide automated responses to common inquiries
  </Card>

  <Card title="Explore Custom Applications" icon="puzzle-piece" href="/resources/tutorials/ai-builder-custom-apps">
    Develop more sophisticated applications using AI Builder
  </Card>
</CardGroup>
