npm install newchat.jsconst login = require('newchat.js');
const api = await login('email@example.com', '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 🏓
const fs = require('fs');
const { loadAppState } = require('newchat.js');
// Lưu session
const api = await login('email@example.com', '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')));| 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.
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.json
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
# Cài dependencies
npm install newchat.js
# Chạy test (ping/pong bot)
node test.js
# Build docs
cd docs-site && npm run buildMIT © 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.
Thanks goes to these wonderful people (emoji key):
Đặng Hoàng Ân 💻 📖 🚧 |
This project follows the all-contributors specification. Contributions of any kind welcome!
