Using node to develop WeChat group chat robot Chapter ①

WeChat group chat robot demonstration

PART sequence

    1. The blog post will be first published on the official account: " Programmer Wild Area ". If you get stuck, you can go to the comment area at the bottom of the official account to ask. Chapter 1 of Developing a WeChat Group Chat Robot Using Node . The link to the next chapter will also be posted on the official account article. Comment area at the bottom

    2. This article mainly describes how to run the wechaty service locally, access personal WeChat, request third-party APIs, and where to find third-party free APIs.

PART description

    A WeChat group chat robot actually uses a personal WeChat ID as a robot, and then others can @ this robot + keywords to trigger the robot's automatic reply. On your node, you can perform requests to third parties based on the keywords sent by the user. operate.

    For example, when a user enters "Xiamen weather", after your node service receives the keyword, it calls the third-party API. To put it bluntly, Ajax goes to http to request a free or paid third-party open web API. You are returning the res After the data is processed, it is sent to the group chat robot, and the group chat robot sends the process in the group using your personal WeChat.

    Use your imagination! This means access to third-party AI (large language model dialogue capabilities), such as Baidu's Wen Xin Yi Yan, Alibaba's Tong Yi Qian Wen (as of the blogger's test, currently only supports py and java SDK calls, it should be available later) out of webapi), iFlytek's Mars AI (the blogger has tested it, and currently it can only be called in the form of ws, which is a long link, that is, a message push call), and of course the famous AI (which is not allowed to be used in China, and is also sensitive words, so the blogger won’t type them)

PART Regarding the solution that the official website is in English

1. Open the official document

Official website address: https://wechaty.js.org/docs/api/

You will find out! I***, pure English? What f***, what does this make people like this blogger who only rely on 45 points out of 150 points in English think? Then the Chinese version option on the official website looks like a decoration. There is no response when I click on it. What should I do?

method one:

Next web page translation software, ===》https://transmart.qq.com/zh-CN/download

The download is a compressed package. After decompression, there is a .crx file. Drag it into the browser and the installation will be successful.

Then you have to open wechaty’s official website https://wechaty.js.org/docs/api/

Click Open, and the translation plug-in on the right will appear. Some friends may have no response after clicking it, so they need to refresh the page and click the plug-in again.

Then you will get a Chinese web page.

Method Two:

Go directly to the Chinese documentation of the lower version of wechaty: https://wechaty.gitbook.io/wechaty/v/zh/

Note that this is a lower version. So some are inconsistent with the official website, and some are abandoned methods!

The blogger was afraid that one day this document would be gone, so he created an offline version of HTML. Friends can also run WeChat mini programs to assign link downloads ===》"Practical Resources for Programmers"

PART Start Tutorial

Okay, pay attention, now let’s get to the point. What I’m interested in is how to use the robot to reply to the first sentence in the WeChat group chat as quickly as possible, which is "hello world". Then pay attention to the configuration mentioned by the following bloggers

Operating environment:

        You'd better use win10 operating system, because win10 can install nvm, which is the node version management system nvm. The blogger of the tutorial wrote a blog ====》 https://blog.csdn.net/xuelang532777032/article/details/128301982

        So why is it best to use win10? Because wechaty needs to run on node>16.x version. The win version of nvm was first ported from mac. The blogger installed it on win7. Sometimes it works, but it doesn't work the next day. However, installing nvm under win10 is stable and possible. Another one, consider your own company's old projects such as vue2, which may use node12.x node 14.x version. If you have not installed nvm, you have to uninstall the local node12.x and reinstall a node16.x version.

        `Attention here! The blogger’s local node version is version 18.12.1! ! ! ! !

In order to prevent some friends from following this blog post and still not being able to run it, it is best to keep the same node version as the blogger. Let’s talk about the key points next.

    1. Ensure local node version: 18.12.1

    2. Save the blogger’s blog post first. We will use node debugging later https://blog.csdn.net/xuelang532777032/article/details/132587083

    3. cnpm must be installed! Don't say that you can translate it locally, or point to other domestic mirrors, it's useless. Wechaty has tried it before, and the dependency update is also incomplete, so you'd better have cnpm pointing to the Taobao mirror. (This sentence is for back-end partners to get started. Front-end friends can ignore this article.)

npm install cnpm -g --registry=https://registry.npm.taobao.org

Start :

1. Create two new folders index.js and package.json with the following codes:

package.json

{
   
     "type": "module",  "dependencies": {
   
       "axios": "^1.4.0",    "qrcode": "^1.5.3",    "wechaty": "^1.20.2",    "xlsx": "^0.18.5",  "file-box": "^1.4.15"  }}

index.js

import {
   
     WechatyBuilder} from 'wechaty'const wechaty = WechatyBuilder.build() // get a Wechaty instancewechaty  .on('scan', (qrcode, status) => console.log(    `Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`))  .on('login', user => console.log(`User ${user} logged in`))  .on('message', message => console.log(`Message: ${message}`))wechaty.start()

2. Open the address chrome://inspect/#devices in Google Chrome (blogger’s Google Chrome version: 116.0.5845.180. Of course, you don’t have to use Google. Google-based browsers like the 360 ​​series browsers can also be used. It’s just to make it easier for you to run it all at once and not get stuck on this or that. Try to use Google Chrome like the blogger. There is no requirement for the Google version)

3. Enter the command in the folder where you created index.js and package.json to run node (front-end students can fast forward, the group owner wrote it more carefully in order to take care of the back-end friends who have not had much contact with the front-end, and published it on a public account It can only be modified once.)

cnpm install

4. Run in debug mode

node --inspect-brk index.js

Notice! After entering this command, the breakpoint of your Google Chrome will be hit, as shown in the picture below. If you are hit by the breakpoint, remember to click Next to let it skip. Some friends are not hit by the breakpoint, so don't worry about them.

After skipping the breakpoint, you need to copy the http: address that pops up as shown below and open it in the browser

Then you will get such a QR code (actually, this address is just a function of converting a URL into a QR code. Later in the article, I will teach you how to directly generate a scannable code locally without jumping to this website. QR code)

5. Use your personal WeChat code to scan the code to log in (note, it is best to use your trumpet account!!! Otherwise, there is a risk of being blocked!)

After logging in, you will find that you will start printing the chat history you received.

    ​​Okay, at this point, you can already receive group information. The next step is to associate with the WeChat group and send hello world. The latest tutorial link will be posted by the blogger in the comment area below this blog post. Everyone. You can see the article by sliding it to the comment area.

    In order to prevent any mistakes, if this blog post is modified, the latest modified blog post will also be put in the comment area under the public account: " Programmer Wild Area ". So friends, come in and check the comment area below the article to see if there is the latest version.

Guess you like

Origin blog.csdn.net/xuelang532777032/article/details/132835674