Building WhatsApp Automation with Baileys: What We Learned
Building WhatsApp Automation with Baileys: What We Learned
使用 Baileys 构建 WhatsApp 自动化:我们的经验总结
When building a WhatsApp automation product for businesses (Argonova), the core technical decision we faced was: official WhatsApp Business API, or an open-source library like Baileys? Here’s why we currently use Baileys, how it works under the hood, and why we’re building an official API version next.
在开发一款面向企业的 WhatsApp 自动化产品(Argonova)时,我们面临的核心技术决策是:选择官方的 WhatsApp Business API,还是像 Baileys 这样的开源库?在这篇文章中,我将分享我们目前选择 Baileys 的原因、它的工作原理,以及我们未来为何会转向官方 API。
What Is Baileys?
什么是 Baileys?
Baileys is an open-source library that reverse-engineers the WhatsApp Web protocol, letting you drive a WhatsApp Web session from Node.js. It’s not an official Meta product — it essentially lets you control WhatsApp Web programmatically, the same way your browser would.
Baileys 是一个通过逆向工程解析 WhatsApp Web 协议的开源库,它允许你通过 Node.js 驱动 WhatsApp Web 会话。它并非 Meta 的官方产品,本质上是让你能够像浏览器一样,以编程方式控制 WhatsApp Web。
How it works, at a high level:
- Connects to WhatsApp Web’s servers over a WebSocket
- Authenticates via QR code (or pairing code) — the same flow as scanning a QR code with your phone in a browser
- Once authenticated, you can send/receive messages, handle media, and manage groups programmatically
其基本工作原理如下:
- 通过 WebSocket 连接到 WhatsApp Web 服务器。
- 通过二维码(或配对码)进行身份验证——这与你在浏览器中用手机扫描二维码的流程完全一致。
- 身份验证成功后,即可通过代码实现发送/接收消息、处理媒体文件以及管理群组等操作。
Why We Started with Baileys
为什么我们选择从 Baileys 开始?
-
Fast prototyping: The official API’s approval, business verification, and integration process can take weeks. With Baileys, we had a working prototype in minutes.
-
Cost predictability: The official API charges per message, which was hard to forecast at an early stage.
-
Flexibility: Some behaviors the official API restricts (certain message formats, group interactions) were easier to test and iterate on with Baileys.
-
快速原型设计: 官方 API 的审批、企业验证和集成流程可能需要数周时间。而使用 Baileys,我们仅需几分钟就能做出一个可运行的原型。
-
成本可控性: 官方 API 按消息收费,这在项目早期很难进行成本预测。
-
灵活性: 官方 API 限制的一些行为(如特定的消息格式、群组交互),使用 Baileys 可以更轻松地进行测试和迭代。
The Trade-offs You Should Know
你需要了解的风险与权衡
To be transparent: Baileys comes with real risks, and I’d encourage anyone considering it to weigh these first.
- Unofficial approach: It can be considered a violation of WhatsApp’s terms of service, carrying a risk of number bans.
- Stability: When WhatsApp Web’s protocol changes, the library needs updates, which means ongoing maintenance overhead.
- Scaling limits: Managing many numbers with high message volume is more fragile compared to the official API.
坦白说,Baileys 确实存在风险,我建议任何考虑使用它的人先权衡以下几点:
- 非官方途径: 这可能被视为违反 WhatsApp 的服务条款,存在账号被封禁的风险。
- 稳定性: 当 WhatsApp Web 协议发生变更时,库需要同步更新,这意味着持续的维护成本。
- 扩展性限制: 与官方 API 相比,在管理大量号码和高频消息发送时,其稳定性较弱。
To mitigate this, we keep our sending patterns closer to human behavior — randomized delays, daily send limits — rather than aggressive bulk sending.
为了降低这些风险,我们将发送模式保持在接近人类行为的水平——例如设置随机延迟和每日发送上限,而不是进行激进的群发。
What’s Next: Official WhatsApp Business API
下一步:官方 WhatsApp Business API
We’re currently building a second version of our product on top of the official WhatsApp Business API. The plan is to offer two tracks:
- Baileys-based version: For smaller businesses that want a fast, low-cost start.
- Official API-based version: For businesses that need enterprise-grade reliability and WhatsApp’s official approval.
目前,我们正在基于官方 WhatsApp Business API 开发产品的第二个版本。我们的计划是提供两条产品线:
- 基于 Baileys 的版本: 适合希望快速、低成本起步的小型企业。
- 基于官方 API 的版本: 适合需要企业级可靠性并希望获得 WhatsApp 官方认证的企业。
I’ll follow up with more technical detail on that migration — webhook architecture, message template approval, and cost comparisons — in a future post. This post is based on real engineering decisions made while building Argonova.
在未来的文章中,我将分享更多关于迁移的技术细节,包括 Webhook 架构、消息模板审批流程以及成本对比。本文基于我们在构建 Argonova 过程中做出的真实工程决策。