chat 协议 API SDK
npm install @wxbot/wechat-api-sdkbash
npm install wechat-api-sdk
`
或使用 yarn:
`bash
yarn add wechat-api-sdk
`
🚀 快速开始
$3
`typescript
import { wechatApi, ProtocolType } from 'wechat-api-sdk';
// 初始化API实例
const api = wechatApi(
'http://your-api-url:port', // GeWe API服务地址
ProtocolType.WX_GeWe, // 协议类型
'your-token' // 可选的认证token
);
// 使用API
const result = await api.login.getLoginQrCode({});
console.log(result);
`
$3
`typescript
import { wechatApi } from 'wechat-api-sdk';
// protocolType 参数可省略,默认使用 GeWe
const api = wechatApi('http://localhost:8080', undefined, 'token');
`
📚 API文档
API实例提供以下模块:
$3
处理微信账号的登录、心跳、在线状态等功能。
`typescript
// 获取登录二维码
await api.login.getLoginQrCode({});
// 检查登录状态
await api.login.getLoginQrStatus({});
// 检查在线状态
await api.login.checkOnline({});
// 心跳
await api.login.heartBeat({});
// 登出
await api.login.logOut({});
`
$3
发送各类消息和同步消息。
`typescript
// 发送文本消息
await api.message.sendTxt({
toWxid: 'wxid_xxx',
content: 'Hello World'
});
// 发送图片
await api.message.sendImage({
toWxid: 'wxid_xxx',
imagePath: '/path/to/image.jpg'
});
// 发送文件
await api.message.sendFile({
toWxid: 'wxid_xxx',
filePath: '/path/to/file.pdf'
});
// 同步消息
await api.message.syncMessage({});
// 撤回消息
await api.message.revokeMsg({
msgId: 'message_id'
});
`
$3
管理好友相关操作。
`typescript
// 获取联系人列表
await api.friend.getContactList({});
// 获取联系人详情
await api.friend.getContactDetail({
wxid: 'wxid_xxx'
});
// 搜索联系人
await api.friend.searchContact({
keyword: 'nickname'
});
// 通过好友验证
await api.friend.passVerify({
v3: 'v3_xxx',
v4: 'v4_xxx'
});
`
$3
管理微信群聊操作。
`typescript
// 获取群成员列表
await api.chatRoom.getChatroomMemberList({
chatRoomId: 'chatroom_id'
});
// 获取群成员信息
await api.chatRoom.getChatroomMemberInfo({
chatRoomId: 'chatroom_id',
wxid: 'wxid_xxx'
});
// 获取群二维码
await api.chatRoom.getChatroomQRCode({
chatRoomId: 'chatroom_id'
});
`
$3
提供文件上传下载、Webhook配置等工具功能。
`typescript
// 上传文件
await api.tools.uploadFile({
filePath: '/path/to/file'
});
// 下载图片
await api.tools.downloadImg({
aesKey: 'key',
url: 'image_url'
});
// 设置Webhook
await api.tools.setWebhook({
url: 'http://your-webhook-url',
token: 'your-token'
});
`
🔔 事件监听
SDK内置事件总线,可以监听消息事件:
`typescript
import { eventBus } from 'wechat-api-sdk';
// 监听新消息
eventBus.on('newMessage', (msgData) => {
console.log('收到新消息:', msgData);
// 处理消息逻辑
});
// 取消监听
eventBus.off('newMessage');
`
🏗️ 项目结构
⚙️ 开发
`bash
克隆项目
git clone
安装依赖
npm install
构建
npm run build
监听模式构建
npm run build:watch
清理
npm run clean
``