yunjia-chat-sdk
v1.0.1
Published
Production-grade TypeScript SDK for Yunjia chat service
Downloads
14
Maintainers
Readme
yunjia-chat-sdk
生产级 TypeScript SDK,用于对接云加聊天服务(IDM 登录、服务发现、Chat Client 注册、Socket 收发、消息发送与回执处理)。
Features
- IDM
password登录与refresh_token自动刷新 - 从
/oauth2.0/profile自动解析企业与 chat/emm 服务地址 - 自动注册 chat client(
/client-registry/rest/v1/client) - 基于
socket.io的长连接通信(支持重连) - 默认使用 Socket.IO v2 客户端(适配云加 chat 服务)
- 可选跨版本模式:
socketIoMode: "auto",自动在 v2/v4 间切换 - 统一封装常见消息能力:
- 单聊文本
- 群聊文本(可 @ 用户)
- 卡片消息
- 撤回消息
- 消息已读
- 拉取频道最近消息
- 强类型事件系统与错误体系
Install
npm install yunjia-chat-sdkQuick Start
import { YunjiaChatSDK } from "yunjia-chat-sdk";
const sdk = new YunjiaChatSDK({
idmBaseUrl: "https://passport.example.com",
clientId: "your_client_id",
clientSecret: "your_client_secret",
username: "robot_account",
password: "******",
tenantId: "default",
socketIoMode: "v2",
autoConnect: true
});
sdk.on("ready", (session) => {
console.log("SDK ready", session.enterpriseId, session.chatClientId);
});
sdk.on("message", (message) => {
const channelId = message.body?.channel;
const text = message.body?.content?.text;
console.log("incoming", channelId, text);
});
sdk.on("error", (error) => {
console.error("sdk error", error);
});
await sdk.initialize();
await sdk.refreshChannels();
sdk.sendTextDirect({
channelId: "CHANNEL_ID",
text: "你好,我是机器人"
});Initialize with Existing Tokens
const sdk = new YunjiaChatSDK({
idmBaseUrl: "https://passport.example.com",
clientId: "your_client_id",
clientSecret: "your_client_secret",
tenantId: "1001",
tokens: {
accessToken: "ACCESS_TOKEN",
refreshToken: "REFRESH_TOKEN"
}
});
await sdk.initialize();API Overview
Required options:
idmBaseUrlclientIdclientSecretinitialize()connect()/disconnect()/close()refreshChannels()createDirectChannel(mateUserId)sendTextDirect(options)sendTextGroup(options)sendTaskCardMessage(options)recallMessage(options)markChannelRead(options)requestChannelRecentMessages(options)refreshAccessToken()getSession()
Events
readyconnecteddisconnectedreconnectingmessagehistoryackstatustokenRefreshederrorraw
Build & Test
npm run build
npm run test
npm run typecheckPublish
npm publish --access public