Wechaty Contact bot is a bot which can list all contacts by Wechat ID & Name.
You can try out the Wechaty Contact bot using this interactive CodeSandbox.
Just scan the generated QR code with WeChat app, and you are ready to play with the bot!
You should have
Node.js 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:
Node.js installation docs
You can head over to the Building the bot section to learn how to build the bot on your own.
Otherwise, if you just want to try out the bot on your local system, follow the steps below:
Use the following commands to clone the GitHub repository and navigate to the directory:
You can install the
npm packages required for running the bot, using this command:
First, you have to
export/set the environment variables, and then you can run the bot:
There are various Wechaty puppets available, you can know more about them here.
It will generate a QR code, scan it using WeChat or WhatsApp (according to the puppet you have used), and you are ready to play with the bot.
Let's get started with building the Wechaty Contact bot using Wechaty.
Create a new folder called
contact-bot and move into that directory:
Use the following command to initialize an npm project:
This will generate the
package.json file containing these:
For building the bot mainly you will require the following dependencies:
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 Gitter):
You can add only the puppets that you need for using the bot.
At this point the
package.json file will look like this (a few more required dependencies are added):
Before proceeding further, you should install the dependencies that we just specified in the
package.json file. Run the following command from the root directory:
This will generate a
node_modules directory containing all the installed dependencies, and a
Now, you are ready to start writing the main code for building the bot.
Start by creating a new folder called
src, and add a file
contact-bot.js. We will be writing the code here.
Specify some functions that we will require for handling different events returned by the Wechaty bot.
This function will be used for generating the QR code for the puppet specified, and display it on the console.
This will print a log message when an user logs in to the bot.
This will print a log message when an user logs out of the bot.
This is for printing an error message to the console.
Now, initialize the Wechaty bot by providing a name:
Assign proper function to call when an event is triggered by the bot:
Use the following to start the bot:
Add the main function to print the official and personal contacts list to the terminal.:
You have to define the script for running the bot. Add the following to your
Here, we have specified the script in way so that the type of puppet can be passed as an environment variable.
In order to run the bot, first you have to export/set an environment variable with the type of puppet to use and then run the bot:
This will start the bot and generate a QR code.
Scan it using your WeChat/WhatsApp as per the puppet you have selected, and you are ready to play with the bot!
You can try building this bot without setting up any dev environment on your local system, just head over to this interactive CodeSandbox by clicking the button below (a preview of the starter project is given below):