跳到正文
DEV Community· Muhammad Mohsin·· 3 小時前AI 評分62

作者以 n8n 搭建自行託管的 WhatsApp 訂單機械人,免付機械人平台月費

I Built a WhatsApp Order Bot with n8n — No Monthly Fees, It Runs on Your Own Server

AI 導讀

Muhammad Mohsin 分享以 n8n、自架伺服器及 Meta 官方 WhatsApp Cloud API 搭建 WhatsApp 訂單機械人,並用 Google Sheets 記錄訂單。

正文

I Built a WhatsApp Order Bot with n8n

A small shop owner asked me a fair question: "Why does a WhatsApp bot cost $50 a month?"

He wasn't wrong to ask. Tools like ManyChat and WATI are good products, but for a business that mostly needs one thing — take orders and answer the same ten questions on WhatsApp — the pricing is hard to defend. Entry plans start around $15–25/month, the useful tiers climb past $50, and the bill arrives whether you sold anything that month or not. The bot is rented. Stop paying, and your order line goes silent.

So I built the version I could hand to someone like him: a WhatsApp order bot that runs on n8n, self-hosted, using Meta's official WhatsApp Cloud API. No per-message platform markup from me, no monthly fee to a bot vendor, and the whole thing lives on a server the shop owns. I later packaged my version as a kit, but everything below is the actual build — including the parts that broke.

What we're building (and what we're not)

The bot does three jobs:

  1. Takes orders. A customer messages the number, the bot walks them through choosing items and quantities, confirms the total, and saves the order.
  2. Answers FAQs. Prices, delivery areas, opening hours, payment methods — the questions every shop answers forty times a day.
  3. Logs everything to Google Sheets. Every order lands as a row the owner can read, filter, and reconcile at night. No dashboard to learn.

What it does not do: it doesn't send bulk marketing blasts (Meta's rules, not mine — more on that below), it doesn't replace a human for complaints, and it doesn't pretend to be one. When a conversation goes off-script, the honest move is to hand it to the owner with a notification, not let an AI improvise.

The architecture, in plain words

Four pieces, each boring on its own:

  • WhatsApp Cloud API — Meta's official API. This is how real businesses are supposed to connect; the test number is free, and a real number runs on Meta's pricing, not a reseller's.
  • n8n (self-hosted) — the brain. One workflow receives messages, decides what they are, and sends replies. It runs on your own machine or a small VPS you control.
  • Google Sheets — the order book. Unsophisticated, and that's the point: the owner already knows how to read a spreadsheet.
  • One AI node — used narrowly, to draft replies for free-text questions the FAQ list doesn't cover. It drafts; the workflow decides whether to send.

Before you start

You need a Meta developer account, a phone number that is not currently registered on WhatsApp (a fresh SIM is easiest), an n8n instance reachable from the internet (webhooks need a public URL — n8n's tunnel works for testing, a domain is better for real use), and a Google account for the Sheet. That's the whole shopping list.

Step by step

1. Create the Meta app. In the Meta developer dashboard, create an app, add the WhatsApp product, and you'll get a test number plus a temporary access token. Send yourself a test message from the dashboard first — if that doesn't arrive, nothing downstream matters yet.

2. Connect your real number. Register the shop's number to the app. This is the point of no return for that number: it becomes a WhatsApp Business API number, so don't use your personal one. Generate a permanent access token with a system user — the temporary token expires in 24 hours, and a bot that dies daily is a special kind of embarrassment. (Ask me how I know a token can expire at the worst possible moment.)

3. Point the webhook at n8n. In n8n, start the workflow with a Webhook node set to receive POST requests, and paste its production URL into the Meta app's webhook settings, subscribed to the messages field.

4. Survive the verification handshake. This is where my own kit embarrassed me. When you save the webhook, Meta immediately sends a GET request with a challenge and expects your endpoint to echo it back — but only if the verify token matches the one you set. My shipped workflow failed this out of the box: the verification path wasn't handled, so Meta refused the webhook and the bot looked "connected" while receiving nothing. The fix is a small branch in the workflow: if the incoming request is a GET carrying hub.mode=subscribe, compare hub.verify_token against your token and return hub.challenge as plain text. Ten minutes of work, half a day to diagnose the first time. Test it before you build anything else on top.

5. Walk the workflow. A message arrives at the webhook as JSON. The flow I use:

  • An IF node separates real messages from status callbacks (delivered/read receipts arrive at the same webhook and will pollute your Sheet if you let them).
  • A CONFIG node (a plain Set node) holds every setting — more on this below.
  • Another IF routes by intent: a known FAQ keyword goes to a fixed reply; anything that looks like an order goes to the order flow, which asks for missing details one question at a time.
  • Orders get appended to the Sheet with timestamp, number, items, and total.
  • Replies go out through an HTTP Request node to the Graph API's /messages endpoint.

6. Send a test order. Message your own bot like a customer: ask about delivery, place an order, watch the row appear in the Sheet. Then break it on purpose — send a voice note, an image, a question it can't answer — and watch what it does. The failure cases are the product.

The CONFIG pattern

The single decision that made this maintainable: every token, number, and text string lives in one node named CONFIG, and nothing anywhere else in the workflow hard-codes a value.

{
  "phone_number_id": "YOUR_PHONE_NUMBER_ID",
  "sheet_id": "YOUR_GOOGLE_SHEET_ID",
  "store_name": "The Corner Store",
  "delivery_fee": 150,
  "ai_timeout_seconds": 300,
  "fallback_reply": "Thanks! The owner will reply shortly."
}

Every other node reads from it with expressions like:

{{ $('CONFIG').first().json.store_name }}

When the owner changes the delivery fee, there is exactly one place to edit. When a client deployment needs a different Sheet, one place. I learned this the scattered way first: hunting the same phone number ID through nine nodes is a rite of passage I don't recommend.

The 90-second lesson

Second scar, briefly: my AI node shipped with a hard-coded 90-second timeout. On a good day, replies drafted in seconds. On a slow day — a long question, a loaded model — the node hit 90 seconds and the whole run died silently. The customer got nothing, the Sheet got nothing. Timeouts on anything network-shaped should be configuration, not constants: mine now lives in CONFIG (see ai_timeout_seconds above) and defaults to a patient 300. If a setting can ruin a run at 2 a.m., it doesn't belong buried inside a node.

Honest limits

  • The 24-hour window. You can only send free-form replies within 24 hours of the customer's last message. Outside that window, Meta requires pre-approved template messages. Your order confirmations are fine (customers write first), but "we miss you" campaigns are a different, more regulated game.
  • Meta can say no. Numbers get quality-rated; spammy behavior gets them restricted. This bot answers people who message it — the safest possible posture — but no tool exempts you from the platform's rules.
  • It needs a home. Self-hosted means you host it. A small VPS or an always-on machine at the shop; if it sleeps, the bot sleeps.

What it actually costs

  • WhatsApp Cloud API: free to start; Meta charges per conversation beyond the free tier, priced by country — for a small shop, typically a few dollars a month, paid to Meta directly.
  • n8n: free, self-hosted.
  • Hosting: $0 on a machine you already own, or roughly $5/month for a small VPS.
  • Google Sheets: free.

Compare that with $50+/month for the rented version, forever. The trade is real, though: you own the maintenance too. That's the honest exchange — money, or responsibility — and different shops should choose differently.

If you'd rather not build it

I packaged my working version — the same workflow above, with the webhook verification fixed and the timeout configurable — as WhatsAppKit on Gumroad, setup guide included: onyxailabs.gumroad.com/l/whatsappkit. I also set it up for clients through my Fiverr gig if you'd rather hand the whole thing over. But genuinely: everything essential is in this article. A weekend and a fresh SIM will get you most of the way.

One question for the comments, from the build above: where do you stand on AI drafting replies for a small shop — helpful, or should a bot only ever send answers a human wrote word for word? I went with "AI drafts, rules decide," but I go back and forth on it.

來源:DEV Community · dev.to