Chatbot ServiceTích hợp GraphQL (Mẫu)
API Gateway
Lấy Lịch Sử Hội Thoại (ExampleMyConversation)
QUERY MẪU
Yêu cầu Bearer Token
ExampleMyConversation.gql
Variables
Response 200 OK
exampleFetchHistory.ts
🛡️ Lưu Ý Schema Mẫu: Tài liệu này mô tả định dạng truy vấn mẫu (Example Query). Cấu trúc schema và tên trường chính thức sẽ được cấp riêng trong tài liệu hợp tác.
Truy vấn danh sách các tin nhắn cũ trong toàn bộ cuộc hội thoại của người dùng theo bot_code. Hỗ trợ phân trang tiến lùi dựa trên mốc thời gian cursor_timestamp và số lượng page_size.
Chi Tiết Truy Vấn & Mã Nguồn Mẫu
GraphQL Query & Payload (Mẫu):
GraphQL Query
query ExampleMyConversation($input: ExampleConversationHistoryInput) {
exampleMyConversation(input: $input) {
message_list {
msg_id
session_id
sender_role
sender_id
sender_name
is_session_active
rating_feedbacks {
feedback_id
score_rating
comment_text
}
content_text
created_timestamp
delivery_status
}
page_info {
current_page
page_size
total_items
total_pages
}
total_count
}
}Variables (JSON Mẫu)
{
"input": {
"bot_code": "ai_assistant",
"cursor_timestamp": null,
"page_size": 20,
"sort_order": [{ "field": "created_timestamp", "direction": "desc" }]
}
}Phản Hồi Thành Công (JSON Response Mẫu)
{
"data": {
"exampleMyConversation": {
"message_list": [
{
"msg_id": "msg_ast_01j89a8bc43e",
"session_id": "sess_89f1a23b4c5d6e7f",
"sender_role": "ASSISTANT",
"sender_id": "bot_assistant_01",
"sender_name": "Pango AI Assistant",
"is_session_active": true,
"rating_feedbacks": [],
"content_text": "Pango AI hỗ trợ đa dạng các giải pháp tích hợp bao gồm: Web Chat Widget, SDK nhúng Mobile App (Flutter/React Native/iOS/Android), và GraphQL API Gateway tùy biến...",
"created_timestamp": 1740200015000,
"delivery_status": "COMPLETED"
},
{
"msg_id": "msg_usr_01j89a8bc43d",
"session_id": "sess_89f1a23b4c5d6e7f",
"sender_role": "USER",
"sender_id": "usr_partner_123456",
"sender_name": "Alex Nguyen",
"is_session_active": true,
"rating_feedbacks": [],
"content_text": "Pango AI hỗ trợ các giải pháp và kênh tích hợp nào cho doanh nghiệp?",
"created_timestamp": 1740200010000,
"delivery_status": "COMPLETED"
}
],
"page_info": {
"current_page": 1,
"page_size": 20,
"total_items": 2,
"total_pages": 1
},
"total_count": 2
}
}
}TypeScript / Axios:
import axios from 'axios';
export interface ExampleConversationHistoryInput {
bot_code: string;
cursor_timestamp?: number | null;
page_size?: number;
sort_order?: Array<{ field: string; direction: string }>;
}
export interface ExampleChatMessageItem {
msg_id: string;
session_id: string;
sender_role: 'USER' | 'ASSISTANT' | 'SYSTEM';
sender_id: string;
sender_name: string;
is_session_active: boolean;
rating_feedbacks: Array<{
feedback_id: string;
score_rating: number;
comment_text?: string;
}>;
content_text: string;
created_timestamp: number;
delivery_status: 'PENDING' | 'COMPLETED' | 'FAILED';
}
/**
* Tải lịch sử tin nhắn hội thoại (Mã mẫu)
*/
export async function exampleFetchConversationHistory(
apiUrl: string = process.env.VITE_API_URL!,
token: string,
botCode: string = 'ai_assistant',
cursorTimestamp?: number | null,
pageSize: number = 20
): Promise<ExampleChatMessageItem[]> {
const query = `
query ExampleMyConversation($input: ExampleConversationHistoryInput) {
exampleMyConversation(input: $input) {
message_list {
msg_id
session_id
sender_role
sender_id
sender_name
is_session_active
rating_feedbacks {
feedback_id
score_rating
comment_text
}
content_text
created_timestamp
delivery_status
}
page_info {
current_page
page_size
total_items
total_pages
}
total_count
}
}
`;
const response = await axios.post(
apiUrl,
{
query,
variables: {
input: {
bot_code: botCode,
cursor_timestamp: cursorTimestamp || null,
page_size: pageSize,
sort_order: [{ field: 'created_timestamp', direction: 'desc' }],
},
},
operationName: 'ExampleMyConversation',
},
{
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'x-org-id': process.env.VITE_ORG_ID || 'org_sample_demo',
'x-client-type': 'app',
'x-external-private-key': process.env.VITE_EXTERNAL_PRIVATE_KEY || 'your_secret_key',
},
}
);
return response.data?.data?.exampleMyConversation?.message_list || [];
}Best Practice Khi Tải Lịch Sử
- Đảo chiều mảng hiển thị: Dữ liệu trả về mặc định sắp xếp theo
created_timestamp: desc(tin nhắn mới nhất ở đầu mảng). Khi render giao diện chat từ trên xuống dưới, hãy đảo ngược mảng (message_list.reverse()) để tin nhắn cũ ở trên và mới nhất ở dưới. - Xử lý tin nhắn dở dang (
PENDING): Kiểm tra phần tử tin nhắn cuối cùng: nếusender_role === 'ASSISTANT'vàdelivery_status === 'PENDING', ứng dụng hãy kích hoạt ngay listener Firebase Firestore theo mãmsg_idcủa tin nhắn đó để tiếp tục nhận câu trả lời.