How I Built a WhatsApp AI Bot That Runs for $0/Month on Windows

How I Built a WhatsApp AI Bot That Runs for $0/Month on Windows

如何在 Windows 上构建一个每月零成本的 WhatsApp AI 机器人

I wanted a simple WhatsApp AI bot without paying every month for cloud hosting or an AI API. So I built one that runs on a Windows PC I already have running 24/7. The result: 我想要一个简单的 WhatsApp AI 机器人,且不想每月支付云托管或 AI API 的费用。于是,我利用自己那台 24/7 全天候运行的 Windows 电脑构建了一个。其成果包括:

  • WhatsApp integration with Node.js
  • Optional local AI using Ollama
  • No VPS or cloud server required
  • No paid AI API required
  • Runs on Windows 10/11
  • Can restart automatically after a reboot
  • 基于 Node.js 的 WhatsApp 集成
  • 使用 Ollama 实现的可选本地 AI
  • 无需 VPS 或云服务器
  • 无需付费 AI API
  • 可在 Windows 10/11 上运行
  • 重启后可自动恢复运行

«The “$0/month” refers to additional software, hosting, and AI API costs. It assumes you already have the PC, internet connection, and electricity.» “每月零成本”指的是无需额外的软件、托管和 AI API 费用。前提是你已经拥有电脑、网络连接和电力供应。

The basic architecture

基本架构

The setup is intentionally simple: WhatsApp → Node.js bot → Local AI → WhatsApp reply 该设置非常简单:WhatsApp → Node.js 机器人 → 本地 AI → WhatsApp 回复

The Node.js application handles incoming WhatsApp messages and decides how to respond. For AI responses, the bot can send the user’s message to a locally running Ollama model and return the generated answer back to WhatsApp. That gives us: Node.js 应用程序负责处理传入的 WhatsApp 消息并决定如何响应。对于 AI 回复,机器人可以将用户的消息发送到本地运行的 Ollama 模型,并将生成的答案返回给 WhatsApp。流程如下:

WhatsApp → Node.js → Ollama on localhost → Node.js → WhatsApp No cloud AI API is required. WhatsApp → Node.js → 本地 Ollama → Node.js → WhatsApp 无需任何云端 AI API。

What you need

所需准备

For the basic setup:

  • Windows 10 or Windows 11
  • Node.js LTS
  • A WhatsApp account
  • Ollama if you want local AI
  • A computer that can stay powered on 基本设置需求:
  • Windows 10 或 Windows 11
  • Node.js LTS 版本
  • 一个 WhatsApp 账号
  • 如果需要本地 AI,则需安装 Ollama
  • 一台可以保持开机的电脑

You don’t need Kubernetes. You don’t need AWS. You don’t need Docker. And you don’t need to rent a VPS. 你不需要 Kubernetes,不需要 AWS,不需要 Docker,也不需要租用 VPS。

Connecting WhatsApp

连接 WhatsApp

For this project I used “whatsapp-web.js”. The first time the application starts, it displays a QR code. You scan the QR code with WhatsApp, similar to connecting WhatsApp Web. After authentication, the application can listen for incoming messages and send replies. 对于本项目,我使用了 “whatsapp-web.js”。应用程序首次启动时会显示一个二维码。你只需像连接 WhatsApp Web 一样用手机扫描二维码即可。验证完成后,应用程序便可监听传入的消息并发送回复。

A simplified example looks like this: 一个简化的示例如下:

const { Client, LocalAuth } = require('whatsapp-web.js');
const client = new Client({ authStrategy: new LocalAuth() });

client.on('qr', (qr) => {
    console.log('Scan the QR code to connect WhatsApp');
});

client.on('ready', () => {
    console.log('WhatsApp bot is ready');
});

client.on('message', async (message) => {
    if (message.body.toLowerCase() === 'hello') {
        await message.reply('Hello from the bot!');
    }
});

client.initialize();

“LocalAuth” stores the authenticated WhatsApp session locally. That means you normally don’t need to scan the QR code again every time the application restarts. “LocalAuth” 会在本地存储已验证的 WhatsApp 会话。这意味着通常情况下,你不需要在每次应用程序重启时重新扫描二维码。

Protect the WhatsApp session

保护 WhatsApp 会话

The authentication files should be treated like credentials. Do not:

  • Upload them to GitHub
  • Share them with other people
  • Include them in downloadable source packages
  • Commit your “.env” file 验证文件应被视为凭据。请勿:
  • 将其上传到 GitHub
  • 与他人共享
  • 将其包含在可下载的源代码包中
  • 提交你的 “.env” 文件

Add sensitive files and directories to “.gitignore”. For example: 将敏感文件和目录添加到 “.gitignore” 中。例如:

.env
.wwebjs_auth/
.wwebjs_cache/
node_modules/

Adding local AI with Ollama

使用 Ollama 添加本地 AI

The next step is connecting the bot to Ollama. Ollama runs the language model locally on the Windows machine. Instead of calling a paid cloud API, Node.js sends the prompt to Ollama on localhost. 下一步是将机器人连接到 Ollama。Ollama 在 Windows 机器上本地运行语言模型。Node.js 不再调用付费云 API,而是将提示词发送到本地的 Ollama。

A simplified request might look like this: 一个简化的请求示例如下:

async function askOllama(prompt) {
    const response = await fetch('http://localhost:11434/api/generate', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ model: 'llama3.2', prompt, stream: false })
    });
    const data = await response.json();
    return data.response;
}

Then the WhatsApp handler can use it: 然后 WhatsApp 处理程序就可以使用它了:

client.on('message', async (message) => {
    try {
        const answer = await askOllama(message.body);
        await message.reply(answer);
    } catch (error) {
        console.error(error);
        await message.reply('Something went wrong.');
    }
});

Now the flow becomes:

  • A WhatsApp message arrives
  • Node.js receives it
  • Node.js sends the prompt to Ollama
  • Ollama generates the answer locally
  • Node.js sends the answer back through WhatsApp 现在的流程变为:
  • 收到一条 WhatsApp 消息
  • Node.js 接收消息
  • Node.js 将提示词发送给 Ollama
  • Ollama 在本地生成答案
  • Node.js 通过 WhatsApp 将答案发回

Choosing a local model

选择本地模型

The model you can run comfortably depends on the hardware. Smaller models generally:

  • Require less RAM
  • Respond faster
  • Work better on older PCs Larger models can provide better results for some tasks, but they require more resources. For a small personal bot or learning project, starting with a relatively small model is usually the easiest approach. 你能流畅运行的模型取决于硬件配置。较小的模型通常:
  • 占用更少的内存
  • 响应速度更快
  • 在旧电脑上运行效果更好 较大的模型在某些任务中能提供更好的结果,但需要更多资源。对于小型个人机器人或学习项目,从较小的模型开始通常是最简单的方法。

Keeping the bot running

保持机器人运行

During development, you can simply run: node index.js. But that isn’t enough for a machine that should host the bot continuously. You want the application to:

  • Start automatically
  • Restart if it crashes
  • Start again after Windows reboots 在开发过程中,你可以直接运行 node index.js。但这对于需要持续托管机器人的设备来说是不够的。你需要应用程序能够:
  • 自动启动
  • 崩溃后自动重启
  • Windows 重启后自动运行

One option is using PM2: 一种选择是使用 PM2: npm install -g pm2

Then: pm2 start index.js --name whatsapp-ai-bot

You can check its status with: pm2 status And view logs with: pm2 logs whatsapp-ai-bot 你可以通过 pm2 status 查看状态,通过 pm2 logs whatsapp-ai-bot 查看日志。

For a long-running Windows setup, make sure you also configure a reliable startup mechanism so the process returns after a machine reboot. 对于长期运行的 Windows 设置,请确保配置可靠的启动机制,以便在机器重启后进程能自动恢复。

What can you build with it?

你可以用它构建什么?

Once the basic connection works, you can extend the bot in many directions:

  • AI Q&A
  • Group-specific commands
  • Personal assistants
  • Home automation
  • Local knowledge bases
  • API integrations
  • Custom system prompts
  • Different behavior for different groups
  • Message summarization
  • Simple internal tools 一旦基本连接成功,你可以从多个方向扩展机器人:
  • AI 问答
  • 群组特定指令
  • 个人助理
  • 家庭自动化
  • 本地知识库
  • API 集成
  • 自定义系统提示词
  • 针对不同群组的不同行为
  • 消息摘要
  • 简单的内部工具

The WhatsApp connection is really just the interface. The interesting part is what you put behind it. WhatsApp 连接实际上只是一个接口。有趣的部分在于你如何在后端实现功能。

A note about whatsapp-web.js

关于 whatsapp-web.js 的说明

“whatsapp-web.js” is an unofficial integration that works through WhatsApp Web. It is not the official WhatsApp Business Platform. That makes it convenient for experiments and personal projects, but it also means you should understand the trade-offs before using it for a business-critical or production system. For an official commercial integration, the WhatsApp Business Platform is the appropriate route to evaluate. “whatsapp-web.js” 是一个通过 WhatsApp Web 工作的非官方集成,它并非官方的 WhatsApp Business Platform。这使得它非常适合实验和个人项目,但也意味着在将其用于业务关键型或生产系统之前,你需要了解其局限性。对于正式的商业集成,WhatsApp Business Platform 才是应考虑的途径。

Why I built this

我为何构建此项目

I wanted something that didn’t require a cloud account, recurring hosting bill, or complicated infrastructure. A Windows machine that was already online could do the job. For a developer, the setup is relatively straightforward. But there were enough small steps — Node.js setup, WhatsApp authentication, Ollama, configuration, persistent startup, and troubleshooting — that I decided to package the complete process into a beginner-friendly guide. 我想要一个不需要云账号、无需持续支付托管费用、也不需要复杂基础设施的方案。一台已经在线的 Windows 电脑就能胜任。对于开发者来说,设置相对简单。但由于涉及 Node.js 设置、WhatsApp 验证、Ollama、配置、持久化启动和故障排除等多个小步骤,我决定将整个过程打包成一份适合初学者的指南。

Complete step-by-step version

完整的分步指南

I created a downloadable guide that includes the full Windows setup and ready-to-run project files. It covers:

  • Windows setup
  • Node.js installation
  • WhatsApp connection
  • Local authentication
  • Ollama setup
  • Local AI integration 我创建了一份可下载的指南,其中包含完整的 Windows 设置和可直接运行的项目文件。内容涵盖:
  • Windows 设置
  • Node.js 安装
  • WhatsApp 连接
  • 本地验证
  • Ollama 设置
  • 本地 AI 集成