newchat.js
v1.1.0
Published
Unofficial Node.js wrapper for NewChat API
Maintainers
Readme
Cài đặt
npm install newchat.jsQuickstart
const login = require('newchat.js');
const api = await login('[email protected]', 'password');
await api.listen((err, event) => {
if (err || event.type !== 'message') return;
if (event.data.isSelf) return;
if (event.data.body === '/ping') {
api.sendMessage(event.data.threadID, 'pong 🏓');
}
});Demo: Bot nhận
/pingvà trả vềpong 🏓

AppState — Không cần login lại
const fs = require('fs');
const { loadAppState } = require('newchat.js');
// Lưu session
const api = await login('[email protected]', 'password');
fs.writeFileSync('appstate.json', JSON.stringify(api.getAppState()));
// Lần sau dùng lại
const api = await loadAppState(JSON.parse(fs.readFileSync('appstate.json')));API
| Hàm | Mô tả |
|-----|-------|
| login(email, password) | Đăng nhập, trả về api object |
| loadAppState({ token }) | Khôi phục session từ token |
| api.getAppState() | Lấy { token } để lưu |
| api.listen(callback) | Lắng nghe events realtime qua WebSocket |
| api.sendMessage(threadID, text) | Gửi tin nhắn văn bản |
| api.sendAttachment(threadID, filePath, caption?) | Gửi file/ảnh |
| api.markAsRead(threadID) | Đánh dấu đã đọc |
| api.getThreadList() | Danh sách cuộc trò chuyện |
| api.getThreadHistory(threadID, options?) | Lịch sử tin nhắn |
| api.getUserInfo() | Thông tin tài khoản đang đăng nhập |
| api.getMyProfile() | Alias của getUserInfo() |
Xem đầy đủ tại docs site.
Cấu trúc dự án
newchat.js/
├── src/
│ ├── index.js # Entry point — login(), loadAppState()
│ ├── httpUtils.js # Axios instance + setToken()
│ ├── wsClient.js # WebSocket — Socket.IO v4 + MessagePack
│ └── api/
│ ├── listen.js
│ ├── sendMessage.js
│ ├── sendAttachment.js
│ ├── markAsRead.js
│ ├── getThreadList.js
│ ├── getThreadHistory.js
│ └── getUserInfo.js
├── docs-site/ # VitePress documentation
├── DOCS.md # API reference đầy đủ
├── test.js # Test script
└── package.jsonKiến trúc
REST → https://api.newchat.vn (Bearer JWT)
WS → wss://ws.newchat.vn (Socket.IO v4 + MessagePack binary)WebSocket auth flow:
- Server gửi EIO OPEN (
0{sid,...}) - Client gửi binary msgpack
{ type:0, data:{token}, nsp:"/" } - Server xác nhận CONNECT ACK
- Server push
channel:messageevents
Thống kê Download
Build & Phát triển
# Cài dependencies
npm install newchat.js
# Chạy test (ping/pong bot)
node test.js
# Build docs
cd docs-site && npm run buildLicense
MIT © KaiyoDev
⚠️ Unofficial — không liên kết với đội ngũ newchat.vn. API có thể thay đổi bất kỳ lúc nào.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
