GeWe Puppet for Wechaty
npm install @re-ai/wechaty-puppet-gewebash
npm install @re-ai/wechaty-puppet-gewe
`
更新日志
使用方法
`typescript
import { WechatyBuilder } from 'wechaty';
import { PuppetGeWe } from '@re-ai/wechaty-puppet-gewe';
import * as qrcode from 'qrcode-terminal';
import { types } from 'wechaty-puppet';
import { FileBox } from 'file-box';
// 创建 Puppet 实例
const puppet = new PuppetGeWe({
name: 'gewe-bot',
token: '你的GeWe Token',
webhookUrl: 'http://your-domain.com',
webhookPort: 8878, // 如果有公网地址,可以设置webhook
debug: true, // 开启调试模式
// SQLite 配置(可选)
sqliteConfig: {
dbPath: './data', // 数据库文件存储目录,会自动添加wxid作为文件名
useWAL: true, // 启用WAL模式提高性能
inMemory: false // 是否使用内存数据库(仅用于测试)
},
});
// 创建 Wechaty 实例
const bot = WechatyBuilder.build({
name: 'gewe-bot',
puppet,
});
// 注册扫码事件处理函数
bot.on('scan', (qrcodeUrl: string, status: types.ScanStatus) => {
console.log(收到二维码,状态: ${status});
// 在控制台显示二维码
if (qrcodeUrl) {
if (qrcodeUrl.startsWith('data:image')) {
console.log('请扫描二维码登录 (请在微信中扫码)');
} else {
qrcode.generate(qrcodeUrl, { small: true });
console.log('请扫描二维码登录 (请在微信中扫码)');
}
}
});
// 注册登录事件处理函数
bot.on('login', async (user) => {
console.log(用户 ${user.name()} (${user.id}) 已登录);
const avatar = await user.payload?.avatar;
console.log(用户头像: ${avatar});
});
// 注册消息事件处理函数
bot.on('message', async (message) => {
console.log(收到消息: ${message.text()}, message.type());
// 如果收到 "ping",回复 "pong"
if (message.text() === 'ping') {
await message.say('pong');
return;
}
// 如果收到 "图片",发送一张图片
if (message.text() === '图片') {
const fileBox = FileBox.fromUrl(
'https://wechaty.js.org/img/wechaty-logo.svg',
'wechaty-logo.svg',
);
await message.say(fileBox);
return;
}
});
// 注册好友请求事件处理函数
bot.on("friendship", async (friendship) => {
console.log(收到好友请求: ${friendship.hello()});
friendship.accept();
});
// 启动机器人
bot.start()
.then(() => console.log('机器人已启动...'))
.catch(console.error);
`
配置选项
| 选项 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| token | string | 是 | GeWe API 访问 token |
| appId | string | 否 | 保存的appId,用于重复登录 |
| regionId | string | 否 | 微信登录地区ID,默认为440000(广东省) |
| webhookUrl | string | 否 | Webhook 回调地址,用于接收 GeWe 平台的事件通知 |
| webhookPort | number | 否 | Webhook 服务器端口,默认为 8878 |
| timeout | number | 否 | API 请求超时时间(毫秒),默认为 30000 |
| retries | number | 否 | API 请求重试次数,默认为 3 |
| debug | boolean | 否 | 是否开启调试模式,默认为 false |
| sqliteConfig | object | 否 | SQLite 数据库配置,详见下表 |
$3
| 选项 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| dbPath | string | 否 | 数据库文件路径,默认为 './gewe-store.db' |
| useWAL | boolean | 否 | 是否启用WAL模式,默认为 true |
| inMemory | boolean | 否 | 是否在内存中运行数据库(用于测试),默认为 false |
功能
- 消息收发(文本、图片、文件等)
- 联系人管理
- 群组管理
- 媒体文件处理
- 好友请求处理
开发
`bash
安装依赖
npm install
开发模式运行
npm run dev
构建
npm run build
运行示例
npm start
``