Build a WeChat Chatbot with Wechaty: Step‑by‑Step Node.js Guide
This guide explains how to create a WeChat chatbot using the open‑source Wechaty SDK, covering required npm packages, QR code login, bot initialization, message sending, and important considerations such as Node.js version and npm mirror configuration.
Background
It is well known that sending robot messages in DingTalk groups is easy, but in WeChat it is difficult because WeChat does not support it natively. However, a WeChat chat robot can be built using tools; this article introduces using Wechaty to implement a WeChat chatbot.
Wechaty is an open‑source chatbot project that provides an SDK for quickly developing chatbots. Through Wechaty you can obtain chat content, contacts, groups, friend relationships, and also create groups and send messages.
Implementation
Wechaty adds a layer of abstraction over WeChat, providing APIs that make it easy to implement a chatbot. Before writing code, install the following npm packages:
wechaty wechaty-puppet-wechat(uses the web version of WeChat)
qrcode-terminal(displays QR code in the console)
First, create a chatbot. Wechaty launches a Puppeteer instance and opens the web version of WeChat; scan the QR code displayed by Wechaty with the mobile WeChat app to log in. After a successful login, various operations can be performed via Wechaty.
<code>const qrTerm = require('qrcode-terminal');
const Wechaty = require('wechaty');
const { ScanStatus, WechatyBuilder, log } = Wechaty;
function onScan (qrcode, status) {
if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
qrTerm.generate(qrcode, { small: true }); // show qrcode on console
const qrcodeImageUrl = [
'https://wechaty.js.org/qrcode/',
encodeURIComponent(qrcode),
].join('');
log.info('StarterBot', 'onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl);
} else {
log.info('StarterBot', 'onScan: %s(%s)', ScanStatus[status], status);
}
}
// get a Wechaty instance
const bot = WechatyBuilder.build({
name: 'wechat-bot',
puppet: 'wechaty-puppet-wechat',
})
// emit when the bot needs to show you a QR Code for scanning
bot.on('scan', onScan);
// start the bot
bot.start()
.then(() => log.info('StarterBot', 'Starter Bot Started.'))
.catch(e => log.error('StarterBot', e));
</code>Then implement message sending. After the chatbot starts successfully, you can send messages with the bot as follows:
<code>// find a room
const room = await bot.Room.find({ topic: 'yours-wechat-group-name' })
if (room) {
// send a message
await room.say(`hello world`)
}
</code>Using Wechaty to build a WeChat chatbot is convenient, but note two points:
Wechaty requires Node.js version 16 or higher; lower versions will cause errors.
If npm package downloads are slow, you can set the following mirrors:
<code>npm config set puppeteer_download_host "https://npm.taobao.org/mirrors"
npm config set sharp_binary_host "https://npm.taobao.org/mirrors/sharp"
npm config set sharp_libvips_binary_host "https://npm.taobao.org/mirrors/sharp-libvips"
</code>Summary
As private‑domain traffic becomes more familiar, personal‑WeChat community management tools have broad use cases; Webchaty provides a solution that makes implementing related business functions easier.
KooFE Frontend Team
Follow the latest frontend updates
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.