0.2.11 • Published 6 months ago

@re-ai/wechaty-puppet-gewe v0.2.11

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

Wechaty Puppet GeWe

这是一个基于 GeWe 云 API 的 Wechaty Puppet 实现,将 GeWe 开放平台的能力与 Wechaty 框架连接,使开发者可以通过统一的 Wechaty 接口操作微信。

安装

npm install @re-ai/wechaty-puppet-gewe

更新日志

使用方法

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);

配置选项

选项类型必填说明
tokenstringGeWe API 访问 token
appIdstring保存的appId,用于重复登录
regionIdstring微信登录地区ID,默认为440000(广东省)
webhookUrlstringWebhook 回调地址,用于接收 GeWe 平台的事件通知
webhookPortnumberWebhook 服务器端口,默认为 8878
timeoutnumberAPI 请求超时时间(毫秒),默认为 30000
retriesnumberAPI 请求重试次数,默认为 3
debugboolean是否开启调试模式,默认为 false
sqliteConfigobjectSQLite 数据库配置,详见下表

SQLite 配置选项

选项类型必填说明
dbPathstring数据库文件路径,默认为 './gewe-store.db'
useWALboolean是否启用WAL模式,默认为 true
inMemoryboolean是否在内存中运行数据库(用于测试),默认为 false

功能

  • 消息收发(文本、图片、文件等)
  • 联系人管理
  • 群组管理
  • 媒体文件处理
  • 好友请求处理

开发

# 安装依赖
npm install

# 开发模式运行
npm run dev

# 构建
npm run build

# 运行示例
npm start

注意事项

  1. 请使用正规注册且已实名认证的微信账号
  2. 避免频繁切换登录设备
  3. 不要使用本项目进行违规操作,以免封号
  4. 合理控制消息发送频率
  5. 避免大量相同内容的消息

许可证

ISC

0.2.11

6 months ago

0.2.10

6 months ago

0.2.9

6 months ago

0.2.8

6 months ago

0.2.7

6 months ago

0.2.6

6 months ago

0.2.5

6 months ago

0.2.4

6 months ago

0.2.3

6 months ago

0.2.2

6 months ago

0.2.1

6 months ago

0.2.0

6 months ago

0.1.9

6 months ago

0.1.8

6 months ago

0.1.7

6 months ago

0.1.6

6 months ago

0.1.5

6 months ago

0.1.3

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.0

8 months ago