Hello, everyone. I am Yu PI. Since I started knowledge sharing, my wechat has been constantly occupied.

But after all, I am only one person, so I can’t reply all messages one by one, so I feel very guilty and powerless. In addition, I found that many of your questions were repetitive, and MOST of them WERE answered in articles.

So, I decided to build an AI question-answering robot to help me automatically respond to people’s frequently asked questions and reduce rework.

As a result, the production process was Bug pit miserably, make a lot of jokes, interested friends can see the video haha:

Address: www.bilibili.com/video/BV1Vq…

Because it is so easy to make, I share the complete tutorial with you so that you can easily make your own AI robot.

Homemade AI robot tutorial

The whole process is expected to take 10 minutes

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

To implement this requirement, there are two main points:

  1. How to let the program receive messages from wechat?
  2. How do I reply to messages?

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

Receive a message

We can use the open source wechaty library to automate wechat operations, such as sending and receiving messages, passing friends, and pulling groups.

It’s very simple to use. In the wechaty Warehouse project documentation, there is a simple example code to get started. It only takes six lines of code to start up a bot that accepts messages for you!

Wechaty supports almost all major programming languages, with the following code for getting started with JavaScript:

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

Explaining the code above, wechaty defines many events, such as scanning codes, user login, receiving messages, accepting friend requests, and so on. You don’t need to worry about how the event is triggered, just write methods for different events, such as automatically replying to the same message when you receive it. Example code is as follows:

// Initialize the robot
const bot = WechatyBuilder.build({
  name: 'yupi-wxrobot'.// It is compatible with different IM protocols
  puppet: 'wechaty-puppet-wechat',})// Process the message
bot.on('message'.async function (msg) {
  // Get the sender
  const contact = msg.talker()
  // Get the message content
  const text = msg.text()
  // Get group chat information
  const room = msg.room()
  / / is the private chat
  if (contact && text) {
    // Reply with the same contentmsg.say(text, contact); }}Copy the code

But don’t run the code directly! Because once you start the bot and you don’t limit the nicknames of the respondents, it works for everyone who messages you!

I wasn’t paying attention at first, but I was fooled…

So, if you only want to automatically reply to someone or a group of messages, add filtering logic to your code, such as:

// Process the message
bot.on('message'.async function (msg) {
  // Get the sender
  const contact = msg.talker()
  // Get the message content
  const text = msg.text()
  // Get group chat information
  const room = msg.room()

  // Do not process your own messages
  if (msg.self()) {
    return
  }
  Group chat or private chat
  if (room) {
    if(room.topic() === 'Fish Skin colony') {
      / / reply}}else {
    if(contact.name() === 'small') {
      / / reply}}}Copy the code

OK, with the above code, you can accept messages and automatic reply!

Do you wonder how Wechaty receives wechat messages? Wechaty uses headless browser technology to secretly open a web-based version of wechat, and then pops up a login QR code on the console where you run the app. After you log in, wechaty simply listens for changes in page elements or automatically triggers a click event.

In fact, the truth is very simple, is that we can be on the web page manual operation into background automation.

Smart reply

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

A lot of students must have said AI at the beginning, that is all by my use of the “intelligence” a deviation. In fact, if it is just a simple auto reply, the problem rules can be convergent, enumerable case, directly use if… else … We can solve it!

if(/ hello /.test(text)) {
  msg.say('good');
} else if (/ thank you /.test(text)) {
  msg.say('You're welcome');
} else if (/ / group.test(text)) {
  msg.say('Public number [programmer fish], reply [add group]');
} else {
  msg.say('I don't understand.');
}
Copy the code

I thought the essence of artificial intelligence is if else haha, it’s just asking machines to do if else for you.

But the reality is that my readers have different ways of saying the same question, “How do I learn Java?” “, “I want to learn Java, how? And so on. So artificial intelligence comes into play.

Where are we going to get artificial intelligence?

We can directly take advantage of the powerful capabilities provided by wechat Dialogue open platform, without writing a line of code, we can realize smart dialogue for free!

Address: openai.weixin.qq.com/

Create a bot after logging in:


Then you can add skills to the robot, you can customize skills, and feed the robot with specific questions and answers; You can also use the default skills provided by the platform, such as listening to music, chat, encyclopedia, etc. :

My requirement was to automatically answer readers’ programming-related questions, so I needed to create a new skill. Here, you can flexibly customize the topic, different questions and answers, all with the interface operation, easy to create your own robot:

Once configured, the robot is ready to be published and used. We can bind the robot to the public account/small program and automatically reply to readers’ messages; Intelligent customer service can be directly accessed in the H5 web page; You can also invoke an open interface in your program to use intelligent conversation capabilities:

We want wechaty to automatically receive a response, so we use the open interface method, which is very simple, using a request library to call the interface. The example code is as follows:

// Get API signature, expire in 2 hours
// The token must be obtained from the platform
const url = `https://openai.weixin.qq.com/openapi/sign/${token}`;
const {signature} = (await axios.post(url, {
    userid: 'test'
})).data;

// Call the AI interface to get the answer
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; }Copy the code

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


I am fish skin, liver text is not easy, if there is help, I hope to give a point to support, thank you.