Make an AI robot and help me answer messages

Hello everyone, I am Yupi. Since I started sharing knowledge, my WeChat has never stopped. On average, I receive hundreds of messages every day. Most of them are friends who are learning programming and ask me about programming.

But after all, I only have one person, so I can't reply to all messages one by one, so I feel guilty and powerless. In addition, I found that many of your questions are repeated, and I have written articles to answer most of them.

So, I decided to make an AI question-and-answer robot to help me automatically answer everyone's common questions and reduce duplication of work.

In the end, I didn't expect that I was caught by a bug during the production process and made a lot of jokes. Interested friends can watch the video haha:

Address: https://www.bilibili.com/video/BV1Vq4y1B7zu/

Since the production method is too simple, I will share with you a complete tutorial so that you can easily make your own AI robot.

Homemade AI Robot Tutorial

The entire production process is expected to take 10 minutes

First describe the requirements: make a robot that can automatically answer WeChat messages.

To achieve this requirement, there are two main points:

  1. How to make the program receive messages from WeChat?
  2. How to reply to the corresponding content according to the message?

These two problems, if there is no certain professional knowledge, it is difficult to solve by yourself. But now in the age of open source, we can stand on the shoulders of giants and solve these problems with off-the-shelf technologies.

receive message

You can use open source wechatylibraries to automate WeChat operations, such as sending and receiving messages, passing friends, pulling groups, etc.

Open source WeChat robot library

The method of use is very simple. In the project introduction file of the wechaty warehouse, there is the simplest example code for getting started. It only takes 6 lines of code to start a robot that helps you receive messages!

Wechaty supports almost all mainstream programming languages, and the entry code for JavaScript is as follows:

import {
    
     WechatyBuilder } from 'wechaty'
// 启动
WechatyBuilder.build()
  .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}`))
  .start()

Explain the above code, you will find that there are many events defined in wechaty, such as scanning code, user login, accepting messages, accepting friend requests, etc. You don't need to care how the event is triggered by it, you just need to write processing methods for different events, such as automatically replying to the same message after receiving the message, the sample code is as follows:

// 初始化机器人
const bot = WechatyBuilder.build({
    
    
  name: 'yupi-wxrobot',
  // 用于兼容不同 IM 协议,不用关心
  puppet: 'wechaty-puppet-wechat',
})
// 处理消息
bot.on('message', async function (msg) {
    
    
  // 获取消息发送人
  const contact = msg.talker()
  // 获取消息内容
  const text = msg.text()
  // 获取群聊信息
  const room = msg.room()
  // 是私聊
  if (contact && text) {
    
    
    // 回复相同内容
    msg.say(text, contact);
  }
}

However, do n't run the above code directly! Because once you start the bot and don't limit the nickname of the replies, it will work for everyone who sends you a message!

I didn't pay attention at first, and got scammed. . .

So, if you only want to automatically reply to someone or a group chat message, remember to add the corresponding filtering logic in the code, such as:

// 处理消息
bot.on('message', async function (msg) {
    
    
  // 获取消息发送人
  const contact = msg.talker()
  // 获取消息内容
  const text = msg.text()
  // 获取群聊信息
  const room = msg.room()

  // 不处理自己的消息
  if (msg.self()) {
    
    
    return
  }
  // 群聊还是私聊
  if (room) {
    
    
    if(room.topic() === '鱼皮群') {
    
    
      // 回复
    }
  } else {
    
    
    if(contact.name() === '小号') {
    
    
      // 回复
    }
  }
}

OK, using the above code, you can accept messages and automatically reply!

So are you curious, how does wechaty receive WeChat messages? In fact, the principle is very simple. When executing the wechaty program, it will use the headless browser technology to quietly open a web version of WeChat, and then pop up the login QR code of the WeChat web version on the console where you run the program. After you scan the code to log in, The program only needs to listen for changes in page elements, or automatically trigger click events.

Web version WeChat

In fact, the reason is very simple, that is, to convert the manual operations we can perform on web pages into automatic background execution.

Smart Reply

The first question is solved, so how to give different responses according to different questions?

Many classmates will definitely say AI when they come up, and that was all misdirected by the "intelligence" I used. In fact, if it is just a simple automatic reply, and the problem rules can be converged and enumerated, it if ... else ...can !

if(/你好/.test(text)) {
    
    
  msg.say('好的');
} else if (/谢谢/.test(text)) {
    
    
  msg.say('不客气');
} else if (/加群/.test(text)) {
    
    
  msg.say('公众号[程序员鱼皮],回复[加群]');
} else {
    
    
  msg.say('我不懂');
}

It's not that the essence of artificial intelligence is if else, haha, it's just letting the machine do the if else for you.

But the reality is that my readers will have different expressions for the same question, such as "How to learn Java?", "I want to learn Java, how to learn it?" and so on. Therefore, artificial intelligence is still needed.

Where to go for artificial intelligence?

We can directly take 微信对话开放平台advantage of the powerful capabilities provided by , without writing a single line of code, to achieve intelligent conversations for free!

Address: https://openai.weixin.qq.com/

After logging in, create a bot first:

Create a bot


Then you can add skills to the robot. You can customize the skills and instill the specified questions and answers to the robot; you can also directly use the default skills provided by the platform, such as listening to songs, chatting, encyclopedia, etc.:

Configure skills

My need is to automatically answer my readers' programming related questions, so I need to create a new skill. Here, you can flexibly customize the questions, different questions, and answers, all with the interface, and easily create your own robot:

custom skills

Once configured, the bot can be published and used. We can bind the robot to the official account/mini program to automatically reply to readers' messages; we can directly access the intelligent customer service in the H5 webpage; we can also call the open interface in the program to use the intelligent dialogue ability:

Publish and use

Here we want to automatically get a reply in the wechaty program, so the way to use the open interface is also very simple, that is, use a request library to call the interface. The sample code is as follows:

// 获取 API 签名,2小时过期
// token 需从平台获取
const url = `https://openai.weixin.qq.com/openapi/sign/${
      
      token}`;
const {
    
    signature} = (await axios.post(url, {
    
    
    userid: 'test'
})).data;

// 调用 AI 接口,获取答案
async function getAnswer(userid, text) {
    
    
  const apiUrl = `https://openai.weixin.qq.com/openapi/aibot/${
      
      token}`;
  return (await axios.post(apiUrl, {
    
    
    "signature": signature,
    "userid": userid,
    "query": text,
  })).data?.answer;
}

That's about it, it's simple and practical, and interested students can use it to make a lot of interesting functions~


I am fish skin, and liver writing is not easy. If it is helpful, I hope to give a like and support. Thank you.

Guess you like

Origin blog.csdn.net/weixin_41701290/article/details/122050088