{"slug": "building-a-whatsapp-ai-agent-with-gemini-using-gemini-as-your-copilot", "title": "Building a WhatsApp AI Agent with Gemini Using Gemini as Your Copilot", "summary": "A developer built a WhatsApp AI agent powered by Google Gemini, using Gemini itself as a coding copilot to scaffold, debug, and extend the code. The agent receives WhatsApp messages via Meta's Cloud API, processes them through the Gemini API, and sends back replies. The project demonstrates using Gemini both as the runtime brain and as an assistant during development.", "body_md": "**What you'll build:** A WhatsApp number that replies with an AI agent powered by Google Gemini. You'll use **Gemini** (via the **Gemini CLI** or **Gemini Code Assist** in your IDE) as your pair-programming copilot to scaffold, debug, and extend the code, so Gemini is both the agent's runtime brain *and* the assistant building it.\n\n```\nWhatsApp user\n     │  message\n     ▼\nMeta WhatsApp Cloud API  ──webhook POST──►  Your server (Flask)\n     ▲                                            │\n     │  send reply (Graph API)                    ▼\n     └──────────────────────────────────  Gemini API (the \"brain\")\n```\n\nTwo moving parts:\n\n**Two hats for Gemini:** at *runtime*, the Gemini API generates replies to WhatsApp users. While *building*, you use Gemini as your coding copilot — the **Gemini CLI** (`npm install -g @google/gemini-cli`\n\n, then run `gemini`\n\n) in your terminal, or **Gemini Code Assist** inside VS Code / JetBrains. You describe what you want in plain English, and it writes, explains, and fixes the code below.\n\n`npm install -g @google/gemini-cli`\n\n) or\n\nAsk Gemini:\"Explain what a Gemini API key can access and how to store it safely as an environment variable.\"\n\nAsk Gemini:\"Walk me through creating a permanent WhatsApp access token with a System User in Meta Business Suite.\"\n\n```\nmkdir whatsapp-gemini-agent && cd whatsapp-gemini-agent\npython -m venv .venv && source .venv/bin/activate\npip install flask google-genai requests python-dotenv\n```\n\nCreate a `.env`\n\nfile:\n\n```\nGEMINI_API_KEY=your_gemini_key\nWHATSAPP_TOKEN=your_whatsapp_access_token\nWHATSAPP_PHONE_NUMBER_ID=your_phone_number_id\nVERIFY_TOKEN=pick_any_random_string\n```\n\nAsk Gemini:\"Generate a .gitignore for a Python project and make sure .env is excluded.\"\n\nMeta requires two things from your endpoint:\n\n`/webhook`\n\n— a one-time verification handshake.`/webhook`\n\n— where inbound messages arrive.Create `app.py`\n\n:\n\n``` python\nimport os\nimport requests\nfrom flask import Flask, request\nfrom google import genai\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\nGEMINI_API_KEY = os.environ[\"GEMINI_API_KEY\"]\nWHATSAPP_TOKEN = os.environ[\"WHATSAPP_TOKEN\"]\nPHONE_NUMBER_ID = os.environ[\"WHATSAPP_PHONE_NUMBER_ID\"]\nVERIFY_TOKEN = os.environ[\"VERIFY_TOKEN\"]\n\napp = Flask(__name__)\nclient = genai.Client(api_key=GEMINI_API_KEY)\n\nSYSTEM_PROMPT = (\n    \"You are a friendly, concise WhatsApp assistant. \"\n    \"Keep replies short and clear — this is a chat app, not email.\"\n)\n\ndef ask_gemini(user_text: str) -> str:\n    response = client.models.generate_content(\n        model=\"gemini-2.5-flash\",\n        contents=user_text,\n        config=genai.types.GenerateContentConfig(system_instruction=SYSTEM_PROMPT),\n    )\n    return response.text\n\ndef send_whatsapp_message(to: str, body: str) -> None:\n    url = f\"https://graph.facebook.com/v22.0/{PHONE_NUMBER_ID}/messages\"\n    headers = {\"Authorization\": f\"Bearer {WHATSAPP_TOKEN}\"}\n    payload = {\n        \"messaging_product\": \"whatsapp\",\n        \"to\": to,\n        \"type\": \"text\",\n        \"text\": {\"body\": body},\n    }\n    requests.post(url, headers=headers, json=payload, timeout=20)\n\n@app.get(\"/webhook\")\ndef verify():\n    # Meta's verification handshake\n    if (request.args.get(\"hub.mode\") == \"subscribe\"\n            and request.args.get(\"hub.verify_token\") == VERIFY_TOKEN):\n        return request.args.get(\"hub.challenge\"), 200\n    return \"Forbidden\", 403\n\n@app.post(\"/webhook\")\ndef incoming():\n    data = request.get_json()\n    try:\n        change = data[\"entry\"][0][\"changes\"][0][\"value\"]\n        message = change[\"messages\"][0]          # inbound message\n        sender = message[\"from\"]                  # user's phone number\n        text = message[\"text\"][\"body\"]            # message text\n\n        reply = ask_gemini(text)\n        send_whatsapp_message(sender, reply)\n    except (KeyError, IndexError):\n        # Status updates and non-text messages land here — ignore them\n        pass\n    return \"OK\", 200\n\nif __name__ == \"__main__\":\n    app.run(port=5000)\n```\n\nAsk Gemini:\"Paste this file and explain each function line by line, then suggest error handling I'm missing.\"\n\nRun the server, then tunnel it:\n\n```\npython app.py            # terminal 1\nngrok http 5000          # terminal 2  → copy the https URL\n```\n\nIn Meta's WhatsApp → **Configuration**:\n\n`https://<your-ngrok-id>.ngrok.io/webhook`\n\n`VERIFY_TOKEN`\n\nfrom your `.env`\n\nAsk Gemini:\"My webhook verification is returning 403. Here's my code and the ngrok logs — what's wrong?\"\n\nSend a WhatsApp message from your registered number to the test number. Within a second or two you should get a Gemini-generated reply.\n\nIf nothing comes back, ask Claude to help you read the logs:\n\nAsk Gemini:\"The webhook receives a POST but no reply is sent. Here's the JSON payload and my server log — trace where it breaks.\"\n\nA chatbot answers. An **agent** takes actions. Gemini supports **function calling** — you declare tools, and the model decides when to call them.\n\nExample: give the agent a `check_reservation`\n\ntool.\n\n``` php\ndef check_reservation(confirmation_code: str) -> dict:\n    # Replace with a real lookup (DB, internal API, etc.)\n    return {\"code\": confirmation_code, \"status\": \"confirmed\", \"checkin\": \"2026-08-14\"}\n\ndef ask_gemini_agent(user_text: str) -> str:\n    response = client.models.generate_content(\n        model=\"gemini-2.5-flash\",\n        contents=user_text,\n        config=genai.types.GenerateContentConfig(\n            system_instruction=SYSTEM_PROMPT,\n            tools=[check_reservation],   # SDK auto-generates the schema from the function\n        ),\n    )\n    return response.text\n```\n\nThe Gemini SDK reads the function's signature and docstring to build the tool schema, calls it when the user asks about a reservation, and folds the result into its reply.\n\nAsk Gemini:\"Add a second tool that cancels a reservation, and add conversation memory so the agent remembers earlier messages in the same chat.\"\n\nGood next tools to build with Claude's help:\n\n`contents`\n\n.`X-Hub-Signature-256`\n\nheader`200`\n\nfast and process Gemini calls in a\n\nAsk Gemini:\"Write the`X-Hub-Signature-256`\n\nverification middleware for my Flask app, and refactor the Gemini call to run in a background thread so the webhook returns 200 immediately.\"\n\n`gemini \"review app.py for security issues\"`\n\n).| Piece | Service | Docs |\n|---|---|---|\n| Inbound + outbound messages | WhatsApp Cloud API | developers.facebook.com/docs/whatsapp/cloud-api |\n| Agent reasoning + tools | Gemini API | ai.google.dev/gemini-api/docs |\n| Your coding copilot | Gemini CLI / Code Assist | github.com/google-gemini/gemini-cli · codeassist.google |\n\n*Model note: gemini-2.5-flash is fast and cheap for chat; switch to gemini-2.5-pro for harder reasoning. Check Google's model list for the latest IDs.*", "url": "https://wpnews.pro/news/building-a-whatsapp-ai-agent-with-gemini-using-gemini-as-your-copilot", "canonical_source": "https://dev.to/devpato/-building-a-whatsapp-ai-agent-with-gemini-using-gemini-as-your-copilot-41", "published_at": "2026-07-29 17:19:11+00:00", "updated_at": "2026-07-29 17:33:00.948971+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-agents", "developer-tools"], "entities": ["Google Gemini", "Meta", "WhatsApp", "Gemini CLI", "Gemini Code Assist", "Flask"], "alternates": {"html": "https://wpnews.pro/news/building-a-whatsapp-ai-agent-with-gemini-using-gemini-as-your-copilot", "markdown": "https://wpnews.pro/news/building-a-whatsapp-ai-agent-with-gemini-using-gemini-as-your-copilot.md", "text": "https://wpnews.pro/news/building-a-whatsapp-ai-agent-with-gemini-using-gemini-as-your-copilot.txt", "jsonld": "https://wpnews.pro/news/building-a-whatsapp-ai-agent-with-gemini-using-gemini-as-your-copilot.jsonld"}}