@redonvn/messaging-zalo
v1.1.0
Published
Enterprise Zalo Client SDK for Personal / Web Messaging with QR Auth, Message Handlers, Delivery/Seen Receipts, Typing Presence & Media Uploads
Keywords
Readme
@redonvn/messaging-zalo
Enterprise TypeScript/JavaScript SDK for Zalo Personal / Web Messaging with QR Code Authentication, Message Dispatching, Delivery/Seen Receipts, Typing Presence, and Conversation Management.
Tính năng chính (Features)
- Xác thực Đăng nhập (Authentication):
- Sinh mã QR code để quét đăng nhập trên ứng dụng di động (
generateQrCode). - Polling kiểm tra trạng thái quét mã thời gian thực (
checkQrStatusvới các trạng thái:WAITING_SCAN,SCANNED,ACCEPTED,EXPIRED). - Đăng nhập trực tiếp bằng session cookie / secret key (
cookies: 'zpw_sek=...'). - Tự động sinh Device IMEI chuẩn 16-byte hex hoặc chỉ định IMEI cố định.
- Sinh mã QR code để quét đăng nhập trên ứng dụng di động (
- Nhắn tin Đa định dạng (Rich Messaging):
- Gửi tin nhắn văn bản cá nhân và nhóm (
sendText). - Gửi sticker theo Category ID và Sticker ID (
sendSticker). - Gửi link preview kèm OpenGraph metadata (
sendLink). - Gửi tin nhắn trích dẫn / trả lời tin nhắn cũ (
sendQuote). - Thu hồi tin nhắn cho tất cả mọi người (
undo). - Xóa tin nhắn cục bộ hoặc cả hai bên (
delete).
- Gửi tin nhắn văn bản cá nhân và nhóm (
- Trạng thái & Sự kiện (Presence & Receipts):
- Báo đã nhận tin nhắn - Delivery receipt (
markDelivered/ dấu tick kép xám). - Báo đã xem tin nhắn - Seen receipt (
markSeen/ dấu tick xanh). - Báo trạng thái đang soạn tin nhắn - Typing indicator (
setTyping). - Đánh dấu hội thoại chưa đọc (
markUnread).
- Báo đã nhận tin nhắn - Delivery receipt (
- Danh bạ & Thông tin cá nhân (Users & Contacts):
- Lấy thông tin tài khoản hiện tại (
getProfile). - Lấy danh bạ bạn bè (
getFriends).
- Lấy thông tin tài khoản hiện tại (
- Chuẩn hóa & An toàn:
- Viết 100% bằng TypeScript strict mode, tự sinh
.d.tstype declarations. - Xử lý lỗi phân cấp:
ZaloAuthError,ZaloNetworkError,ZaloApiError,ZaloRateLimitError. - Tự động retry khi gặp sự cố mạng tạm thời.
- Viết 100% bằng TypeScript strict mode, tự sinh
Cài đặt (Installation)
npm install @redonvn/messaging-zaloHướng dẫn sử dụng (Quickstart)
1. Đăng nhập bằng quét mã QR (QR Code Login)
import { ZaloClient } from '@redonvn/messaging-zalo';
async function loginWithQr() {
const client = new ZaloClient();
// 1. Tạo QR code
const qr = await client.auth.generateQrCode();
console.log('Quét mã QR tại link:', qr.qrCodeUrl);
// 2. Chờ người dùng quét và xác nhận trên app Zalo điện thoại
const pollInterval = 2500;
const timeout = qr.expiresInSeconds * 1000;
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
const status = await client.auth.checkQrStatus(qr.code);
if (status.status === 'ACCEPTED') {
console.log('Đăng nhập thành công!');
console.log('Tên hiển thị:', status.displayName);
console.log('Lưu cookie cho các phiên sau:', client.getCookieString());
break;
} else if (status.status === 'SCANNED') {
console.log('Đã quét mã, đang chờ xác nhận trên điện thoại...');
}
await new Promise((r) => setTimeout(r, pollInterval));
}
}2. Gửi tin nhắn (Gửi Text, Sticker, Quote, Thu hồi)
import { ZaloClient } from '@redonvn/messaging-zalo';
const client = new ZaloClient({
cookies: 'zpw_sek=YOUR_SAVED_SESSION_COOKIE',
});
async function sendMessages() {
const targetUid = '84901234567';
// Báo đang soạn tin
await client.conversations.setTyping({
toId: targetUid,
isTyping: true,
});
// Gửi tin nhắn văn bản
const textMsg = await client.messages.sendText({
toId: targetUid,
text: 'Xin chào từ Zalo Client SDK!',
});
console.log('Tin nhắn đã gửi:', textMsg.msgId);
// Gửi sticker
await client.messages.sendSticker({
toId: targetUid,
cateId: 100,
stickerId: 10001,
});
// Trích dẫn / Trả lời tin nhắn
await client.messages.sendQuote({
toId: targetUid,
text: 'Phản hồi tin nhắn trên',
quoteMessageId: textMsg.msgId,
quoteOwnerId: targetUid,
quoteText: 'Tin nhắn gốc',
});
// Thu hồi tin nhắn
await client.messages.undo({
toId: targetUid,
msgId: textMsg.msgId,
cliMsgId: textMsg.cliMsgId,
});
}3. Xử lý lỗi (Error Handling)
import { ZaloClient, ZaloAuthError, ZaloApiError } from '@redonvn/messaging-zalo';
try {
const client = new ZaloClient({ cookies: 'expired_cookie' });
await client.users.getProfile();
} catch (error) {
if (error instanceof ZaloAuthError) {
console.error('Lỗi phiên đăng nhập hoặc cookie hết hạn:', error.message);
} else if (error instanceof ZaloApiError) {
console.error(`Lỗi từ Zalo API [${error.errorCode}]:`, error.message);
}
}