Messenger聊天机器人:实现人工转接协议并追踪用户对话历史
First off, great job getting the human handover flow working smoothly—those edge cases can be tricky! Now, let's break down how to implement full chat history tracking for every user that interacts with your bot, even if they never trigger a handover to a human agent.
Core Requirements Recap
You need to log:
- All messages between users and the bot
- System events (handover requests, bot regaining control)
- Messages exchanged during human agent interactions (since you already have that flow set up)
Step-by-Step Implementation
1. Hook Into Your Messenger Webhook for Incoming Messages
Every time your bot receives a message from a user (text, button taps, quick replies, etc.), capture the key details from the webhook payload and log them to your database. Here’s what you’ll want to store per entry:
user_id: The uniquesender.idfrom the payloadtimestamp: Thetimestampfield from the message object (convert to a readable datetime if needed)sender_type: Label this asusercontent: For text messages, usemessage.text; for buttons/quick replies, usemessage.payload; for attachments, store a summary (e.g., "Image attachment")conversation_id: Optional, but useful for grouping messages from the same thread
2. Log Bot Outgoing Responses
Don’t forget to track what your bot sends back! Right after your bot sends a response via the Messenger Send API, log a similar entry with:
user_id: Same as abovetimestamp: Current time when the response was sentsender_type: Label this asbotcontent: The exact text/attachment details your bot sent
3. Extend Logging for Human Handover Events
Since you already have the handover flow working, add logs for these critical system events:
- When a user taps 「Chat with a person」: Log an entry with
sender_type: systemandcontent: "User initiated human handover" - When an admin clicks 「Mark as done」: Log another system entry:
content: "Bot regained conversation control" - To capture messages between the user and human admins, use the Facebook Graph API’s
/{page-id}/conversationsendpoint. You can pull these messages periodically or set up webhooks for Page Inbox events, then sync them to your database withsender_type: admin.
4. Database Schema Recommendations
Use a database that supports easy querying by user ID and timestamp—PostgreSQL, MongoDB, or even Firebase Firestore work well. A sample SQL schema might look like this:
CREATE TABLE chat_history ( id SERIAL PRIMARY KEY, user_id VARCHAR(255) NOT NULL, timestamp TIMESTAMP NOT NULL, sender_type VARCHAR(50) NOT NULL CHECK (sender_type IN ('user', 'bot', 'admin', 'system')), content TEXT NOT NULL, conversation_id VARCHAR(255) );
5. Compliance & Testing
- Privacy First: Make sure you’re adhering to Facebook’s Platform Policies and regulations like GDPR. Only store necessary data, and provide users with a way to request deletion of their chat history if required.
- End-to-End Testing: Test the full flow: send messages as a user, trigger handover, have an admin respond, hand back control, and verify all entries are logged in chronological order.
内容的提问来源于stack exchange,提问作者Steve.NayLinAung

