Using node to develop WeChat group chat robot Chapter 3

PART sequence

  1. How to request third-party api via https

  2. What are the free and paid third-party APIs?

  3. Remember to read the first few chapters first, and then read this chapter after you understand it, otherwise you will be confused. Click to collect ==》#programmer dry stuff

  4. Remember to add the mini program to your favorites first, otherwise you won’t be able to find it when you want to debug => “ Practical Resources for Programmers

PART text

Come on, enter the state, first figure out how to make the http request

Because we have updated "axios" during cnpm install in Chapter 1, we can now start writing methods

Come on, the blogger wrote one, you can take it directly

import axios from "axios"const ajax=async(url,param,type)=>{
   
     return new Promise((resolve,reject)=>{
   
       var axiosOption={
   
         method:type?type:"get",      url: url,      data: param,      timeout: 0,      headers: {'Content-Type': 'application/json;charset=utf-8'}    }    axios(axiosOption).then(function (res){
   
         resolve(res)    })  })}

Look at the location of the code the blogger put

Okay, let's write a request. Let's open a random URL https://api.oick.cn/ and click on the poisonous chicken soup as shown below - the strong man needs a bowl.  

(Note that because it is a site opened by others, you may not be able to request if your personal site is closed that day. It’s okay. There are many free third parties in the group owner’s mini program. You can just find a free interface on the platform and follow the instructions. Just follow the blogger’s process operation) ===》Mini Program: Practical Resources for Programmers

Without further ado, let’s continue with the tutorial. Did you see it? This is an API that can be called by others for free.

They all indicate the request method "get", and the request url and this interface do not require parameters.

Let's go back to our project and put a request

var res=await ajax("https://api.oick.cn/dutang/api.php",null,"get")debugger

Remember to re-run the service and scan the QR code to log in every time after modifying the code! ! !

Have you discovered it? After requesting, a paragraph will be returned: 'When you meet someone you like, pursue it bravely, so that you can know that there is far more than one person who will reject you. '

The next process is the same. Reply this paragraph to the WeChat group chat and let’s change the code.

var res=await ajax("https://api.oick.cn/dutang/api.php",null,"get")room.say(res.data)//在这个群发送消息

Remember to re-run the code and scan it (we won’t repeat this sentence!! Anyway, if you find it doesn’t work, re-run it yourself)

The code location is as follows

You can see here that you have requested the third-party API to reply text content.

Come on, how do we reply with a picture?

Let's go back to the top and add a line of code

import { FileBox }  from 'file-box'

Adding a command to send pictures

var fileBox =FileBox.fromUrl("https://img-blog.csdnimg.cn/789ce9f6d9974986ae0435c36d10c559.jpeg");await room.say(fileBox)

The attentive friends discovered that the blogger posted a link to the picture. Because the blogger was afraid that the third-party API would not be available by then, he simply posted a link to the online picture. You may not necessarily follow the link to the picture here. Same, you can just find a picture address online and replace it. Remember not to request that kind of web page to return the entire picture, otherwise you can download it locally through fs and then send it to the WeChat group chat. So remember to look for the kind of third-party interface that returns the image path, such as the following return format of the blogger.

Actually, FileBox.fromUrl is used to send videos, files, and languages.

I'll put a FileBox github by the way. Friends, go to it yourself ===》https://github.com/huan/file-box

If you can’t understand his English, remember to open the webpage translation plug-in on the blogger’s first chapter.

After reading this chapter, you basically know how to reply and send pictures, text, and videos in group chats. In the next chapter, I will teach you how to use a robot’s API for free, and connect it to weather queries, tell jokes, and tell love stories. These are the three functions shown below

After I finish writing the next chapter of the tutorial, I will send it to the "Public Account Programmer Wild Area" for everyone to check it out by themselves.

PART public account collection

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

おすすめ

転載: blog.csdn.net/xuelang532777032/article/details/132881374