Whatsapp 机器人 Flutter

whatsapp_bot_flutter_logo

whatsapp_bot_flutter version

使用 WhatsApp Web 抓取技术的 WhatsApp 机器人

入门

我们也可以在 Flutter 和纯 Dart 项目中使用此库,请查看 Dart 示例

首次启动将需要一些时间

它将本地下载 chromium 文件,使用 puppeteer 进行 WhatsApp Web 和数据抓取

MacOS 设置

为 macOS 启用出站和入站连接,并禁用沙盒模式,请在 macos/Runner/*.entitlements 中注释掉此项

<key>com.apple.security.app-sandbox</key>
<true/>

特点

支持的 WhatsApp 功能有

  • 使用二维码登录
  • 登出
  • 保持会话
  • 发送文本消息
  • 发送图片、音频和文档
  • 监听新消息
  • 监听连接事件

用法

首先使用 WhatsappBotFlutter.connect 方法连接 WhatsApp,我们可以从 onQrCode 回调中获取二维码,如果我们得到 onSuccess,这意味着我们已连接并准备好发送消息

要将 qrCode 字符串转换为 QrCode 小部件进行扫描,请使用 pretty_qr_code,更多详情请查看示例

WhatsappBotFlutter.connect(
  onQrCode: (String qr) {
    // there we will get QrCode string use any library to convert string to qrcode and scan
  },
  onError: (String er) {
    // listen for errors
  },
  onSuccess: () {
    // if we received this callback , it means we are connected to whatsapp
  },
  progress: (int prg) {
    // we can listen for progress update
  },
);

使用 sendTextMessage 发送文本消息

WhatsappBotFlutter.sendTextMessage(
    countryCode: "91",
    phone: "------",
    message: "Test Message",
);

使用 sendFileMessage 发送文件

await WhatsappBotFlutter.sendFileMessage(
    countryCode: "91",
    phone: "------",
    fileBytes: imageBytes, // Pass file bytes
    caption: "Test Message", // Optional
    fileType: fileType, // document, image, audio
  ;

要获取新消息,请订阅 WhatsappBotFlutter.messageEvents

WhatsappBotFlutter.messageEvents.listen((Message message) {
  // Got whatsapp messages ...
});

要获取 WhatsApp 连接事件,请订阅 WhatsappBotFlutter.connectionEventStream

WhatsappBotFlutter.connectionEventStream.listen((event) {
  // Got Connection Events ...
});

资源

感谢 wa-js 将函数从 WhatsApp Web 导出

免责声明

本项目不隶属于、关联、授权、认可 WhatsApp 或其任何子公司或关联公司,或以任何方式与它们有官方联系。官方 WhatsApp 网站可在 https://whatsapp.com 找到。“WhatsApp”以及相关名称、标记、徽章和图像是其各自所有者的注册商标。

笔记

这只是初始版本,正在大力开发中,我不能保证您使用此方法不会被封锁,尽量避免使用主要 WhatsApp 号码。WhatsApp 不允许在其平台上使用机器人或非官方客户端,因此这不应被认为完全安全。

GitHub

查看 Github