loud-crowd-60573
12/30/2025, 3:38 PMorange-memory-1465
12/30/2025, 3:39 PMloud-crowd-60573
12/30/2025, 3:46 PMorange-memory-1465
12/30/2025, 3:47 PMorange-memory-1465
12/30/2025, 3:48 PMloud-crowd-60573
12/30/2025, 3:55 PMadmin scope (keep secret!)
const TOKEN = "";
// =================
// Helper to call Botpress Admin API
async function bpFetch(path) {
const res = await fetch(${BP_API}${path}, {
headers: { Authorization: Bearer ${TOKEN} }
});
if (!res.ok) throw new Error(BP API error: ${res.status});
return res.json();
}
// Get the HubSpot contact ID that Botpress knows about
// When the HubSpot integration is installed, the contact ID is stored
// as a user tag hubspot:contactId. We can read it via the Botpress API.
async function getHubSpotContactId() {
const me = await bpFetch("/admin/users/me");
return me.tags?.["hubspot:contactId"];
}
// Fetch conversation history for that contact
async function loadHistory(contactId) {
// Botpress conversation ID is derived from the user ID.
// Use the āGet conversationsā endpoint and filter by user ID.
const convs = await bpFetch(/admin/conversations?userId=${contactId});
if (!convs.conversations?.length) return [];
// Pick the latest conversation (or iterate all)
const convId = convs.conversations[0].id;
const msgs = await bpFetch(/admin/conversations/${convId}/messages);
return msgs.messages || [];
}
// Render messages in the container
function renderMessages(messages) {
const container = document.getElementById("bp-chat-history");
container.innerHTML = ""; // clear
messages.forEach(m => {
const div = document.createElement("div");
div.style.marginBottom = "6px";
const author = m.from === "bot" ? "Bot" : "You";
const bg = m.from === "bot" ? "#e6f7ff" : "#f0f0f0";
div.style.background = bg;
div.style.padding = "6px 10px";
div.style.borderRadius = "4px";
div.textContent = `${author}: ${m.payload?.text || "[nonātext]"}`;
container.appendChild(div);
});
// scroll to bottom
container.scrollTop = container.scrollHeight;
}
// Initialise the standard Webchat widget (still plain JS)
function initWebchat() {
// Load the Webchat script from Botpress CDN
const script = document.createElement("script");
script.src = "https://cdn.botpress.cloud/webchat/v0/webchat.js";
script.onload = () => {
window.botpressWebChat.init({
// required params
botId: BOT_ID,
// you can hide the builtāin header if you prefer
hideHeader: false,
// container where the live chat will be rendered
container: "#bp-webchat"
});
};
document.body.appendChild(script);
}
// ==== Run the flow ====
(async () => {
try {
const contactId = await getHubSpotContactId();
if (!contactId) throw new Error("HubSpot contact ID not found.");
const history = await loadHistory(contactId);
renderMessages(history);
// finally start live chat so new messages appear
initWebchat();
} catch (e) {
console.error(e);
document.getElementById("bp-chat-history").innerText = "āļø Unable to load chat history.";
}
})();
`loud-crowd-60573
12/30/2025, 3:56 PMorange-memory-1465
12/30/2025, 3:57 PM