Great! Let’s now expand your AI system into a full Lead Follow-Up Sequence Bot with memory—a smart, persistent agent that nurtures leads over time and moves them toward conversion.


🔁 Lesson: Creating a Lead Follow-Up Sequence Bot (with Memory)

Module 4.4 Extension: Lead Generation & CRM Integration


🎯 Lesson Objective

By the end of this lesson, you’ll be able to:

  • Build an AI agent that remembers lead history and adapts follow-ups

  • Design automated, personalized outreach sequences

  • Use memory to store conversation history or stages

  • Trigger actions like emails, reminders, or CRM updates based on status


🧠 Why Use a Follow-Up Bot with Memory?

Most leads require 5–8 touchpoints before converting.

A memory-enabled agent can:

  • 🧠 Track lead stage (e.g., “Interested”, “Waiting for demo”, “Followed up 2x”)

  • 📤 Send personalized follow-ups based on past responses

  • 🧾 Store previous messages, questions, and objections

  • 📅 Schedule timely nudges and respond conversationally over days/weeks


🔧 Tools You’ll Use

Tool Purpose
LangChain + ConversationBufferMemory Store past interactions
LLM (Ollama / OpenAI) Write follow-ups
CRM or Google Sheets Track status
SMTP or external API Send emails
Scheduler (e.g., cron) Trigger automated follow-ups

🔁 Step-by-Step: Build the Follow-Up Sequence Bot


✅ 1. Initialize the Agent with Memory

from langchain.agents import initialize_agent, AgentType
from langchain.llms import Ollama
from langchain.memory import ConversationBufferMemory

llm = Ollama(model="mistral")
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)

agent = initialize_agent(
    llm=llm,
    tools=[],  # Add tools later if needed
    agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
    memory=memory,
    verbose=True
)

✅ 2. Define the Follow-Up Stages

You can model the lead journey:

Stage Trigger Action
New Lead Initial inquiry Welcome + info email
Follow-Up 1 No reply in 2 days Gentle nudge + benefits
Follow-Up 2 No reply in 5 days Offer call/demo
Cold No response after 7 days Thank you + close lead

Use a simple tracker in memory or database:

lead_status = {
    "name": "Sarah Green",
    "stage": "followup_1",
    "last_contact": "2025-06-28",
    "history": []
}

✅ 3. Create Dynamic Follow-Up Prompts

from langchain.prompts import PromptTemplate

template = """
You are a follow-up bot for B2B leads. Use the previous interaction history and current stage to write a short, professional message.

Lead name: {name}
Current stage: {stage}
Conversation history:
{chat_history}

Generate a polite follow-up message that shows value and asks for next steps.
"""

followup_prompt = PromptTemplate(
    input_variables=["name", "stage", "chat_history"],
    template=template
)

# Generate message
msg = agent.run(followup_prompt.format(
    name=lead_status["name"],
    stage=lead_status["stage"],
    chat_history="n".join(lead_status["history"])
))

✅ 4. Store Conversation and Update Stage

lead_status["history"].append(msg)
lead_status["stage"] = "followup_2"  # or based on logic

✅ 5. Send the Follow-Up Email

Same as before:

import smtplib
from email.mime.text import MIMEText

message = MIMEText(msg)
message["Subject"] = "Following up – Ready to chat?"
message["From"] = "[email protected]"
message["To"] = "[email protected]"

with smtplib.SMTP("smtp.gmail.com", 587) as server:
    server.starttls()
    server.login("[email protected]", "your_password")
    server.sendmail(message["From"], message["To"], message.as_string())

🔁 Optional: Schedule with Python or Cron

import schedule
import time

def run_followup_cycle():
    # Load all leads
    # Check stage + last_contact
    # Trigger next follow-up with AI
    pass

schedule.every().day.at("10:00").do(run_followup_cycle)

while True:
    schedule.run_pending()
    time.sleep(1)

🧠 Sample Output Messages

Follow-Up 1

“Hi Sarah, just checking in to see if you had a chance to review the demo I mentioned. Let me know if you’d like to schedule a quick walkthrough!”

Follow-Up 2

“Hi again Sarah – we’d love to support GreenTech’s goals with our solution. Would this week work to connect?”

Final Nudge

“Thanks again for your time. If it’s not a fit right now, no problem—feel free to reach out in the future!”


🧠 Ideas for Smarter Follow-Ups

Add-On Description
📥 Inbound triggers Auto-react if lead replies
🔁 Lead scoring Prioritize hot leads with custom rules
🧾 CRM update Change lifecycle stage after response
🌐 Multi-channel Use SMS, WhatsApp, or LinkedIn messaging
🔐 Authenticated dashboard Show follow-up history in a UI

✅ Summary

Step What You Built
🤖 AI follow-up agent Writes messages for each stage
🧠 Memory enabled Tracks chat history + lead stage
✉️ Sends messages Automatically via email
🔁 Scheduled logic Automates nurturing over time

🔜 Next: Want to Level Up?

Would you like to:

  1. 🗃️ Store all lead sequences in a local SQLite or Firebase DB?

  2. 🌍 Make this agent live on a website or CRM (frontend integration)?

  3. 🎯 Add lead scoring logic (based on keywords, replies, or budget)?

Let me know and I’ll help you expand!

53