Sharing a workflow I built for handling Facebook Messenger customer service with an AI backend. Posting here in case it helps others who are trying to set up Messenger bots on n8n.
Template: Build a Facebook Messenger customer service AI chatbot with Google Gemini | n8n workflow template
How it works
The flow has three stages:
-
Webhook & validation - Receives both GET (verification handshake) and POST (messages) from Facebook. Filters out non-text events and blocks echo messages (the bot’s own replies coming back through the webhook - this trips up a lot of people).
-
AI processing - Extracts user_id, page_id, message text and token via Set Context. Sends “seen” + “typing” indicators immediately so users get feedback. Runs the message through Gemini Flash with a 10-message memory window per user.
-
Format & send - Strips markdown from the response (Messenger doesn’t render it), truncates at 1900 chars, sends via Graph API.
A few things I learned the hard way
- Echo filtering is essential. Facebook sends your bot’s own messages back to your webhook. Without filtering sender ID vs page ID, you get infinite loops.
- Simple Memory doesn’t persist in queue mode. Works fine for testing. For stable production you need Redis or Postgres-backed sessions.
- Gemini Flash is fast enough. With a tight system prompt, response times stay under 2 seconds - feels natural in Messenger.
- Token management is a separate problem. I built a dedicated workflow for long-lived page token refresh: Get long-lived Facebook Page access tokens and subscribe Messenger webhook fields via Graph API | n8n workflow template
What this template doesn’t cover (upgrade paths)
- Smart message batching (waits for user to finish typing before responding): Smart message batching AI-powered Facebook Messenger chatbot use Data Table | n8n workflow template
- Human takeover detection (bot auto-pauses when admin joins): Smart human takeover & auto pause AI-powered Facebook Messenger chatbot | n8n workflow template
Happy to answer questions about setup or any issues you’ve run into with the Facebook Graph API on n8n.