Youtube Explanation: https://youtu.be/KgmNiV7SwkU
This n8n workflow is designed to automate the initial intake and scheduling for a law firm. It's split into two main parts:
- New Inquiry Handling: Kicks off when a potential client fills out a JotForm, saves their data, and sends them an initial welcome message on WhatsApp.
- Appointment Scheduling: Activates when the client replies on WhatsApp, allowing an AI agent to chat with them to schedule a consultation.
Here’s a detailed breakdown of the prerequisites and each node.
Prerequisites
Before building this workflow, you'll need accounts and some setup for each of the following services:
JotForm
- JotForm Account: You need an active JotForm account.
- A Published Form: Create a form with the exact fields used in the workflow:
Full Name
, Email Address
, Phone Number
, I am a...
, Legal Service of Interest
, Brief Message
, and How Did You Hear About Us?
.
- API Credentials: Generate API keys from your JotForm account settings to connect it with n8n.
Google
- Google Account: To use Google Sheets and Google Calendar.
- Google Sheet: Create a new sheet named "Law Client Enquiries". The first row must have these exact headers:
Full Name
, Email Address
, Phone Number
, client type
, Legal Service of Interest
, Brief Message
, How Did You Hear About Us?
.
- Google Calendar: An active calendar to manage appointments.
- Google Cloud Project:
- Service Account Credentials (for Sheets): In the Google Cloud Console, create a service account, generate JSON key credentials, and enable the Google Sheets API. You must then share your Google Sheet with the service account's email address (e.g.,
[email protected]
).
- OAuth Credentials (for Calendar): Create OAuth 2.0 Client ID credentials to allow n8n to access your calendar on your behalf. You'll need to enable the Google Calendar API.
- Gemini API Key: Enable the Vertex AI API in your Google Cloud project and generate an API key to use the Google Gemini models.
WhatsApp
- Meta Business Account: Required to use the WhatsApp Business Platform.
- WhatsApp Business Platform Account: You need to set up a business account and connect a phone number to it. This is different from the regular WhatsApp or WhatsApp Business app.
- API Credentials: Get the necessary access tokens and IDs from your Meta for Developers dashboard to connect your business number to n8n.
PostgreSQL Database
- A running PostgreSQL instance: This can be hosted anywhere (e.g., AWS, DigitalOcean, Supabase). The AI agent needs it to store and retrieve conversation history.
- Database Credentials: You'll need the host, port, user, password, and database name to connect n8n to it.
Node-by-Node Explanation
The workflow is divided into two distinct logical flows.
Flow 1: New Client Intake from JotForm
This part triggers when a new client submits your form.
-
JotForm Trigger
- What it does: This is the starting point. It automatically runs the workflow whenever a new submission is received for the specified JotForm (
Form ID: 252801824783057
).
- Prerequisites: A JotForm account and a created form.
-
Append or update row in sheet
(Google Sheets)
- What it does: It takes the data from the JotForm submission and adds it to your "Law Client Enquiries" Google Sheet.
- How it works: It uses the
appendOrUpdate
operation. It tries to find a row where the "Email Address" column matches the email from the form. If it finds a match, it updates that row; otherwise, it appends a new row at the bottom.
- Prerequisites: A Google Sheet with the correct headers, shared with your service account.
-
AI Agent
- What it does: This node crafts the initial welcome message to be sent to the client.
- How it works: It uses a detailed prompt that defines a persona ("Alex," a legal intake assistant) and instructs the AI to generate a professional WhatsApp message. It dynamically inserts the client's name and service of interest from the Google Sheet data into the prompt.
- Connected Node: It's powered by the
Google Gemini Chat Model
.
-
Send message
(WhatsApp)
- What it does: It sends the message generated by the
AI Agent
to the client.
- How it works: It takes the client's phone number from the data (
Phone Number
column) and the AI-generated text (output
from the AI Agent node) to send the message via the WhatsApp Business API.
- Prerequisites: A configured WhatsApp Business Platform account.
Flow 2: AI-Powered Scheduling via WhatsApp
This part triggers when the client replies to the initial message.
-
WhatsApp Trigger
- What it does: This node listens for incoming messages on your business's WhatsApp number. When a client replies, it starts this part of the workflow.
- Prerequisites: A configured WhatsApp Business Platform account.
-
If
node
- What it does: It acts as a simple filter. It checks if the incoming message text is empty. If it is (e.g., a status update), the workflow stops. If it contains text, it proceeds to the AI agent.
-
AI Agent1
- What it does: This is the main conversational brain for scheduling. It handles the back-and-forth chat with the client.
- How it works: Its prompt is highly detailed, instructing it to act as "Alex" and follow a strict procedure for scheduling. It has access to several "tools" to perform actions.
- Connected Nodes:
Google Gemini Chat Model1
: The language model that does the thinking.
Postgres Chat Memory
: Remembers the conversation history with a specific user (keyed by their WhatsApp ID), so the user doesn't have to repeat themselves.
- Tools:
Know about the user enquiry
, GET MANY EVENTS...
, and Create an event
.
-
AI Agent Tools (What the AI can do)
Know about the user enquiry
(Google Sheets Tool): When the AI needs to know who it's talking to, it uses this tool. It takes the user's phone number and looks up their original enquiry details in the "Law Client Enquiries" sheet.
GET MANY EVENTS...
(Google Calendar Tool): When a client suggests a date, the AI uses this tool to check your Google Calendar for any existing events on that day to see if you're free.
Create an event
(Google Calendar Tool): Once a time is agreed upon, the AI uses this tool to create the event in your Google Calendar, adding the client as an attendee.
-
Send message1
(WhatsApp)
- What it does: Sends the AI's response back to the client. This could be a confirmation that the meeting is booked, a question asking for their email, or a suggestion for a different time if the requested slot is busy.
- How it works: It sends the
output
text from AI Agent1
to the client's WhatsApp ID, continuing the conversation.