It’s Showtime, folks! — All That Jazz (1979)

Skip to content

Automation6 min read

How I Built an AI Chatbot for Messenger and Instagram with n8n

An AI chatbot can answer Messenger and Instagram inquiries at any hour, book consultations and log every lead. Here is how to build one with n8n and OpenRouter, and what to watch.

By · Published

Part of Automation

At the end of this guide you will have an AI chatbot for Facebook Messenger and Instagram that answers inquiries, books consultations and logs leads automatically. It runs on n8n for the workflow, the Meta messaging platform for the two channels, and OpenRouter for the AI model.

I built a chatbot like this at VITE SEO Digital Advertising, the agency I founded and have since closed, and this guide is based on that build. The steps below follow the order I built it in. It is part of my Automation writing. I built it because inquiries from our ads often came in at night and on weekends, and I wanted every person to get a reply right away instead of the next morning.

What do you need before you start?

  • A Facebook Page, and an Instagram professional account connected to it.
  • A Meta developer account and an app with messaging access. The app needs the pages_messaging permission for Messenger and instagram_manage_messages for Instagram. Standard Access works for Pages and accounts you own or manage. To serve accounts you don’t manage, Meta requires Advanced Access, which needs App Review and Business Verification.
  • An n8n instance, either n8n Cloud or self-hosted, reachable over HTTPS so Meta can call its webhook.
  • An OpenRouter account and API key, with credit for the model you pick.
  • A place to log leads, such as a Google Sheet or a database table.
  • A booking link, such as a calendar scheduling page.
  • A written list of what the bot should know: services, prices, hours, FAQs and when to hand over to a person.

How do you build it, step by step?

  1. Create a Meta app. In Meta for Developers, create a business app, add the Messenger product, and connect your Facebook Page. Then open Messenger’s Instagram settings and connect the Instagram professional account linked to that Page. Meta renames menus from time to time, so follow the current Messenger Platform docs if a label differs.
  2. Connect the webhook to n8n. Add a Webhook node in n8n, copy its production URL into the Meta app’s webhook settings, and set a verify token. Add a branch that answers Meta’s verification request so the webhook is accepted.
  3. Parse the incoming message. Pull out the sender ID, the channel (Messenger or Instagram) and the message text. Ignore echoes of your own replies so the bot does not answer itself.
  4. Add the AI reply node through OpenRouter. Use an HTTP Request node or an AI model node pointed at OpenRouter, pass the system prompt and the recent messages, and read back the reply.
  5. Write the system prompt. Tell the model who it speaks for, what it knows, the tone to use, what it must not answer, and the exact phrase or flag to return when a human should take over.
  6. Add booking and logging branches. When the person wants a consultation, send the booking link. For every conversation, write the sender, channel, message and outcome to your lead log.
  7. Send the reply. Call the Send API with a POST to https://graph.facebook.com/v25.0/{PAGE-ID}/messages, using the Page access token. The body needs recipient.id (the sender’s ID from the webhook), messaging_type: "RESPONSE" and message.text. You can reply freely within 24 hours of the person’s last message. After that, Meta only allows specific message types.
  8. Test. Message the Page and the Instagram account from a test account. Try normal questions, off-topic questions, a request for a human and a message in another language.
  9. Go live. Switch the Meta app to live mode, complete any app review Meta requires, and watch the first real conversations closely.

Here is my workflow node by node, in the order a message passes through it:

  1. Webhook: receives every request Meta sends.
  2. IF (verification): if it is Meta’s verification request, a Respond to Webhook node returns the challenge and the run stops.
  3. Respond to Webhook: sends Meta a quick 200 OK so it does not resend the message.
  4. Code: pulls out the sender ID, channel and message text, and drops echoes of the bot’s own replies.
  5. Google Sheets (read): loads the last few messages from this sender for context.
  6. HTTP Request (OpenRouter): sends the system prompt and recent messages, and reads back the reply and any flag.
  7. Switch: routes on the flag: a normal answer, a booking request, or a handover.
  8. HTTP Request (Send API): sends the reply, with the booking link added when the person wants a call.
  9. Google Sheets (append): logs the sender, channel, message and outcome.
  10. Gmail: on a handover, emails a person with the conversation so far.

What does it cost to run?

ItemMonthly costNotes
n8n hostingAbout US$6Self-hosted on a small VPS (1 to 2 GB of memory)
AI tokens through OpenRouterAbout US$3 to US$5A low-cost model such as GPT-4o mini, around 6,000 to 10,000 tokens per conversation
Lead log and booking toolAbout US$0 extraGoogle Sheets and a Google Calendar booking page, already part of our Google Workspace
TotalAbout US$10Approximate figures for a typical month in early 2026

The biggest variable is usually tokens: how long the system prompt is, how much history you send with each message and how long the replies are.

What does it do well, and where does it fall short?

Handles wellFalls shortWhen a human must step in
Common questions about services, prices and hoursQuestions the system prompt does not coverThe person asks for a human
Replies at any hourMessages with little context, such as a single emojiComplaints or sensitive topics
Sending the booking link at the right momentLong back-and-forth negotiationsAnything involving payments, contracts or personal data
Logging every lead in one placeKnowing when information in the prompt is out of dateWhen the bot is unsure and says so

Our chatbot did best with the simple, repeated questions: what services we offered, rough package prices and how to book a call. It struggled with mixed Taglish messages and very short ones like “hm” or “how much?”, where it sometimes guessed which service the person meant. It also needed a prompt update every time our packages changed, so I learned to treat the system prompt like a price list someone has to keep current.

When should a person take over?

Build the handover in from the start. Have the model return a clear flag when it should not answer, then have n8n stop replying in that conversation and notify a person, for example by email or a chat message. Tell the customer that someone will follow up, and keep the logged conversation so the person does not have to ask everything again.

What should you check before going live?

  • The webhook answers Meta’s verification request.
  • The bot ignores its own messages.
  • The system prompt lists what the bot must not answer.
  • Handover to a person works and sends a notification.
  • Every conversation is logged with its channel and outcome.
  • You know your expected monthly token use and have set a spending limit where your provider allows it.
  • Your privacy notice covers storing messages and contact details.

Questions people ask

Do I need to code to build this chatbot?

Not much. n8n builds the workflow visually, but you will write a system prompt, set up a Meta app and webhook, and may write small expressions or a short code node to shape the data.

Can one workflow handle both Messenger and Instagram?

Yes, if the Instagram professional account is connected to your Facebook Page. In that setup, Instagram messages run through the Messenger Platform, so one Meta app and one webhook receive both channels, and the workflow branches on which channel a message came from. Instagram-only accounts use a separate Instagram Login setup instead.

When should a human take over from the chatbot?

When the person asks for a human, when the question is sensitive or outside what the bot knows, or when the conversation turns into a complaint. The workflow can flag these and notify a person.

Tools in this post

Share

Keep reading

More in Automation.

Get new posts by email

One email every two weeks with new posts, news takes and one personal note. No spam, unsubscribe any time.

Prefer a feed? Follow by RSS. About the newsletter.

Building something like this? Let's talk →