npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@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

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 (checkQrStatus vớ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.
  • 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).
  • 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).
  • 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).
  • Chuẩn hóa & An toàn:
    • Viết 100% bằng TypeScript strict mode, tự sinh .d.ts type 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.

Cài đặt (Installation)

npm install @redonvn/messaging-zalo

Hướ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);
  }
}