Busy Bot
Busy bot sends an auto response message for you when you are busy.
Try out the bot
You can try out the Wechaty Busy bot using this interactive CodeSandbox.
Just scan the generated QR code with WeChat app, and you are ready to play with the bot!
Requirements
- Node.js v16+
- Wechaty Puppet Service TOKEN (if you want to use RPA protocols other than Web)
Getting Started
Before getting started make sure you have Node
installed on your system. If you do not have Node.js
installed (or have a version below 12),
then you need to install the latest version of Node.js
by following the links below:
Installation guide for
Node.js
in other platforms can be found here.
You can head over to Building the bot section to learn how to build the bot on your own.
Otherwise if you want to try out the bot on your local system, follow the steps below:
1. Clone the repository
Use the following command to clone the Github repository and navigate to the directory.
git clone https://github.com/wechaty/wechaty-getting-started.git
cd wechaty-getting-started
2. Install dependencies
Install the npm
packages required for running the bot using the following command:
npm install
3. Run the bot
You have to export/set environment variables.
- Linux
- macOS
- Windows
export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-wechat
export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-wechat
set WECHATY_LOG=verbose
set WECHATY_PUPPET=wechaty-puppet-wechat
There are various Wechaty puppets available, you can know more about them here
For running the bot, use the following command:
npx ts-node examples/advanced/busy-bot.js
This will generate a QR code. Scan it using Wechat/Whatsapp and you are ready to go.
Building the bot
Let's get started with building busy-bot using Wechaty.
1. Initialize project
Create a new folder called busy-bot
and move into that directory.
mkdir busy-bot
cd busy-bot
Use the following command to initialize an npm project
npm init -y
2. Install dependencies
For building the busy bot, you will require these dependencies:
- wechaty: Official Wechaty package
- qrcode-terminal: Displays the QR code
For installing these dependencies run the following commands:
- For installing wechaty
npm install wechaty
- For installing qrcode-terminal
npm install qrcode-terminal
You will also need to add dependencies for using any Wechaty Puppet which helps to integrate Wechaty with various instant messaging (IM) systems (such as WeChat, WhatsApp, and WeCom):
If you want to use WhatsApp, install
wechaty-puppet-whatsapp
:npm install wechaty-puppet-whatsapp
If you want to use WeChat, you can try the following puppets:
Web Protocol: Install
wechaty-puppet-wechat
:npm install wechaty-puppet-wechat
iPad Protocol:
padlocal: Install
wechaty-puppet-padlocal
:npm install wechaty-puppet-padlocal
Then get a token like
puppet_padlocal_XXX
, know more about puppet service padlocal here.paimon: Install
wechaty-puppet-service
:npm install wechaty-puppet-service
Then get a token like
puppet_paimon_XXX
, know more about puppet service paimon here.
If you want to use WeCom, install
wechaty-puppet-service
:npm install wechaty-puppet-service
Then get a token like
puppet_wxwork_XXXXX
, more about puppet service wxwork here.
You can find more information about the puppets here.
Now, you are ready to write main code for bot.
3. Writing code for bot
Create a new file busy-bot.js
. You will be writing code here.
Let's import required packages in busy-bot.js
and initialize the bot by providing it a name and puppet to be used.
import { Wechaty, log } from 'wechaty';
import qrTerm from 'qrcode-terminal';
const bot = new Wechaty({
name: "busy-bot",
})
Assigning proper functions to call when an event is triggered.
bot
.on("scan", (qrcode, status) => {
qrTerm.generate(qrcode, { small: true });
console.log(`${status}: ${qrcode} - Scan QR Code of the url to login:`);
})
.on("logout", (user) => log.info("Bot", `${user.name()} logouted`))
.on("error", (e) => log.info("Bot", "error: %s", e))
.on("login", async function (user) {
const msg = `${user.name()} logined`;
log.info("Bot", msg);
await this.say(msg);
});
- When
scan
is triggered, it generates QR code. logout
will display the name of the user along with the statuslogouted
.error
is used to notify if the bot encounters an error.login
will display status logined if a user has logged in.
Global Event: message
This event handles auto response for you depending on the text filehelper
has recieved. You have to do the following configurations for the filehelper:
- '#busy' - set busy mode ON
- '#busy I'm busy' - set busy mode ON and set a Auto Reply Message
- '#free' - set busy mode OFF
- '#status' - check the current Busy Mode and Auto Reply Message.
let busyIndicator = false;
let busyAnnouncement = `Automatic Reply: I cannot read your message because I'm busy now, will talk to you when I get back.`;
bot.on("message", async function (msg) {
log.info("Bot", "(message) %s", msg);
const filehelper = bot.Contact.load("filehelper");
const sender = msg.from();
const receiver = msg.to();
const text = msg.text();
const room = msg.room();
if (!sender || !receiver) {
return;
}
if (receiver.id === "filehelper") {
if (text === "#status") {
await filehelper.say("in busy mode: " + busyIndicator);
await filehelper.say("auto reply: " + busyAnnouncement);
} else if (text === "#free") {
busyIndicator = false;
await filehelper.say("auto reply stopped.");
} else if (/^#busy/i.test(text)) {
busyIndicator = true;
await filehelper.say("in busy mode: ON");
const matches = text.match(/^#busy (.+)$/i);
if (!matches || !matches[1]) {
await filehelper.say('auto reply message: "' + busyAnnouncement + '"');
} else {
busyAnnouncement = matches[1];
await filehelper.say('set auto reply to: "' + busyAnnouncement + '"');
}
}
return;
}
if (sender.type() !== bot.Contact.Type.Personal) {
return;
}
if (!busyIndicator) {
return; // free
}
if (msg.self()) {
return;
}
/**
* 1. Send busy anoncement to contact
*/
if (!room) {
await msg.say(busyAnnouncement);
return;
}
/**
* 2. If there's someone mentioned me in a room,
* then send busy announcement to room and mention the contact who mentioned me.
*/
const contactList = await msg.mention();
const contactIdList = contactList.map((c) => c.id);
if (contactIdList.includes(this.userSelf().id)) {
await msg.say(busyAnnouncement, sender);
}
});
Use the following for starting the bot:
bot.start().catch((e) => console.error(e));
4. Running the bot
In order to run the bot, you have to export/set environment variables with the type of puppet you want to use.
- Linux
- macOS
- Windows
export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-wechat
export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-wechat
set WECHATY_LOG=verbose
set WECHATY_PUPPET=wechaty-puppet-wechat
Run the bot using the following command.
npx ts-node busy-bot.js
Scan the generated QR code with Wechat app and you are ready to play with the bot.