ඔබේ AI agent එකට memory දෙන්න: මාස හයක WhatsApp chats සහ contacts
Coexistence හරහා සම්බන්ධ කළ අංකයක, ව්යාපාරය යෙදුමේ එකඟ වුවහොත්, Meta එකට එක් වරක් යෙදුමේ contacts සහ දින 180 දක්වා one-to-one chats ඔබේ server එකට යැවිය හැකිය. ඒ සඳහා සම්බන්ධ කළ පැය 24ක් ඇතුළත direct webhook destination එකක් සුරකින්නේ නම් පමණි. Chunks අනුපිළිවෙල නොවී පැමිණිය හැකි අතර, progress 100 අවසානය දක්වයි. පණිවිඩ id අනුව ගබඩා කරන්න, පුද්ගලයින් BSUID අනුව තබන්න, සහ අවසන් turns agent එකට ලබා දෙන්න.
easyCoexistence අංකය Meta හි Coexistence flow හරහා සම්බන්ධ කරන අතර, ඔබ ඔබේ endpoint එක පැය 24ක් ඇතුළත direct webhook destination ලෙස සුරකින විට, contacts සහ දින 180 දක්වා chats සඳහා Meta වෙතින් එක් වරක් ඉල්ලයි, ඒවා සෘජුවම ඔබේ server එකට යයි.
Onboarding සිට history sync එක ළඟා වන කාලය; Meta හි feature table එක එය වඩාත් මෑත මාස 6 ලෙස හඳුන්වයි.
Onboarding එකෙන් පසු contacts සහ history ඉල්ලන්නට ගතවන කාලය; එක් ඉල්ලීමක් එක් වරක් පමණක් කළ හැකිය.
metadata.progress අගය, එය history sync එක සම්පූර්ණ බව දක්වයි; Meta පවසන්නේ phase පමණක් ප්රමාණවත් නොවන බවයි.
WhatsApp Business යෙදුමේ ව්යාපාරය history sharing අක්රිය කළ විට history webhook එකක දක්වෙන දෝෂ කේතය.
Phase 0 දින 0 සිට 1 දක්වා onboarding ට පෙර, phase 1 දින 1 සිට 90 දක්වා, phase 2 දින 90 සිට 180 දක්වා.
Meta යවන විශාලතම webhook payload එක, එයින් ඔබේ endpoint එකට අවශ්ය body ප්රමාණ සීමාව තීරණය වේ.
ආයාතයෙන් ලැබෙන්නේ මොනවාද, සහ පිටතම ඉතිරි වන්නේ මොනවාද?
ප්රවාහ දෙකක්, දෙකම WhatsApp Business යෙදුමෙන් දුරකථනයේ සිට. Contacts smb_app_state_sync webhooks ලෙස පැමිණේ, එක් contact එකකට එක් entry එකක්, action add හෝ remove සමඟ; එම field එකම ව්යාපාරය යෙදුමේ පසුව එකතු කරන, සංස්කරණය කරන හෝ ඉවත් කරන contacts ද වාර්තා කරයි. Chats history webhooks ලෙස පැමිණේ: onboarding ට පෙර දින 180 දක්වා ඇති one-to-one සංවාද, දෙපැත්තෙන්ම, ඒවායේ delivery status එක සමඟ. පහතින් ලැයිස්තුගත කර ඇති දේ ඒ හරහා නොපැමිණේ, ඒ නිසා ආයාත දත්ත මත ගොඩනඟන memory එකකින් ඒවා බලාපොරොත්තු නොවිය යුතුය.
ආයාත දත්ත ඔබේ server එකට ළඟා වන බව ඔබ සහතික කරන්නේ කෙසේද?
සම්බන්ධ කිරීමට පෙර endpoint එක live කර තබා ගන්න, ඉන්පසු අංකය සම්බන්ධ කළ පැය 24ක් ඇතුළත එය අංකයේ direct webhook destination ලෙස සුරකින්න: අංකයේ පිටුවේ Route incoming messages to your own webhook යටතේ, AI හරහා set_webhook_destination සමඟ, හෝ Partner API connect session එකක webhook_url ලෙස. මෙය සුරකීම නිසා easyCoexistence Meta වෙතින් sync ඉල්ලයි, පළමුව contacts, ඉන්පසු history, onboarding එකකට එක් වරක්; ඉල්ලීම සහ Meta හි request ids අංකයේ event log එකේ history_sync_requested ලෙස දිස්වේ.
සම්බන්ධ කිරීම අතරතුර යෙදුම ඉල්ලන විට ව්යාපාරය history බෙදා ගැනීමට එකඟ විය යුතුය. පණිවිඩ easyCoexistence හරහා එන අංකයක (Where incoming messages go කාඩ්පතේ Through easyCoexistence), Forward URL එකක් තිබුණත්, ආයාතයක් නොලැබේ, මන්ද ඉල්ලීම කරන්නේ direct destination එකකට පමණි. පසුව automations අවශ්ය නම්, ආයාතය අවසන් වූ පසු මාරු කරන්න.
History chunks පැමිණෙන්නේ කෙසේද?
අනුපිළිවෙල නොවී, නවතම පණිවිඩවලින් පසුපසට යන phases වලින්: phase 0 onboarding ට පෙර අවසාන දිනය, phase 1 දින 90 දක්වා, phase 2 දින 180 දක්වා. අනුපිළිවෙල වැදගත් නම් metadata.chunk_order අනුව අනුපිළිවෙලට සකසන්න, සහ metadata.progress = 100 අවසානය ලෙස සලකන්න, මන්ද phase එක අවසන් බව phase එකෙන් නොකියන බව Meta සටහන් කරයි. සංවාදයක id එක පාරිභෝගිකයාගේ දුරකථන අංකයයි, බෙදා ගත නොහැකි විට එය ඉවත් වේ, සහ එහි context එකේ wa_id, user_id සහ username ඇත. ව්යාපාරය යවන පණිවිඩයක from ව්යාපාරයේ අංකය සහ to field එකක් ඇත.
{
"messaging_product": "whatsapp",
"metadata": { "display_phone_number": "16315550142", "phone_number_id": "106540352242922" },
"history": [{
"metadata": { "phase": 1, "chunk_order": 2, "progress": 55 },
"threads": [{
"id": "5511987654321",
"context": { "wa_id": "5511987654321", "user_id": "BR.13491208655302741918" },
"messages": [
{
"from": "5511987654321",
"from_user_id": "BR.13491208655302741918",
"id": "wamid.HBgNNTUxMTk4NzY1NDMyMRUCABIYFDNBMDhFN0QxRjM2QTk4RTU2RTY0AA==",
"timestamp": "1789912800",
"type": "text",
"text": { "body": "Can I pick up the cake on Saturday?" },
"history_context": { "status": "read" }
},
{
"from": "16315550142",
"to": "5511987654321",
"id": "wamid.HBgNNTUxMTk4NzY1NDMyMRUCABEYEjQ2RDhGMkE3QjEwQzNFOTFBMgA=",
"timestamp": "1789913100",
"type": "text",
"text": { "body": "Yes, from 9 am. See you then!" },
"history_context": { "status": "read" }
}
]
}]
}]
}Agent එකේ memory කෙසේ ගබඩා කළ යුතුද?
Tables දෙකක් ප්රමාණවත්ය. පුද්ගලයින් BSUID මගින් යතුරු කරයි, එනම් Meta contacts, conversations සහ messages වලට එකතු කරන user_id, ඒ අසල දුරකථන අංකය තබා ගනිමින්, මන්ද username ඇති පරිශීලකයෙකු එය නැතිව පැමිණිය හැකිය; contact එකක් හඳුනා ගැනීමට user_id භාවිත කරන්න යනු Meta හි සටහනේ කියා ඇත. පණිවිඩ wamid මගින් යතුරු කරයි, එවිට ආයාතය, live webhooks සහ නැවත යැවූ එකම පණිවිඩය එක row එකකට එකතු වේ, සහ from_business flag එකක් කතා කළේ කවුද යන්න වාර්තා කරයි. පුද්ගලයා සහ වේලාව අනුව index කරන්න, මන්ද agent එක පුද්ගලයෙකුගේ නවතම turns ඉල්ලයි.
-- One row per person. Key on the BSUID (user_id); keep the phone number beside it.
create table wa_contacts (
user_key text primary key, -- user_id (BSUID) when present, else the phone number
wa_id text, -- phone number in digits, when Meta shares it
username text,
name text, -- full_name from the app's address book
removed boolean not null default false,
updated_at timestamptz not null default now()
);
-- One row per message. The wamid makes history, live events and retries collapse.
create table wa_messages (
id text primary key, -- messages[].id
user_key text not null,
from_business boolean not null,
type text not null, -- text, image, media_placeholder...
body text not null, -- the text, a caption, or a [type] placeholder
sent_at timestamptz not null,
source text not null -- history | live | echo
);
create index wa_messages_by_user on wa_messages (user_key, sent_at desc);Chunks සහ contacts එයට load කරන්නේ කෙසේද?
Idempotent inserts සමඟ, එවිට අනුපිළිවෙල සහ නැවත delivery වීම වැදගත් නොවේ. ingestHistory එක history change එකක් කියවයි: 2593109 ප්රතික්ෂේපයක් log කර ඉදිරියට යයි, සංවාදය context.user_id හෝ එහි id මගින් යතුරු කරයි, from අගය metadata.display_phone_number ට සමාන වූ විට පණිවිඩය ව්යාපාරයේ ලෙස සලකුණු කරයි, සහ text නොමැති විට [type] placeholder එකක් ගබඩා කරයි. ingestContacts user_id හෝ දුරකථන අංකය මත upsert කරන අතර, ඉවත් කළ contacts ඔවුන්ගේ පණිවිඩ මකනවා වෙනුවට flag කරයි. Direct destination එකක change.value සමඟ, හෝ forward හි event එක history හෝ smb_app_state_sync වන විට body එකේ value සමඟ ඒවා කැඳවන්න.
// memory.mjs: npm i pg. Feed it the value of a "history" or "smb_app_state_sync"
// change: change.value on a direct destination, delivery.value on a forward.
import pg from "pg";
const db = new pg.Pool(); // reads the PG* environment variables
export async function ingestHistory(value) {
const business = value.metadata?.display_phone_number; // digits, like messages[].from
for (const chunk of value.history ?? []) {
if (chunk.errors?.some((e) => e.code === 2593109)) {
console.log("The business turned off history sharing in the app"); // contacts still come
continue;
}
for (const thread of chunk.threads ?? []) {
const userKey = thread.context?.user_id ?? thread.id; // thread.id is the phone, when shared
for (const m of thread.messages ?? []) {
await db.query(
`insert into wa_messages (id, user_key, from_business, type, body, sent_at, source)
values ($1, $2, $3, $4, $5, to_timestamp($6), 'history')
on conflict (id) do nothing`,
[m.id, userKey, m.from === business, m.type,
m.text?.body ?? m[m.type]?.caption ?? `[${m.type}]`, Number(m.timestamp)]
);
}
}
const { phase, chunk_order, progress } = chunk.metadata ?? {};
console.log(`history: phase ${phase}, chunk ${chunk_order}, ${progress}%`);
if (Number(progress) === 100) console.log("Import complete; late chunks still insert safely");
}
}
export async function ingestContacts(value) {
for (const s of value.state_sync ?? []) {
const c = s.type === "contact" ? s.contact ?? {} : null;
const userKey = c && (c.user_id ?? c.phone_number);
if (!userKey) continue;
await db.query(
`insert into wa_contacts (user_key, wa_id, username, name, removed)
values ($1, $2, $3, $4, $5)
on conflict (user_key) do update set
wa_id = coalesce(excluded.wa_id, wa_contacts.wa_id),
username = coalesce(excluded.username, wa_contacts.username),
name = coalesce(excluded.name, wa_contacts.name),
removed = excluded.removed, updated_at = now()`,
[userKey, c.phone_number ?? null, c.username ?? null, c.full_name ?? c.first_name ?? null,
s.action === "remove"]
);
}
}
// A bounded memory for one person: who they are and their last turns, oldest first.
export async function memoryFor(userKey, turns = 20) {
const [{ rows: [contact] }, { rows }] = await Promise.all([
db.query("select name from wa_contacts where user_key = $1 and not removed", [userKey]),
db.query(
`select from_business, body from wa_messages
where user_key = $1 order by sent_at desc limit $2`, [userKey, turns]),
]);
const history = rows.reverse().map((r) => ({ role: r.from_business ? "assistant" : "user", content: r.body }));
while (history[0]?.role === "assistant") history.shift(); // open on the customer's turn
const who = contact?.name ? `The customer is ${contact.name}.` : "The customer is not in the address book.";
return { system: `${process.env.AI_INSTRUCTIONS ?? ""}\n${who}`.trim(), history };
}එම memory agent එකට කෙසේ ලබා දෙන්නද?
සීමා සහිතව, කිසි විටෙක සියල්ල එකවර නොවේ. memoryFor මගින් address book එකෙන් පුද්ගලයාගේ නම සහ ඔවුන්ගේ අවසාන turns 20, පැරණිම පළමුව, බොහෝ chat APIs බලාපොරොත්තු වන පරිදි පාරිභෝගික turn එකකින් ආරම්භ වන ලෙස ලබා දෙයි; agent එකට වැඩි අවශ්ය නම් පැරණි සංවාද සාරාංශයක් එකතු කරන්න. සෑම prompt එකකම මාස හයක chats ගැනීම සෑම පිළිතුරකටම tokens වැය කරන අතර ඇසෙන ප්රශ්නය යටපත් කරයි.
සසඳා බැලීමට, built-in Reply with AI step එක උපරිම පණිවිඩ 20ක් (පෙරනිමියෙන් 10) කියවන අතර, Call my webhook step එක අවසන් 10 යවයි. දෙකම හිමිකරු තෝරන රඳවා ගැනීමේ කාලය, දින 30, 90 හෝ 365 (පෙරනිමියෙන් 90), තබා ගත් text මත පදනම් වේ, automations ක්රියාත්මක වූ මොහොතේ සිට, ඒ නිසා ආයාත කළ මාස හයක chats ඔබේම database එකේ තිබිය යුතුය.
ආයාතයෙන් පසු memory නව ලෙස තබා ගන්නේ කෙසේද?
එම destination එක live traffic ලබා ගන්නා අතර: පාරිභෝගිකයින්ගෙන් පණිවිඩ, යෙදුමේ ටයිප් කළ පිළිතුරු සඳහා smb_message_echoes, සහ ව්යාපාරය contacts සංස්කරණය කළ විට smb_app_state_sync. ඒවා එම tables හරහා source live හෝ echo සමඟ ඇතුළත් කරන්න. පාරිභෝගිකයෙක් දුරකථන අංකය වෙනස් කළ විට, Meta type user_changed_number හෝ user_changed_user_id සහිත system message එකක් යවයි, previous_user_id සමඟ, ඔවුන්ගේ rows නව BSUID වෙත ගෙනයාමට ඇති ඉඟිය එයයි.
පසුව automations ක්රියාත්මක කළ විට direct destination එක ඉවත් වේ. එම URL එකම Forward URL ලෙස එක් කර, සෑම signed POST එකකින්ම event සහ value කියවන්න, සහ මාරු කිරීමට පෙර progress 100 එනතෙක් රැඳී සිටින්න, නැතිනම් ඉතිරි chunks එම forward හරහා පමණක් ඔබට ළඟා වේ.
එය සකසන ආකාරය
- Put the endpoint onlineIt must return hub.challenge as plain text on Meta's GET handshake when hub.verify_token matches, accept POST bodies of a few megabytes and answer 200 fast.
- Connect the numberRun the connection from the dashboard, and accept sharing chat history when the WhatsApp Business app asks.
- Save the destination within 24 hoursOn the number's page, under Route incoming messages to your own webhook, fill Webhook URL (with a long random path) and Verify token, then Save; or ask your AI to call set_webhook_destination.
- Store chunks until progress 100Insert contacts and messages as they come; the event log shows history_sync_requested once the request went to Meta.
- Switch modes only afterwardsIf you want automations, choose Through easyCoexistence in the card Where incoming messages go and add the same URL as the Forward URL.
සාමාන්ය වැරදි
- දෙවන දිනයේ destination එක සුරකීම. Sync ඉල්ලන්නේ සම්බන්ධ කළ පැය 24ක් ඇතුළතදී පමණි, ඒ නිසා පසුව සුරකින endpoint එකක් live පණිවිඩවලින් පමණක් ආරම්භ වේ.
- Progress 100 ට පෙර automations ක්රියාත්මක කිරීම. Direct destination එක එකවර ඉවත් වේ, සහ ඉතිරි chunks ඔබට ළඟා වන්නේ Forward URL එකක් දැනටමත් සකස් කර ඇත්නම් පමණි.
- Phase 2 ආයාතයේ අවසානය ලෙස සලකීම. Sync එක සම්පූර්ණ බව කියන්නේ metadata.progress = 100 පමණි, සහ chunks අනුක්රමිකව delivery නොවේ.
- Memory යතුරු කිරීම දුරකථන අංකය පමණක් මත. Username ඇති පරිශීලකයෙකු එය බෙදා ගත නොහැකි විට සංවාද ids සහ contacts දුරකථන අංකය ඉවත් කරයි, user_id තවමත් පවතින අතර.
ඉල්ලීම අංකයේ event log එකේ වාර්තා වන අතර, පසුව අංකය 24/7 නිරීක්ෂණය වේ. අංකයකට මාසිකව US$ 9 සිට US$ 0.75 දක්වා, අංක එකතු කරන විට අඩු වේ, පළමු දින 3ක නොමිලේ trial කාලය සමඟ.
නිතර අසන ප්රශ්න
ආයාත ක්රියාවලිය දෙවන වරට ධාවනය කළ හැකිද?
onboarding එකක් හරහා පමණි. Meta onboarding එකකට එක් ඉල්ලීමක් ඉඩ දෙන බැවින්, අංකය විසන්ධි කර නැවත සම්බන්ධ කළ යුතුය, ඉන්පසු chats නැවතත් පැමිණේ; message id අනුව ඇතුළත් කිරීම නිසා දෙවන පාස් එක කිසිවක් දෙවරක් එකතු නොකරයි.
easyCoexistence ආයාත කළ chats පිටපතක් තබා ගනීද?
නැත. History සහ contacts Meta සිට කෙලින්ම ඔබේ endpoint එකට යන අතර පෙරනිමියෙන් ගබඩා නොවේ. Automations ක්රියාත්මක අංකයක් සඳහා, easyCoexistence පණිවිඩ text ඔබ තෝරන කාලය, දින 30, 90 හෝ 365 (පෙරනිමියෙන් 90), තබා ගන්නා අතර Conversations තිරය එය පෙන්වයි; forward කළ deliveries දින 7ක් දක්වා, සහ run logs දින 30ක් තබා ගනී.
Payloads කොපමණ විශාල විය හැකිද?
තනි Meta webhook එකක 3 MB දක්වා ගෙන යා හැකි අතර, කාර්යබහුල අංකයක් බොහෝ history chunks යවයි. ඔබේ framework එකේ body limit එක වැඩි කරන්න (Express පෙරනිමිය 100 kb), සහ database එකට ලිවීමට පෙර acknowledge කරන්න.
Agent එකට ආයාතය ක්රියා කිරීමට අවශ්යද?
නැත. එය නොමැතිව, memory සම්බන්ධ වීමෙන් පසු පළමු පණිවිඩයෙන් ආරම්භ වී live traffic වලින් වැඩි වේ. ආයාතය වැදගත් වන්නේ පසුගිය සංවාද පළමු දිනයේ සිටම පිළිතුරු හැඩගස්වන විට, උදාහරණයක් ලෙස ආපසු පැමිණෙන පාරිභෝගිකයෙකුගේ අවසන් ඇණවුම.
තවදුරටත් කියවන්න
ආරම්භ කිරීමට සූදානම්ද?
WhatsApp Coexistence මිනිත්තු කිහිපයකින් සකසන්න, මාස ගණනකින් නොව. යෙදුම දුරකථනයේ දිගටම ක්රියා කරයි.
නොමිලේ trial කාලය ආරම්භ කරන්නසත්යාපනය කළේ