If you don’t want to develop, please try thisYou can have your own wechat robot with a swipe

demand

If you see someone else has a wechat robot and want to have one yourself, develop one.

The main function

  • Automatically pass friend authentication
    • When someone adds a robot, decide whether to pass after the validation message key or pass directly
    • After verification, the robot will automatically reply and introduce the functions of the robot
  • Private chat keyword reply
    • For example, reply plus group push group list
    • Reply group name push group chat invitation,
  • Automatic chat
    • In group chat, you can chat with the robot by @[robot] XXX or beginning with Nami
    • Private chat You can chat by sending messages
  • Join the group chat automatically welcome
    • When a new friend joins a group chat, it automatically sends a message of welcome to [new friend]
  • Timing task
    • Group chat, daily timing push news or other information.

Open source projects wechaty- Puppet-wechat and wechaty

Due to the limitation of wechat Web terminal, I made a robot through wechaty-Puppet-PadPlus based on iPad protocol, but this package requires token, 200 yuan/month.

It is very friendly to users who cannot use the web version of wechat.

  1. Using this package requiresToken
  2. JuziBOT Inc staff (wechat id: Botorange_yeah) can be added as wechat friends. Once added, you will receive a review form. If you pass the review and are willing to blog in Wechaty, you can use the iPad agreement for free (free Token for 15 days)!
  3. To get a free token that works for a long time, sign up for a so-called open source incentive plan, where after 15 days, you have to submit a Github repository of MVP(minimum Viable Product), Wechaty forks it out to the community and offers a one-year free token.

Thanks to wechaty's upgrade, wechaty now supports all wechat logins. Even if you couldn't log in to the Web version before, you can use wechat now.

Start building your own robot

  • You can just follow the quick start on the official website and develop yourself against the API.
  • Directly pull my existing examples, simple modification, into their own robot.

Take my robot:

  • Fork a piece of code
  • Clone to the local
  • Node-v // Preferably v14 or above
  • npm i
  • node index

  • Scan code login can be, the default access to the day line API. In this way, your wechat will have an automatic reply function.

The configuration file

Configuration files are just constants. You can add a background management system for dynamic change.

config.js

/* * @author: Ace * @date: 2020-03-08 16:31:25 * @description: Configuration item */
module.exports = {
  NAME: "".NICKNAME: "".MEMORIAL_DAY: "".CITY: "".SENDDATE: "".// Scheduled send time 17:19:0 every day. For the rules, see /schedule/index.js
  TXAPIKEY: "Your own heavenly apikey."./ / http://api.tianapi.com/txapi/one/index?key= here must fill in the personal application day apikey, please replace themselves Apply for address https://www.tianapi.com/signup.html?source=474284281

  // Advanced function configuration items (optional)
  AUTOREPLY: true.// The automatic chat function is enabled by default
  DEFAULTBOT: "0".// Set default chat robot 0 day travel robot 1 Turing robot 2 day travel docking Turing robot, need to go to the official website of Tianxing robot recharge (50 yuan/year, 1000 times a day)
  AUTOREPLYPERSON: [""].// Enable the bot chat function to specify friends' remarks, preferably without special characters
  TULINGKEY: "Turing Robot Apikey".// Turing Robot apikey, you need to go to the Turing robot official website to apply, and need to be certified
}
Copy the code

The entrance

index.js

/* * @author: Ace * @date: 2020-03-08 16:31:25 * @description: wechaty-puppet-padplus import program */

const { Wechaty } = require("wechaty") // Wechaty core package
const { PuppetPadplus } = require("wechaty-puppet-padplus") // Padplus protocol package
const config = require("./config") // Config file

const onScan = require("./onScan") // When the robot needs to scan the QR code, it listens for the callback
const onRoomJoin = require("./onRoomJoin") // Join the room listening callback
const onMessage = require("./message/index") // Message listening callback
const onFriendShip = require("./onFriendShip") // Add friends to listen for callback

/ / initialization
const bot = new Wechaty({
  puppet: new PuppetPadplus({
    token: config.token
  }),
  name: config.name
})
// Next a chain call, listen, start
bot
  .on("scan", onScan) // The robot listens when it needs to scan the QR code
  .on("room-join", onRoomJoin) // Join the room monitor
  .on("message", onMessage(bot)) Wechaty (wechaty); wechaty (wechaty)
  .on("friendship", onFriendShip) // Add friends to listen
  .start()
Copy the code

message

The more logical part is the Message section, so Message creates a separate folder.

First, connect to the chat interface

It’s simple, it’s an API, and the parameters are the text of the chat.

Install dependencies

npm i axios urlencode
Copy the code

msg_reply.js

async function onMessage(msg, bot) {
  const contact = msg.talker(); // The sender
  const content = msg.text().trim(); // Message content
  const room = msg.room(); // Is it a group message
  const alias = (await contact.alias()) || (await contact.name()); // The sender's remarks
  const isText = msg.type() === bot.Message.Type.Text;
  if (msg.self()) {
    return;
  }

  if (room && isText) {
    // For group messages, only text messages are currently processed
    const topic = await room.topic();
    console.log(
      ` group name:${topic}Sender:The ${await contact.name()}Content:${content}`
    );
  } else if (isText) {
    if (content) {
      let reply;
      if (config.DEFAULTBOT == "0") {
        // Skyline chatbot logic
        reply = await superagent.getReply(content);
        console.log("Skyline robot replied:", reply);
      } else if (config.DEFAULTBOT == "1") {
        // Turing chatbot
        reply = await superagent.getTuLingReply(content);
        console.log("Turing robot replied:", reply);
      } else if (config.DEFAULTBOT == "2") {
        // Turing chat
        reply = await superagent.getTXTLReply(content);
        console.log("Celestial docking Turing robot replied:", reply);
      }
      try {
        await delay(2000);
        await contact.say(reply);
      } catch (e) {
        console.error(e); }}// }}}Copy the code

GetReply method

// Skyline chatbot
async function getReply(word) {
  let url = TXHOST + "robot/";
  let content = await superagent.req({
    url,
    method: "GET".params: {
      key: config.TXAPIKEY, // ApiKey in the configuration file
      question: word,
      mode: 1.datatype: 0.userid: uniqueId,
    },
  });

  if (content.code === 200) {
    let res = content.newslist[0];
    let response = "";
    if (res.datatype === "text") {
      response = res.reply;
    } else if (res.datatype === "view") {
      response = 'Although I don't quite understand what you are talking about, it feels very advanced, so I also searched for similar articles to learn, do you think it is useful?${content.newslist[0].title}"${content.newslist[0].url}`;
    } else {
      response =
        "You're so good, you've got me stumped, and I'm going to study, or I can't answer your question.";
    }
    return response;
  } else {
    return "I feel lost in the endless web, can you find me?"; }}Copy the code

The branch processes the received information

  • The message comes from itself and returns directly

  • The news came from a group chat

    • Messages starting with Nami are directly connected to the robot’s automatic response.
    • @ the message of the robot is directly connected with the robot to reply automatically. (Every time you have to chat with a robot @robot is still quite troublesome, start the chat with Na Mei.)
    • Other group messages are ignored
  • Message to selfish chat

    • Specify keyword reply (lead add group, add group welcome, etc.)
    • Other chat content docking robot automatic reply

Better all alone than all

There are still some people who can’t happily own their own robots for various reasons

  • Not being able to develop Node
  • Don’t have your own server (play locally)
  • Not a developer

So I upgraded to have my own wechat robot with just a swipe

reference

The official documentation

Hand rolled Wechaty | NodeJS based on the agreement of a simple WeChat robot assistant

Problems encountered during project startup (server is centos 8)

1. Failed to launch the browser process The related library file is missing

According to theTROUBLESHOOTING

Solution:

  • Updated node juejin.cn/post/684490 according to the Trouble shooting documentation…
Yum provides libatk. 1.0. So. 0#1: atK-Libs-1.1.3-40.el7.i686: atK Printing System-libraries
#Source: OS
#Matching source:
#Provide: libatk. 1.0. So. 0
yum install atk
Copy the code
  • Restart or lack of other library files, directly to the Chromium file under the query dependency
cd node_modules/puppeteer/.local-chromium/linux-686378/chrome-linux
ldd chrome | grep not
#Libatk - bridge - 2.0. So. 0 = > not found
#Libpangocairo 1.0. So. 0 = > not found
#Libpango 1.0. So. 0 = > not found
# libcairo.so.2 => not found
# libatspi.so.0 => not found
# libgtk-3.so.0 => not found
# libgdk-3.so.0 => not found
#Libgdk_pixbuf 2.0. So. 0 = > not found
#Find the missing so file, same as above to find the installation package name
yum install atk pango at-spi2-atk gtk3
Copy the code

Start-up success