How to use node to build a WeChat personal account robot
This time I will show you how to use node to build a WeChat personal account robot. What are the precautions for using node to build a WeChat personal account robot. The following is a practical case, let's take a look.
Nowadays, daily life is inseparable from WeChat. This article will give you a clue and demonstrate how to use wechaty to operate your WeChat personal account to do some interesting things. It can automatically implement friend requests and keyword replies , automatic group pulling and other functions. Greatly improved the efficiency of community operations.
wechaty (https://chatie.io/wechaty/) is an open source WeChat personal account SDK. It carries out a series of packages and provides simple and easy-to-use interfaces. Then Developers can develop WeChat robots on top of it. You can use it to do many things:
1) Manage and maintain friends; 2) Process chat messages quickly and orderly 3) WeChat group management 4) Implement automatic replies 5)...
1. Get started quickly
wechaty The project homepage says 6 lines of code to create a WeChat robot, so let’s give it a try. Note: Node.js version >= 8.5
The World's Shortest ChatBot Code: 6 lines of JavaScript
const { Wechaty } = require('wechaty') Wechaty.instance() // Singleton .on('scan', (url, code) => console.log(`Scan QR Code to login: $[code]\n${url}`)) .on('login', user => console.log(`User ${user} logined`)) .on('message', message => console.log(`Message: ${message}`)) .start()
These 6 lines of code implement logging in to the WeChat personal account and Print out received messages.
wechaty can be run through Docker and NPM. This article uses the npm method
node bot.js to start this personal account robot. At this time, the command line will print out a link. We will Open it in the browser and you will see a QR code. Scan the code to log in (Note: At the end of 2017, as WeChat gradually shut down support for WeChat web version login, WeChat accounts registered later do not support web WeChat login. I spent 105 RMB to buy an account and felt sorry for my wallet for 3 seconds)
User Contact<刘星> logined // 扫码登录 Message: Message<JavaScript之禅> // 接收微信消息
The robot experience is at the end of the article. Sample code address: Github
2. Automatically pass friend requests
Through the previous sample code, you can find out how simple and powerful this library is. . Then let's implement the first small function: automatically request through friends
//··· .on('friend', async (contact, request) => { if (request) { if (/JavaScript|JS/i.test(request.hello)) { logMsg = 'accepted because verify messsage is "JS之禅"' request.accept() } else { logMsg = 'not auto accepted, because verify message is: ' + request.hello } } else { logMsg = 'friend ship confirmed with ' + contact.get('name') } }) //···
Here we listen to the friend event. You can get who sent what friend request. Here we have verified the request when adding friends. When the content of the friend request is [JS] or [JavaScript], the friend request will be automatically passed.
Copying the link to the browser and opening the QR code every time is not cool at all and it is not convenient to use on the server. At this time we can use the qrcode-terminal package to print out the QR code directly on the command line.
//··· const QrcodeTerminal = require('qrcode-terminal') //··· .on('scan', (url, code) => { let loginUrl = url.replace('qrcode', 'l') QrcodeTerminal.generate(loginUrl) console.log(url) }) //···
Run the node bot.js command line and the QR code will be printed directly, which is much more convenient.
3. Automatic reply
Now let’s implement the second small function: keyword automatic reply.
//··· .on('message', async (message) => { const contact = message.from() const content = message.content() const room = message.room() if (room) { console.log(`Room: ${room.topic()} Contact: ${contact.name()} Content: ${content}`) } else { console.log(`Contact: ${contact.name()} Content: ${content}`) } // 不处理自己发的消息 if (message.self()) { return } if (/JavaScript|Js|js/.test(content)) { message.say('关注公众号 JavaScript之禅') } }) //···
Above we listen to message for message processing. message can receive all messages. We can determine whether it is a group chat message and reply with corresponding content based on the message. If you send [js], you will receive the set message and realize automatic reply.
4. Automatically pull groups
#As mentioned earlier, we mentioned the automatic reply of message keywords. By combining message processing and group management, we can Automatically attract groups based on keywords
//··· .on('message', async (message) => { const contact = message.from() const content = message.content() const room = message.room() if (room) { console.log(`Room: ${room.topic()} Contact: ${contact.name()} Content: ${content}`) } else { console.log(`Contact: ${contact.name()} Content: ${content}`) } if (message.self()) { return } if (/JavaScript|Js|js/.test(content)) { message.say('关注公众号 JavaScript之禅没?') } // 自动拉群 if (/加群/.test(content)) { let keyroom = await Room.find({ topic: 'test' }) if (keyroom) { await keyroom.add(contact) await keyroom.say('欢迎新朋友!', contact) } } }) //···
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
##How to use vue to determine the class of dom
The above is the detailed content of How to use node to build a WeChat personal account robot. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

How to delete node with nvm: 1. Download "nvm-setup.zip" and install it on the C drive; 2. Configure environment variables and check the version number through the "nvm -v" command; 3. Use the "nvm install" command Install node; 4. Delete the installed node through the "nvm uninstall" command.

1. First, open the enterprise WeChat software you downloaded on your mobile phone. When logging in, there are two ways to choose: one is to use WeChat ID, the other is to use mobile phone number. 3. At this time, the enterprise administrator needs to add your mobile phone number in the background, and then Enterprise WeChat will identify the enterprise based on your mobile phone number. Then display your business and click the Enter Business option below. 4. Then you can enter the use of functions in the software. It can be said that the most important thing is that your mobile phone number must be added to the enterprise by the administrator, otherwise it will not be available.

This article will share with you Node's process management tool "pm2", and talk about why pm2 is needed, how to install and use pm2, I hope it will be helpful to everyone!

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

npm node gyp fails because "node-gyp.js" does not match the version of "Node.js". The solution is: 1. Clear the node cache through "npm cache clean -f"; 2. Through "npm install -g n" Install the n module; 3. Install the "node v12.21.0" version through the "n v12.21.0" command.

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to
