Quick overview
This workflow powers a Facebook Messenger chatbot that batches incoming user messages, answers with an OpenRouter chat model using RAG retrieval from a Supabase vector store, and keeps the knowledge base synced from PDFs in Google Drive (add, update, and delete).
How it works
- Receives Facebook Messenger webhook requests (including the initial webhook verification challenge) and ignores messages sent by the page itself.
- Extracts the sender, page, and message text, then stores each incoming message in an n8n Data Table keyed by the sender ID.
- Re-checks the sender’s pending messages until the latest message is at least 10 seconds old, then combines all pending messages into a single prompt.
- Sends the combined prompt to an AI agent backed by an OpenRouter chat model, using Supabase Vector Store retrieval as a tool and Postgres chat memory for conversation context.
- Posts the agent’s response back to the user through the Facebook Graph API.
- Triggers on new PDFs in a specified Google Drive folder, downloads and extracts text, chunks the content, generates OpenAI embeddings, and inserts the vectors into a Supabase
documents table.
- Triggers on updated PDFs in a specified Google Drive folder, deletes existing vectors matching the file name, then re-downloads, re-extracts, re-chunks, re-embeds, and re-indexes the updated content in Supabase.
- Triggers when a file appears in a Google Drive Trash folder, deletes matching vectors from Supabase, and then deletes the file from Google Drive.
Setup
- Create a Facebook Page and Meta app, configure the Messenger webhook to point to this workflow’s webhook URL, and add Facebook Graph API credentials with permission to send messages.
- Connect Google Drive OAuth and set the correct folder IDs for the “new file”, “updated file”, and “trash” triggers.
- Set up Supabase (with pgvector) and a
documents table, then add Supabase API credentials used for vector upserts and deletes.
- Add an OpenAI API key for embeddings and an OpenRouter API key for the chat model used by the AI agent.
- Provide a Postgres database for chat memory and ensure the n8n Data Table named
conversations exists for temporary message batching storage.