登入開始免費試用
Meta Tech Provider

n8n 中的 WhatsApp AI Agent,回覆由你的工作流程撰寫

自動化步驟會以已簽署的 POST,將客戶的訊息連同最多 10 則近期對話訊息,傳送至 n8n Webhook 節點。AI Agent 節點撰寫回覆,Respond to Webhook 節點於 20 秒內回傳 {"reply": "..."},該文字即由 WhatsApp Business 號碼傳送,而該號碼在 WhatsApp Business 應用程式中仍可正常使用。較慢的 AI Agent 則直接接收 Meta 的 webhook,並透過 Graph API 回覆。

easyCoexistence 透過 Coexistence,把你在應用程式中已使用的 WhatsApp Business 號碼連接到 Meta 的 Cloud API,並在其上運行自動化。

20 s

「Call my webhook」步驟等待你的工作流程回覆的時間,逾時後該步驟失敗。

10

以由舊到新的順序放入 history 的近期對話訊息,最後一則為觸發此次執行的訊息。

4,096

以單一 WhatsApp 文字訊息傳送的回覆字數上限,超出部分會被截斷。

6 hours

webhook 步驟失敗時,同一自動化兩封失敗通知電郵之間的最短間隔。

2 modules

n8n Cloud 在 Code 節點中允許的模組為 crypto 及 moment;自行架設的 n8n 則透過 NODE_FUNCTION_ALLOW_BUILTIN 啟用 crypto。

24 hours

客戶最後一則訊息之後的客服時限;時限過後,Meta 只接受範本訊息。

WhatsApp 訊息如何抵達 n8n AI Agent?

透過號碼上的自動化。該號碼必須透過 easyCoexistence 接收訊息(「Through easyCoexistence」模式),Meta 才會把訊息傳送至此處。觸發條件為「A message arrives」、步驟為「Call my webhook」的自動化,會把訊息以 POST 傳送至你的 n8n Production URL。工作流程在同一個 HTTP 回應中作答,其回傳的文字會以 WhatsApp 訊息,從你的號碼傳送給客戶。

在呼叫 n8n 之前,該次執行已通過各項防護檢查。Meta 重複傳送的同一則傳遞,不會再啟動第二次執行;你在應用程式中親自回覆的訊息,會令該對話的自動化暫停(預設 4 小時);而回覆只會在客戶最後一則訊息後 24 小時內發出。你的工作流程只負責決定說什麼。

1Customer writesTo the number you keep in the WhatsApp Business app
2Call my webhookSigned POST with the message and recent chat
3n8n AI AgentWebhook node, signature check, AI Agent node
4Respond to Webhook{"reply": "..."} within 20 seconds
5Reply sentAlso shown in the app's chat thread

easyCoexistence 會向 n8n Webhook 節點傳送什麼?

一個 JSON POST,User-Agent 為 easyCoexistence-automations/1,X-EC-Event 設為 automation.step,並帶有 X-EC-Signature 標頭。message 包含客戶的文字;若是媒體,則為佔位符,例如附有說明文字的 [image];語音訊息則以 [voice message] 傳來,若已儲存 OpenAI 或 OpenRouter 金鑰,後面會接上其內容文字。history 包含最多 10 則近期對話訊息,由舊到新排列,每則都附有 direction:客戶的訊息為 in,自動化的回覆為 out,人員從手機撰寫的訊息則為 echo。它涵蓋帳戶保留期間內保存的訊息(30、90 或 365 天,預設 90 天),而且僅限該號碼的訊息經由 easyCoexistence 傳入期間收到的訊息。

從編輯器的 Try it 面板進行測試,會傳送相同的 POST,但 dry_run 設為 true、history 為空、範例聯絡人為 15550000000,名稱為 Test customer,而訊息 ID 以 test_ 開頭。此測試不會向任何人發送訊息,但你的工作流程確實會執行,因此當 dry_run 為 true 時,請略過建立訂單等副作用。

POST body of a Call my webhook step
{
  "event": "automation.step",
  "dry_run": false,
  "automation": { "id": "b7e0c3a2-5d1f-4c8e-9a6b-2f4d8e1c7a90", "name": "Send to my system" },
  "run_id": "0d9f6a41-8c2e-4b7a-a315-6e9c2b8d4f17",
  "channel": {
    "type": "whatsapp",
    "connection_id": "3f2a9c1e-7b4d-4e8a-9c2f-1a2b3c4d5e6f",
    "phone": "+1 631-555-0142",
    "phone_number_id": "106540352242922"
  },
  "connection": { "id": "3f2a9c1e-7b4d-4e8a-9c2f-1a2b3c4d5e6f", "phone": "+1 631-555-0142", "phone_number_id": "106540352242922" },
  "contact": { "id": "5511987654321", "wa_id": "5511987654321", "name": "Ana Souza" },
  "message": {
    "id": "wamid.HBgNNTUxMTk4NzY1NDMyMRUCABIYFjNFQjBDNzE4RjQ2QjI5RDkxQTNFAA==",
    "type": "text",
    "text": "Do you have gluten-free bread today?",
    "at": "2026-11-11T14:02:11.000Z"
  },
  "history": [
    { "direction": "in", "text": "Hi! Are you open today?", "at": "2026-11-11T13:58:40.000Z" },
    { "direction": "out", "text": "Hi Ana! Yes, until 8 pm.", "at": "2026-11-11T13:58:45.000Z" },
    { "direction": "in", "text": "Do you have gluten-free bread today?", "at": "2026-11-11T14:02:11.000Z" }
  ]
}

如何在 n8n 中驗證簽名?

在 Webhook 節點之後緊接加入一個 Code 節點。X-EC-Signature 的格式為 t=<unix seconds>,v1=<hex>,其中 v1 是以自動化的 Signing secret 為金鑰,對 t、一個句點及內文計算出的 HMAC-SHA256。要複製該密鑰,請開啟已儲存的自動化並選取其「Call my webhook」步驟。每個自動化都有各自的密鑰,而 MCP 工具從不回傳密鑰,因此由 AI 建立的自動化,仍需從控制台複製一次此密鑰。「New secret」按鈕會取代該密鑰,舊密鑰隨即失效,而且只有帳戶擁有者看得到,受邀的團隊成員看不到。

拒絕與你的時鐘相差超過 5 分鐘的 t,以恆定時間方式比較,並在驗證失敗時拋出錯誤:如此工作流程會在 Respond to Webhook 之前出錯,n8n 回應 500,且不會發送任何內容。n8n Cloud 允許 Code 節點使用 crypto 模組;自行架設的 n8n 則需要設定 NODE_FUNCTION_ALLOW_BUILTIN=crypto。Crypto 節點的 Hmac 動作配合 IF 節點也可行,但 IF 比較並非恆定時間。

n8n Code node: verify X-EC-Signature
// n8n Code node, mode "Run Once for Each Item", placed right after the Webhook node.
// n8n Cloud allows require('crypto'); self-hosted needs NODE_FUNCTION_ALLOW_BUILTIN=crypto.
const crypto = require('crypto');
const SECRET = 'whsec_...'; // the automation's Signing secret, from its editor page

const m = /^t=(\d+),v1=([0-9a-f]{64})$/.exec($json.headers['x-ec-signature'] || '');
if (!m) throw new Error('Missing X-EC-Signature');
if (Math.abs(Date.now() / 1000 - Number(m[1])) > 300) throw new Error('Stale signature');

// The body was signed as compact JSON, which JSON.stringify rebuilds from the parsed body.
const mac = crypto.createHmac('sha256', SECRET).update(`${m[1]}.${JSON.stringify($json.body)}`).digest();
if (!crypto.timingSafeEqual(mac, Buffer.from(m[2], 'hex'))) throw new Error('Bad signature');

return { json: $json.body }; // event, dry_run, contact, message, history...

如何設定 AI Agent 節點及回覆?

將客戶的文字作為 AI Agent 節點的使用者訊息,其餘內容全部放入系統訊息:包括業務是什麼、可引用的資料、語氣,以及來自 history 的近期對話,好讓跟進問題也說得通。再接上你想用的聊天模型,並可加入工具,例如用來查詢訂單的 HTTP Request 工具。

最後加上一個 Respond to Webhook 節點,以 JSON 回應,並將 agent 的文字放入名為 reply 的欄位。easyCoexistence 會修剪該文字,忽略空白或非字串的回覆,並將其餘部分以最多 4,096 個字元的單一 WhatsApp 文字訊息傳送,因此請要求模型使用純文字句子,而非 markdown。

AI Agent and Respond to Webhook settings
AI Agent node
  Prompt: Define below
  Prompt (User Message): {{ $json.message.text }}
  Options > System Message:
    You answer WhatsApp messages for Bloom Café, 12 Baker St, open Tue to Sun 8:00 to 20:00.
    Reply in 1 to 3 short sentences, in the customer's language, as plain text.
    Never invent prices or stock. If unsure, say a person will reply here soon.
    Recent chat, oldest first:
    {{ $json.history.map(h => (h.direction === 'in' ? 'Customer: ' : 'Business: ') + h.text).join('\n') }}
  Chat Model: your OpenAI, Anthropic or OpenRouter model
  Tools (optional): an HTTP Request tool that looks up orders

Respond to Webhook node
  Respond With: JSON
  Response Body: { "reply": {{ $json.output.toJsonString() }} }

如果工作流程緩慢、失敗或沒有回傳任何內容,會怎樣?

每個結果都會記錄在自動化的 Runs 標籤中,並附上該步驟的輸出或錯誤,讓你看到發生了以下哪一種情況:

  • 20 秒內沒有回覆:該步驟失敗,顯示 Webhook did not answer,不會發送任何內容,執行狀態顯示 Failed。
  • 狀態碼不在 2xx 範圍內(包括重新導向):該步驟失敗,顯示 Webhook answered HTTP 及該狀態碼。
  • 2xx 但沒有回覆字串:執行狀態為 Done,該步驟顯示 HTTP 200 且無回覆,客戶不會收到任何自動回覆。
  • 失敗的執行會發電郵通知你,以便你親自回覆該客戶,每個自動化每 6 小時最多一封。
  • 若你在暫停期間(預設 4 小時,可於 Advanced 下設定)從手機回覆過某對話,該對話會在呼叫 n8n 之前以 human_active 略過。
  • 多則快速訊息會啟動多次執行。在「Call my webhook」之前加入 1 分鐘的 Wait,可令其只回覆一次,因為選中同一自動化的較新訊息會取代等待中的執行。

何時應改由 n8n 直接接收 Meta 的 webhook?

當 agent 需要超過 20 秒、工作流程必須看到每一個事件(狀態、你的手機回覆、媒體 ID)、需要傳送沒有任何步驟涵蓋的內容(例如 Flow 或目錄訊息),或業務需要 6 個月歷史記錄匯入時,就應如此。6 個月歷史記錄匯入只會傳送至連接後 24 小時內儲存的直接目的地。直接模式會為該號碼關閉自動化:Meta 會把自己的 payload 直接 POST 至 n8n,easyCoexistence 不會看到任何訊息。

在 n8n 中,請使用一般的 Webhook 節點,而非 WhatsApp Trigger 節點,後者需要你自己的 Meta 應用程式及其密鑰。開啟 Allow Multiple HTTP Methods,讓一條又長又隨機的路徑同時接收 Meta 的 GET 握手及 POST 請求;以純文字回傳原始的 hub.challenge 作為 GET 的回應,並透過 set_webhook_destination 連同驗證權杖儲存該 Production URL。之後的回覆會由 HTTP Request 節點傳送至傳送端點發出,而去重、手機暫停及 24 小時檢查將由你自行負責。

Call my webhook (Automations on)Direct webhook (automations off)
Who calls n8neasyCoexistence, when the automation matchesMeta, for every event on the number
Time to answer20 seconds, reply in the responseNo limit: send later through the Graph API
AuthenticationX-EC-Signature with the automation's secretA long random secret in the URL path
GuardsDedupe, phone pause and 24-hour check built inYours to write
What n8n can sendText in the reply; the other steps of the automation can send buttons, files and templatesAny Cloud API message type, templates included
History importNot availableIf saved within 24 hours of connecting
Kept by easyCoexistenceMessage text for your retention period (90 days by default), run logs 30 daysNothing

設定方法

  1. Turn on automations for the numberOn the number's page, the Automations and incoming messages card shows Automations on or Direct to your server. Click Turn on automations if needed; numbers connected without a webhook URL start with automations on.
  2. Build and publish the workflowWebhook node (POST, Respond: Using 'Respond to Webhook' Node), Code node, AI Agent node, Respond to Webhook node. Publish or activate it and copy the Production URL.
  3. Create the automation, switched OffIn Automations, click New automation and pick Send to my system under Start from an example. Paste the Production URL into Webhook URL, set the switch to Off so it does not answer customers yet, and click Save.
  4. Copy the Signing secret into n8nSelect the Call my webhook step of the saved automation, copy Signing secret and paste it into the Code node.
  5. Test, then switch it OnClick Test, type a sample message in Try it and press Run test: n8n runs with dry_run true and the result shows the reply. When it reads right, switch the automation On and save.

常見錯誤

  • 將 Webhook 節點的 Respond 選項保留為 Immediately。n8n 會即時回覆 Workflow got started,執行記錄為 HTTP 200,但沒有回覆,客戶也收不到任何內容。請選擇「Using 'Respond to Webhook' Node」。
  • 貼上 Test URL,即含 /webhook-test/ 的網址。它只在編輯器等待測試事件時接聽;自動化需要已發布工作流程的 Production URL。
  • 使用號碼的轉發密鑰進行驗證。Call my webhook 是以自動化自身的 Signing secret 簽署;轉發密鑰只簽署轉發的事件,因此真正的步驟會未能通過驗證。
  • 以另一種格式回傳文字。只有含頂層字串欄位 reply 的 JSON 內文才會被發送;{"output": "..."} 或 text/plain 內文會被記錄為沒有回覆。
使用 EasyCoexistence 完成這項設定

其 Call my webhook 步驟會把客戶的訊息交給 n8n,再傳回回覆,並內置去重、手機回覆時的暫停,以及 24 小時檢查,每次執行不另外收費。每個號碼每月 US$ 9 起,號碼越多越便宜,最低降至 US$ 0.75,首 3 天免費試用。

常見問題

可以用 ChatGPT 或 Claude 為我建立這個自動化嗎?

可以。加入 easyCoexistence 連接器後,要求在你的號碼上建立一個自動化,將收到的訊息傳送至你的 n8n URL;AI 會呼叫 create_automation 並加入 call_webhook 步驟,再呼叫 test_automation。你可以要求它先建立已停用的自動化,待測試通過後再啟用。Signing secret 不會透過 MCP 回傳,因此請從該自動化的頁面複製。

工作流程能否傳送圖片、按鈕或範本?

不能透過 reply 欄位傳送,該欄位只能承載文字。請改由自動化處理:「Send a picture or file」、「Ask a question with buttons」與「Send a template」等步驟可以放在「Call my webhook」之前或之後。至於其他訊息類型,請從 HTTP Request 節點傳送至 Cloud API,使用 get_api_credentials 回傳的傳送端點及權杖,並且不回傳 reply,以免客戶收到兩次回覆。

一個工作流程能否回覆多個號碼?

可以。為每個號碼的自動化指向同一個 Production URL,並依 channel.phone_number_id 或 automation.id 分支處理。每個自動化都有各自的 Signing secret,因此在驗證簽名前,請先依 automation.id 選取正確的密鑰。

自行架設的 n8n 可以使用嗎?

可以,前提是其 Production URL 為公開的 HTTPS。儲存 URL 時,以及之後每次呼叫前,easyCoexistence 都會拒絕 http、localhost、私有及內部位址,而且不會跟隨重新導向。

繼續閱讀

準備開始嗎?

幾分鐘內設定 WhatsApp Coexistence,而非幾個月。應用程式在手機上仍可繼續運作。

開始免費試用

驗證日期