登入開始免費試用
Meta Tech Provider

自動將 WhatsApp 訊息與聯絡人儲存至 Google Sheets

要將 WhatsApp Business 訊息自動儲存到 Google Sheets,請把訊息以已簽署的 HTTP POST 發送到 n8n、Make 或 Zapier 等自動化工具,再由其 Google Sheets 步驟為每則訊息新增一行,內容包括時間、方向、電話號碼、名稱、文字和訊息 ID。另設一個以電話號碼為鍵的分頁,為每位聯絡人保留一行,對方再次發訊時更新該行。

easyCoexistence 透過 Coexistence 將您在應用程式中保留的 WhatsApp Business 號碼連接至 Meta 的 Cloud API。

7 attempts

轉發的傳送在您的 URL 失敗時,會於約 1、5、30、120、360 和 1,440 分鐘後重試。

10 s

轉發的 POST 等待 2xx 回應的時限,超時之後該次傳送便會重試。

60 / min

Google Sheets API 寫入請求的配額:每位使用者、每個專案每分鐘允許的寫入請求數;每個專案為 300 次。

Redirect

Apps Script 網頁應用程式的輸出會重新導向至一次性的 script.googleusercontent.com 網址。

No headers

Apps Script 的 doPost 事件物件只提供內文與參數,不提供 HTTP 請求標頭。

應由哪條路徑填入試算表?

在訊息經由 easyCoexistence 的號碼上,有兩條路徑,各自滿足不同需要。轉發網址(Forward URL)每個號碼只需設定一次,無論是否有自動化回應,都會轉發 Meta 的原始事件:客戶訊息、您的團隊在 WhatsApp Business 應用程式中輸入的回覆,以及傳送狀態。若要完整的訊息記錄,應採用此路徑。自動化內的「Call my webhook」步驟只會觸發該自動化所選取的訊息,並附上近期對話,包括自動化剛發出的回覆。適合由同一個自動化處理的潛在客戶、訂單或要求記錄。兩者均會簽署,但使用不同的密鑰。

Forward URL (per number)Call my webhook step (per automation)
Fires onAny message event on the number, with or without automationsOnly messages that automation is picked for
CarriesMeta's original value: messages, phone replies, statusesThe message, the contact and up to 10 recent messages
Automated repliesDelivery statuses only, without the textIn history, direction out
Retries7 attempts over about 33 hoursOne attempt
Signed withThe number's forward secret, shown onceThe automation's Signing secret
Set inNumber page, Where incoming messages go cardThe automation editor, or create_automation

會收到哪些欄位,試算表應設哪些欄?

轉發的 POST 會包裝一個 Meta change:id(傳送編號)、event(Meta 的欄位名稱,例如 messages 或 smb_message_echoes)、received_at、connection(您的號碼編號,包括 phone 與 phone_number_id),以及 value,與 Meta 發送時完全相同。在 value 中,messages 存放客戶的來信,message_echoes 存放您的團隊在手機上輸入的內容。電話號碼以數字形式傳來,包含國家代碼,沒有加號;請把該欄設為純文字,以免 Sheets 將其轉為數字。擁有 WhatsApp 用戶名稱的用戶,可能會附上商業範圍的用戶 ID(與號碼並存,或取代號碼),因此請為其保留一欄。

Timemessages[].timestamp, Unix seconds, converted to a date
Directionin for messages, out for smb_message_echoes
Phonemessages[].from, or message_echoes[].to for your replies
User IDmessages[].from_user_id, the BSUID, when Meta sends it
Namecontacts[].profile.name, the customer's WhatsApp profile name
Texttext.body, a button's text, or [image] with its caption
Message idmessages[].id, the wamid: the key that stops duplicates
Your numberconnection.phone, to tell several numbers apart

如何在 n8n 中建立訊息記錄?

需要三個節點。Webhook 節點:HTTP Method 設為 POST,Respond 設為 Immediately,並將其 Production URL 存為該號碼的轉發網址。Code 節點:以轉發密鑰檢查 X-EC-Signature,並將事件轉為行,略過傳送狀態。Google Sheets 節點:使用 Append or Update Row 操作,將 Column to match on 設為「Message id」,如此重試的傳送會改寫同一行,而不會新增一行。

轉發密鑰只會在您儲存轉發網址時顯示一次;若遺失,請按 New secret 並貼上新密鑰。n8n Cloud 允許 Code 節點使用 crypto 模組;自行架設的 n8n 需設定 NODE_FUNCTION_ALLOW_BUILTIN=crypto。

n8n Code node (Run Once for All Items): verify a forward and return rows
const crypto = require('crypto');
const SECRET = 'whsec_...'; // the number's forward secret, shown once when you saved the Forward URL

const { headers, body } = $input.first().json;
const sig = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(headers['x-ec-signature'] || '');
if (!sig || Math.abs(Date.now() / 1000 - Number(sig[1])) > 300) throw new Error('Bad or stale signature');
// n8n has parsed the body; it was sent as compact JSON, so JSON.stringify rebuilds the signed bytes.
const mac = crypto.createHmac('sha256', SECRET).update(sig[1] + '.' + JSON.stringify(body)).digest();
if (!crypto.timingSafeEqual(mac, Buffer.from(sig[2], 'hex'))) throw new Error('Bad signature');

const { event, connection, value } = body;
const names = Object.fromEntries((value.contacts || []).map((c) => [c.wa_id, c.profile?.name || '']));
const textOf = (m) => m.text?.body ?? m.button?.text ?? m.interactive?.button_reply?.title
  ?? m.interactive?.list_reply?.title ?? ('[' + m.type + '] ' + (m[m.type]?.caption || '')).trim();
const row = (m, direction, phone) => ({
  time: new Date(Number(m.timestamp) * 1000).toISOString(), direction, phone,
  user_id: m.from_user_id || '', name: names[phone] || '', text: textOf(m),
  message_id: m.id, number: connection.phone,
});

let rows = [];
if (event === 'messages') rows = (value.messages || []).map((m) => row(m, 'in', m.from || ''));   // statuses skipped
if (event === 'smb_message_echoes') rows = (value.message_echoes || []).map((m) => row(m, 'out', m.to || ''));
return rows.map((json) => ({ json }));

如何為每位聯絡人保留一行?

在聯絡人分頁再加一個 Google Sheets 節點,同樣使用 Append or Update Row,並以 Phone 為比對欄。對應電話、個人檔案名稱、用戶 ID、時間,以及最新訊息的文字。來自某號碼的第一則訊息會建立其行;之後的訊息則更新名稱與最後聯絡時間欄,讓該分頁同時可作為發訊者及時間的名單。請將手寫備註放在另一個分頁,並以電話號碼查找,如此工作流程就不會覆寫它們。

手機通訊錄中儲存的聯絡人不會以此方式傳來。Meta 只會在連接號碼後 24 小時內儲存的直接 webhook 目的地,傳送這些聯絡人及最多 6 個月的對話。

Apps Script 能否直接接收訊息?

不能妥善處理,因此請在試算表前放置一個工作流程工具。Google 會透過重新導向至一次性 script.googleusercontent.com 網址來提供 Apps Script 網頁應用程式的回應,而 easyCoexistence 不會跟隨重新導向。302 會被視為傳送失敗:轉發最多重試 7 次,每次重試都會再次執行 doPost 並寫入另一行;而「Call my webhook」步驟會失敗並向您發送電郵。doPost 也無法讀取請求標頭,因此在那裡無法進行 X-EC-Signature 檢查。

Apps Script 仍適合反方向的用途,例如從試算表的行發送已批准的範本,Google Sheets 整合頁面有相關程式碼。

Make 或 Zapier 能否改為填入試算表?

可以。在 Make 中,以 Webhooks > Custom webhook 啟動情境,再加入 Google Sheets > Add a Row;在 Zapier 中,使用 Webhooks by Zapier 搭配 Catch Raw Hook(會保留標頭),然後使用 Google Sheets > Create Spreadsheet Row。兩者都會立即以 2xx 回應 POST,這正是轉發在 10 秒內所需。請從 value.messages 對應上述相同欄位,並篩選掉 value 中含有 statuses 而非 messages 的事件。

請將 hook 網址視為密碼,因為任何持有者都可以寫入行。若您的方案包含程式碼步驟,請在那裡以 n8n 節點所用的相同 HMAC 方法檢查 X-EC-Signature。

如何避免重複或遺漏的行?

重複來自重試。若轉發未在 10 秒內收到 2xx,將會以相同的 X-EC-Delivery 標頭及內文中相同的 id 再次傳送,其中的訊息仍保有相同的 wamid,因此以「Message id」作比對,可令重試不產生副作用。在 webhook 步驟路線中,來自 Try it 的測試會帶有 dry_run true:請將其導向測試分頁或直接捨棄。

遺漏的行來自 Google 的寫入配額。Sheets API 允許每個專案每位使用者每分鐘 60 次寫入請求,而一個 n8n 憑證即代表一位使用者;在範本行銷活動後突然湧入的大量回覆,可能超出此限制並收到 HTTP 429。請在 Google Sheets 節點的設定中開啟 Retry On Fail,或將訊息收集數秒後,以一次請求批次寫入。

設定方法

  1. Check the number's modeOn the number's page, the card Where incoming messages go must show Through easyCoexistence. If it shows Straight to your system, choose Through easyCoexistence.
  2. Build the workflowWebhook node (POST, Respond Immediately), the Code node above, then Google Sheets with Append or Update Row matched on Message id. Activate it and copy the Production URL.
  3. Save the Forward URLPaste the Production URL into Forward URL (optional) and click Save forward URL. Copy the signing secret shown once into the Code node.
  4. Send a test messageFrom another phone, write to your number and reply to it from the WhatsApp Business app. Two rows should appear: one in, one out.

常見錯誤

  • 以自動化的 Signing secret 檢查轉發,或反過來。Forward URL 以該號碼的轉發密鑰簽署,Call my webhook 則以自動化本身的密鑰簽署;請依 X-EC-Event 標頭選擇密鑰。
  • 每則 POST 都新增一行而沒有鍵值。若傳送因回應緩慢而被重試,同一則訊息會寫入兩次;請使用 Append or Update Row,並以 message id 比對。
  • 把傳送狀態當作訊息寫入。messages 事件的 value.statuses 中也帶有 sent、delivered 和 read 狀態,這些狀態沒有文字,會產生空白行。
  • 把 Apps Script 網頁應用程式網址儲存為轉發網址。其重新導向會被視為失敗,因此 7 次嘗試中的每一次都會再次執行 doPost 並新增重複行。
使用 EasyCoexistence 完成這項設定

開啟自動化後,轉發網址會把 Meta 的原始事件傳送到您的工作流程,以只有您持有的密鑰簽署,並在您的網址停止運作時重試;「Call my webhook」步驟則傳送自動化所處理的內容及其回覆。兩者皆包含在方案中。每個號碼每月 US$ 9 起,最低降至 US$ 0.75,號碼越多越便宜,前 3 天免費試用。

常見問題

試算表會收到我的團隊在手機上輸入的訊息嗎?

會,透過轉發網址。在 WhatsApp Business 應用程式中輸入的回覆,會以 smb_message_echoes 事件抵達,並附上客戶的號碼於 to 欄位。由自動化發出的回覆只會以傳送狀態返回,因此請在回覆之後放置的「Call my webhook」步驟中記錄其文字。

我能把現有對話載入試算表嗎?

在訊息經由 easyCoexistence 的號碼上不行。Meta 只會在連接後 24 小時內儲存的直接 webhook 目的地,傳送最多 6 個月的一對一對話,且每個連接僅一次。經由 easyCoexistence 路由的號碼,會從儲存轉發網址之時起開始記錄。

easyCoexistence 會保留訊息的副本嗎?

部分會儲存。在已開啟自動化、且經由 easyCoexistence 傳送訊息的號碼上,訊息文字會依您選擇的期限保留,可選 30、90 或 365 天(預設為 90 天),並顯示於「Conversations」;轉發的傳送內容最多保留 7 天以供重試;執行記錄則保留 30 天。試算表才是您的永久副本。

我的 AI 能否讀取試算表來回覆客戶?

「Reply with AI」步驟無法,該步驟只會讀取其指令與對話內容。AI 代理可透過自訂 HTTP 工具查找資料,「Call my webhook」或「Call an API」步驟則可將問題送到一個工作流程,由其在試算表中查找答案,並以回覆形式返回。

繼續閱讀

準備開始嗎?

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

開始免費試用

驗證日期