See llms.txt for all machine-readable content.

Back to Templates

Build a Facebook Messenger customer service AI chatbot with Google Gemini

๐Ÿค– Build a customer service AI chatbot for Facebook Messenger with Google Gemini

๐Ÿ“Œ Overview

A streamlined Facebook Messenger chatbot powered by AI with conversation memory.
This is a simplified version designed for quick deployment, learning, and testing โ€” not suitable for production environments.

Base workflows:

๐ŸŽฏ What This Workflow Does

โœ… Core Features:

  • Receives messages from Facebook Messenger via webhook
  • Processes user messages with Google Gemini AI
  • Maintains conversation context using Simple Memory node
  • Automatically responds with AI-generated replies
  • Handles webhook verification for Facebook setup
  • Send image or video to customer through Facebook Messenger

๐Ÿ”น Simplified Approach:

  • Memory: Simple Memory node (10-message window)
  • Format: Cleans text, strips markdown, truncates >1900 chars
  • Response: Single message delivery

โš ๏ธ Limitations & Trade-offs:

  1. No Smart Batching โ†’ fragmented user messages cause spam-like replies
  2. No Human Takeover Detection โ†’ bot continues even when admin joins
  3. Basic Memory Management โ†’ no persistence, not reliable in production
  4. Basic Text Formatting โ†’ strips markdown, truncates brutally, no smart splitting

๐Ÿš€ When to Upgrade

Upgrade to full workflows when you need:

  • Production deployment with reliability & persistence
  • Analytics & tracking (query history, reports)
  • Professional formatting (bold, italic, lists, code blocks)
  • Handling long messages (>2000 chars)
  • Smart batching for fragmented inputs
  • Human handoff detection
  • Full conversation persistence

Key upgrades available:

โš™๏ธ Setup Requirements

Facebook Setup

  1. Create Facebook App at developers.facebook.com
  2. Add Messenger product
  3. Configure webhook:
    • URL: https://your-domain.com/webhook/your-path
    • Verify token: secure string
    • Subscribe to: messages, messaging_postbacks
  4. Generate Page Access Token
  5. Copy token to "Set Context" node

n8n Setup

  1. Import workflow
  2. Edit "Set Context" node โ†’ update page_access_token
  3. Configure "Gemini Flash" node credentials
  4. Deploy workflow (must be publicly accessible)

๐Ÿ”„ How It Works

User Message โ†’ Facebook Webhook โ†’ Validation โ†“ Set Context (extract user_id, message, token) โ†“ Mark Seen โ†’ Show Typing โ†“ AI Agent (Gemini + 10-message memory) โ†“ Format Output (remove markdown, truncate) โ†“ Send Response via Facebook API

๐Ÿ—๏ธ Architecture Overview

Section 1: Webhook & Initial Processing

  • Facebook Webhook: handles GET (verification) & POST (messages)
  • Confirm Webhook: returns challenge / acknowledges receipt
  • Filters text messages only
  • Blocks echo messages from bot itself

Section 2: AI Processing with Memory

  • Set Context: extracts user_id, message, token
  • Seen & Typing: user feedback
  • Conversation Memory: 10-message window, per-user isolation
  • Process Merged Message: AI Agent with Jenix persona
  • Gemini Flash: Googleโ€™s AI model for response generation

Section 3: Format & Delivery

  • Cuts replies >2000 chars, strips markdown
  • Sends text via Facebook Graph API

๐ŸŽจ Customisation Guide

  • Bot Personality: edit system prompt in "Process Merged Message" node
  • Memory: adjust contextWindowLength (default 10), change sessionKey if needed
  • AI Model: replace Gemini Flash with OpenAI, Anthropic Claude, or other LLMs

๐Ÿ“Œ Important Notes

โš ๏ธ Production Warning: testing only, memory lost on n8n restart in queue mode
๐Ÿ“Š No Analytics: no history storage, no reporting
๐Ÿ”ง Format Limitations: responses โ‰ค1800 chars, markdown stripped, no complex formatting

๐Ÿ› ๏ธ Troubleshooting

  • Bot not responding โ†’ check token, webhook accessibility, event subscriptions
  • Memory not working โ†’ verify session key, ensure not in queue mode, restart workflow
  • Messages truncated โ†’ adjust system prompt for conciseness, reduce response length

๐Ÿ“œ License & Credits

Created by: Nguyแป…n Thiแป‡u Toร n (Jay Nguyen)