🚀 Build the 24/7 AI Lead Qualifier: The Pipecat Bot Runner Architecture

This project solves a core business problem: automating lead qualification for real estate wholesaling. Instead of a contact form, we build a scalable, real-time AI assistant that can talk to sellers, qualify them, and email you a clean lead report when the call ends.

We're going beyond simple Python scripts. We are building the Bot Runner Architecture, a structure used in production systems to separate the AI's logic from the server that controls it.

🔑 Get Your API Keys (Required)

To run this project, you need accounts and API keys from the following services:

Service

What it Does

Where to Get Key

OpenAI

Provides the GPT-4o-mini brain (LLM) for conversation and email summary.

https://platform.openai.com

Daily

Provides the WebRTC room URL and tokens for the video call.

https://dashboard.daily.co/developers

ElevenLabs

Provides the realistic Text-to-Speech (TTS) voice.

https://elevenlabs.io

Simli

Provides the real-time AI avatar and facial animation.

https://simli.com

SMTP (Email)

Required to send the final lead report to your inbox. (e.g., Gmail App Password or Mailgun/Sendgrid credentials).

[Your Email Provider]

Part 1: The Architecture Explained (3 Files, 1 Goal)

Our application is built on three independent files that work together to manage the call, the AI, and the network connection:

  1. The Frontend (index.html): The beautiful "We Buy Houses" website that the user sees. It contains the simple JavaScript that initiates the call.

  2. The "Boss" Server (bot_runner.py): The central orchestrator (built with FastAPI). It handles all the network security and logic.

  3. The "Worker" Bot (bot.py): The final AI agent (built with Pipecat). It only handles the conversation and the pipeline.

Part 2: Building the "Boss" Server (bot_runner.py)

The job of the Boss is to listen for the button click and securely set up the meeting.

1. Resource Provisioning (Room & Keys)

When a user clicks "Start Call," the Boss executes three critical asynchronous API calls to Daily.co using your DAILY_API_KEY:

  • Create Room: It generates a new, unique, temporary video call URL.

  • Create Bot Token: It generates an owner-level token (password) for the bot process.

  • Create User Token: It generates a standard participant token for the user.

2. Dynamic Spawning

The core concept of the "Bot Runner" is the use of subprocess.Popen.

  • After getting the keys, the Boss uses subprocess.Popen to instantly launch the bot.py script as a new process.

  • It passes the unique room URL and the bot's token as command-line arguments (e.g., --url <room_url> --token <bot_token>).

  • This approach ensures that each user gets their own dedicated AI agent, which prevents calls from interfering with each other and allows the main server to remain stable.

3. The Hand-off

The Boss then sends the room_url and the user's token back to the website, which allows the user to join the call the bot is already waiting in.

Part 3: The AI Worker Pipeline (bot.py)

The Worker bot contains the conversational intelligence and the post-call automation.

1. The Core Pipeline

The Pipecat framework creates a real-time data assembly line:

  • Input: The DailyTransport efficiently sends text transcription (STT) and audio from the user.

  • Intelligence: The OpenAILLMService (running on GPT-4o-mini) processes the transcription against the "We Buy Houses" system prompt to follow the 6-step qualification script.

  • Output: The LLM's text response is converted to speech by ElevenLabs (TTS), and that audio is fed directly into the SimliVideoService, which generates the lifelike, synchronized avatar video that streams back to the user.

2. Post-Call Automation (The Lead Report)

The most valuable feature is the automatic lead reporting:

  • When the user hangs up, the on_participant_left event fires.

  • The worker uses an AsyncOpenAI call to send the full, raw chat transcript back to the LLM.

  • The LLM's job is to act as a copywriter, summarizing the address, condition, price, and timeline into a clean, professional email body.

  • The script uses asyncio.to_thread to safely run the blocking smtplib code in the background, ensuring the server connection is closed gracefully while your email sends.