Workflow Code: Lead scoring and automated whatsapp proposal sender · GitHub
Here is a node-by-node explanation of the n8n workflow below:
1. Lead Scoring and Routing Pipeline
This flow ingests new leads, researches them, scores them against an Ideal Customer Profile (ICP), and routes them into your CRM.
- When New Lead Received (Webhook): Acts as the entry point. It listens for a
POSTrequest at the/newleadendpoint, expecting a JSON payload containing the lead’s basic info (name, email, phone, etc.). - Prepare Lead Data (Set): Extracts and normalizes variables from the incoming webhook (First Name, Last Name, Full Name, Email, Phone, Contact ID) to make them easily accessible for downstream nodes.
- Fetch Google AI Lead Overview (Apify): Triggers an Apify actor to perform a web search (including public directories and LinkedIn) using the lead’s name and email. It extracts their current job title, employer, and past experience to build a professional profile.
- Lead Processor Agent (AI Agent): This is the brain of the scoring process. It takes the enriched candidate profile from Apify and evaluates it against a detailed prompt defining your ICP (focusing on automation agencies, e-commerce, real estate, etc., with 10-100 employees).
- Gemini Chat Model: Provides the underlying LLM (Gemini 3.1 Flash Lite) powering the agent’s logic.
- Parse Structured Output: Forces the LLM to output a strict JSON schema containing a
lead_score(0-100),score_breakdown,recommendation, andnext_steps.
- Route by Lead Type (Switch): Evaluates the integer from the
lead_score.- Route 0 (High Priority): For scores >= 80.
- Route 1 (Medium Priority): For scores between 40 and 79.
- (Scores below 40 are routed to an empty Path 2, essentially discarding or halting the automated outreach).
High-Priority Path (Route 0 - B Nodes)
- Upsert Contact B (HighLevel): Updates or creates the GoHighLevel (GHL) contact, saving the AI-generated lead score, breakdown, and next steps into custom fields.
- Create Opportunity B (HighLevel): Generates a new pipeline opportunity in GHL named
[Full Name] | ai_scored_lead. - Send WhatsApp Template B (WhatsApp): Sends an initial outreach template (
hvac|en) to the lead via the WhatsApp Business API. - Update Opportunity B (HighLevel): Updates the GHL opportunity, likely moving it to a specific stage to indicate that initial contact has been initiated.
Medium-Priority Path (Route 1 - A Nodes)
- Upsert Contact A, Create Opportunity, Send WhatsApp Template A, Update Opportunity A: These nodes perform the exact same CRM and messaging sequence as the high-priority path, but run in parallel to allow for different pipeline stages or tags if you decide to customize the medium-priority routing later.
2. Customer Service AI Interaction Flow
This flow acts as an autonomous sales and support assistant that handles incoming WhatsApp messages, negotiates based on requirements, and uses tools to update the CRM.
- When WhatsApp Message Received (WhatsApp Trigger): Listens for incoming WhatsApp messages and triggers the workflow.
- If Valid Sender Exists (If): A simple validation node that checks if the incoming message payload actually contains a sender phone number before proceeding.
- Fetch GHL Contacts (HighLevel): Looks up the sender’s phone number in GoHighLevel to pull their existing contact record and CRM context.
- Get Opportunities (HighLevel): Fetches any open pipeline opportunities associated with that specific contact’s phone number.
- If Opportunity Exists (If): Validates that an active opportunity ID was found in the previous step so the AI agent knows it is dealing with a tracked lead.
- Customer Service Agent (AI Agent): A sophisticated conversational agent tasked with uncovering client needs, providing quotes (distinguishing between n8n and Python complexity), and handing the deal off to the team.
- Gemini Chat Model Service: The LLM (Gemini) driving the conversational intelligence.
- Get Redis Chat History (Memory): Uses Redis to store and retrieve the context window of the conversation, using the sender’s WhatsApp number as the session key. This allows the AI to “remember” previous messages in the thread.
- Agent Tools (What the AI can do):
- Save User Requirements: Saves a summary of the client’s needs into the notes field of their GHL profile.
- Update Stage to Proposal Sent: Moves the user’s GHL opportunity to a specific stage once a quote is generated.
- Send Telegram Message: Notifies your internal team via Telegram with a summary of the requirements and the estimated quote.
- Close Opportunity: A strictly constrained tool the AI is instructed to use only if the user rejects the budget or explicitly states they are no longer interested.
- Send WhatsApp Response (WhatsApp): Takes the final conversational output generated by the Customer Service Agent and sends it back to the user as a WhatsApp text message.