Dùng GitHub Copilot tạo TikTok DM Webhook Receiver
Nếu đội hỗ trợ ở Việt Nam cần nhận TikTok DM, đừng để AI tự đoán payload. Hãy đưa hợp đồng API thật vào GitHub Copilot trước: UnifyPort chuyển tin nhắn inbound từ TikTok thành event chuẩn message.received, cùng cơ chế ký HMAC-SHA256 trên raw request body.
Điểm chính
- Tài liệu chính thức của TikTok mô tả Direct Messages trong Data Portability scopes và data types, không phải một live webhook phổ thông cho customer support.
- Prompt cho Copilot phải dùng đúng tên trong UnifyPort API reference:
POST /v1/webhook-endpoints,subscribed_events,signing_secret,X-Device-Timestamp,X-Device-Signature. - Verify chữ ký trên raw body trước khi parse JSON; serialize lại JSON sẽ đổi bytes.
- Với team Việt Nam dùng cả TikTok, Zalo và WhatsApp, một receiver chuẩn hóa giúp thêm kênh sau này mà không đổi luồng xử lý chính.
Nếu bạn còn đang tìm hiểu TikTok DM API, đọc trước vì sao không có endpoint chính thức phổ thông cho TikTok DM. Nếu mục tiêu là live intake, xem thêm TikTok Data Portability vs live DMs.
Demo cuối cùng
Demo là một Node.js service nhỏ với route /webhook. Nó nhận UnifyPort event, kiểm tra X-Device-Timestamp và X-Device-Signature, parse message.received, lưu các field cần thiết rồi trả 200.
Trong lúc build, mở webhook delivery and signature verification làm nguồn tham chiếu. Trang này mô tả header, chuỗi ký, retry và idempotency.
Prompt đầu tiên cho Copilot
Build a minimal Express service for a UnifyPort webhook receiver.
Use express.raw({ type: 'application/json' }). Verify X-Device-Signature as hex HMAC-SHA256 over X-Device-Timestamp + '.' + raw request body using WEBHOOK_SIGNING_SECRET.
Only process event.type === 'message.received'. Store provider, account_id, conversation.id, sender.id, message.id, message.text, message.direction, and occurred_at.
Return 200 after storing; return 401 on invalid signature.
Phần core code nên giữ raw body:
import crypto from 'crypto';
import express from 'express';
const app = express();
const secret = process.env.WEBHOOK_SIGNING_SECRET;
const queue = [];
function verifySignature(req) {
const timestamp = req.get('X-Device-Timestamp') || '';
const signature = req.get('X-Device-Signature') || '';
const expected = crypto.createHmac('sha256', secret)
.update(timestamp + '.')
.update(req.body)
.digest('hex');
return signature.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
}
app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
if (!verifySignature(req)) return res.status(401).end();
const event = JSON.parse(req.body.toString('utf8'));
if (event.type !== 'message.received') return res.status(200).end();
queue.push({
provider: event.provider,
account_id: event.account_id,
conversation_id: event.data.conversation.id,
sender_id: event.data.sender.id,
message_id: event.data.message.id,
text: event.data.message.text || '',
direction: event.data.message.direction,
occurred_at: event.occurred_at,
});
res.status(200).end();
});
Tạo webhook endpoint
Sau khi deploy HTTPS receiver, tạo endpoint bằng POST /v1/webhook-endpoints. Dùng message.received trong subscribed_events; signing_secret bật chữ ký giao hàng.
curl -X POST https://api.unifyport.ai/v1/webhook-endpoints \
-H "X-Api-Key: dk_live_example" \
-H "Content-Type: application/json" \
-d '{
"url": "https://inbox.example.com/webhook",
"status": "active",
"subscribed_events": ["message.received"],
"signing_secret": "whsec_6f5b1c9d4e7a2b8c"
}'
Muốn xem thêm workflow AI coding, đọc tutorial auto-reply bot bằng AI coding agent.
Thêm độ tin cậy
Prompt tiếp theo nên yêu cầu Copilot thêm idempotency bằng X-Device-Event-Id, lưu raw event và chỉ route message có data.message.direction === 'inbound' vào triage queue. Sau đó mới nối Slack, CRM hoặc AI classifier.
Giới hạn
Nếu sản phẩm cần official publishing, login, research tools hoặc data export, hãy dùng API chính thức của TikTok. UnifyPort là unofficial interface cho live inbound messages từ tài khoản hiện có, cung cấp event stream có thể kiểm chứng chứ không phải official TikTok product scope.
FAQ
Copilot có thể làm toàn bộ inbox không?
Copilot có thể scaffold receiver, tests và queue code. Đội kỹ thuật vẫn cần review signature verification, secrets, deployment và storage behavior.
Data Portability có phải live DM không?
Không. Tài liệu chính thức mô tả export scopes và data types; hệ thống hỗ trợ thường cần live event stream.
Nên subscribe event nào?
Dùng message.received cho inbound messages. Chỉ dùng ["*"] khi bạn xây full event collector.
Sources checked on 2026-08-29
Biến tích hợp nhắn tin thành một pipeline sản phẩm ổn định.
Bắt đầu bằng cách gửi qua một API, rồi đưa mọi tin nhắn inbound trở lại hệ thống kinh doanh bằng sự kiện chuẩn.