Using node to develop WeChat group chat robot Chapter 2

​▍ 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. Friends who can take the initiative to open this article mean that  you have already conquered this article " Developing WeChat Group Chat Bots with Node Chapter 1" . Okay, without further ado, let’s start this blog post with hello world.

PART text

The first sentence hello world from the robot

Note that before running the code, you'd better have only one group chat, otherwise whenever you receive a message, you will send a hello world to that group!!!!!!!!!!!!!!!!!!! !

Okay, did you read the note above clearly? Look clearly! ! ! ! !

1. Modify index.js

import {
  WechatyBuilder
} from 'wechaty'
const wechaty = WechatyBuilder.build() // get a Wechaty instance
wechaty
  .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', msg => {
    let text = msg.text()//这是消息内容
    let room= msg.room()//这是房间对象
    if (room && text.length > 0) {//这是代表,接收到了房间发送的消息
      room.say("hello world")//在这个群发送消息
    }
  })
wechaty.start()

You don’t have to rush to read what the blogger has noted. You run it first. If there are results first, I will help you understand them later.

2. Then you run the code (please go here to read this article => Using node to develop a WeChat group chat robot Chapter ① )

node --inspect-brk index.js

3. Scan the QR code to log in. The process is the same as the operation in Chapter 1. You also need to skip the breakpoint (remember to use your WeChat account to scan the QR code to log in).

4. Use your WeChat account to send a random message in the WeChat group, such as 2

You will find that he replied "hello world", which welcomes you and opens the door to a new world, because you already know how to receive information ===》The process of sending messages, don't worry, you now want to know how to send audio and video , pictures, files. Later, when the interface for requesting third-party free https is released, I will attach a case.

Before that, let me first tell you the meaning of the code changed in Chapter 2, which is the following paragraph

Let me explain this idea to you from top to bottom.

.on('message', msg => {
    let text = msg.text()//这是消息内容
    let room= msg.room()//这是房间对象
    if (room && text.length > 0) {//这是代表,接收到了房间发送的消息
      room.say("hello world")//在这个群发送消息
    }
  })

Needless to say, msg is the returned data, followed by an arrow. (This is the basis of the front-end! Back-end friends also have the concept of arrow functions,)

The text variable is the content of the message

The room variable is the room object

This is to send a text message to the current room, which is the room where the message was sent and received.

 room.say("hello world")

The following judgment will only be made when receiving a group chat message, and will not be entered in individual private chats.

if (room && text.length > 0)

Let's change the code so that it only receives messages from a certain group before replying hello world

.on('message', async msg => {
    debugger
    let text = msg.text()//这是消息内容
    let room= msg.room()//这是房间对象
    if (room && text.length > 0 ) {//这是代表,接收到了房间发送的消息
      let topic = await room.topic()
      if(topic=="web前端、后端、ui内推、技术交流"){
        room.say("hello world")//在这个群发送消息
      }
    }
  })

Come and pay attention! ! ! A few changes! ! !

async msg => {
   
   

This is asynchronous, so async is added, in order to cooperate with the subsequent await room.topic()

topic is the name of the group chat. The name of the group chat is as shown below. (Of course, some friends will say that the name of the group chat may be repeated. What should I do? There is the ID of the room, but the blogger tried it, and it seems that The ID will change every time you log out. Later the blogger will think of a way to reply in the comment area of ​​this article)

So in this code, topic=="web front-end, back-end, ui internal push, technical exchange". You remember to replace the blogger's "web front-end, back-end, ui internal push," with the group chat name you created yourself. Technology Exchange"

Then you are running it. There is no need for bloggers to teach you how to run it. Just repeat points 2, 3, and 4 of this blog post, which are the steps as shown below.

Okay, after reading this article, you will know

①How to send hello world?

②How to specify which group chat to reply to?

③ Attached is the document address of the message. Don’t worry, I will teach you how to use it to send videos, files, and pictures where the interface calls pictures and videos later ===》 https://wechaty.js.org/docs/ api/message

The next step is how to request third-party free api interfaces through ajax, and where to find these free and paid third-party api interfaces.

    After I finish writing the tutorial for the next chapter, I will post it to the comment area of ​​the public account " Programmer Wild Zone ". Everyone can check it out by themselves when the time comes.

 ▍ PART public account collection

#人间awake  #front-end memoir   #controversial topic   # programmer dry stuff

PART other articles

  1. When you are admitted because of your looks

  2. When the facts are in your favor, emphasize the facts

  3. Is the web front-end dead? The truth is out.

  4. What should programmers do after turning 35?

  5. What do programmers look for first when looking for a job? boss? project?

  6. Programmers can avoid pitfalls by investing in technical shares

Supongo que te gusta

Origin blog.csdn.net/xuelang532777032/article/details/132854690
Recomendado
Clasificación