preface

Once upon a time, I added a wechat, sent it “add group”, and was automatically entered into the developer group, then asked questions in the group chat, and the wechat would @ me, quoting my question with an answer and a reference link. Every day when commuting, it will send a warm reminder, out of the strong interest in this technology, started wechat robot research trip.

I started to find several third-party platforms and tried them for a few days, but I always felt that I was not free enough. Since I am a programmer, why not realize it through technical means? I started to find the following three open source games on GitHub

  • itchat
    • itchatIs an open source wechat user interface, using Python to call wechat
    • With less than 30 lines of code, you can build a wechat robot that can process all the information
  • wechaty
    • wechatyBot SDK is a Bot SDK for wechat individuals, which can create a robot with 6 lines of JS
    • Have includedlinux.Windows.MacOSandDockerCross-platform support, based on Node.js
  • vbot
    • vbotIs based on wechat Web version of the interface, useThe HTTP protocolAnd polling implementation
    • The highlight is the variety of interesting gameplay that can be achieved through anonymous functions
    • Through API, it is more convenient to create their own web version of wechat, based on PHP

I met wechaty

I chose wechaty because I was relatively familiar with JS

It is a Node project written in Typescript. It is a bot framework built specifically for personal wechat. The basic implementation of the original is through Chrome real-time monitoring of wechat web version of all kinds of information, and then capture the information encapsulated into an interface for developers to use.

Take a look at the official documentation

Wechaty – Github wechaty Chinese document

6 lines of JS to create a robot, really neat

const { Wechaty } = require('wechaty')
const bot = new Wechaty()
bot.on('scan',    (qrcode, status) => console.log(['https://api.qrserver.com/v1/create-qr-code/?data='.encodeURIComponent(qrcode),'&size=220x220&margin=20',].join(' ')))
bot.on('login',   user => console.log(`User ${user} logined`))
bot.on('message', message => console.log(`Message: ${message}`))
bot.start()
Copy the code

You can see an example of wechaty-Getting-started in the documentation

After downloading, NPM I installs the module first, and then runs to have the login QR code

Failed to log in to wechat page

The original wechat registered after 2017 can not log in the webpage version of wechat, and before 2017 registered wechat account also has a great chance to log on, looking for friends to try are not good.

Check whether your wechat account supports web wechat login:

  • wx.qq.com

Click the link, enter the PC side and then scan the code to log in. If you can log in, you can use the above example

Then I looked at VBot and ItChat, but found that they were also implemented based on web protocols

Search information from the Internet, there are probably several ways to achieve:

  • Web page: after 2017, new login numbers are not supported. Only old login numbers are supported. In addition, there are serious disconnection and serious function loss
  • Xposed technology: in June 2019, wechat official focus on the Xposed in the industry, since then in the industry a howling everywhere, to iPad/MAC protocol transition. For specific cases, please click
  • PC Hook: Code injection, which means reverse development. Seal the situation is more, easy to use the sealing, the company large-scale seal and other situations, and the current use rate is less in the marketing industry, relatively small team use simulator: high delay, message real-time arrival rate is low, analog artificial operation efficiency is slow, the function is less, can not bear the commercial function
  • IPad protocol: good security, satisfying functions, high industry share, but with a small number of capable r&d personnel, basically two or three teams of research and development, and the current team disbanded, some wechat login failed, and through GRPC, MMTLS research and development, detection probability exists
  • MAC protocol: better security than the iPad protocol, less functional than the iPad protocol, the industry has less research and development capabilities, security, stability is relatively good, there will not be sealing, batch sealing
  • Mixed channel: wechat internal channel, the highest permission, based on MAC and Ipad protocol, non-GRPC, MMTLS, suitable function, wechat genuine channel, no technical seal problem

Look, internal channel is impossible, only iPad protocol and MAC protocol is the best at present

wechaty-puppet-padplus

I was about to give up when WEChaty came up with a package based on the iPad protocol

  • wechaty-puppet-padplus

Wechat robot platform — WXbot

Project introduction

  • The console
    • Bound robot
    • The login
    • Automatically through friends to verify keyword Settings, when someone adds a robot, keywords match directly through
    • Friend verification passes automatic reply
    • exit
  • Auto reply
    • Ordinary message
      • Set up automatic keyword reply for friends/group chat/all group chat
    • Add group invited
      • The robot responds to a group chat list, and friends can be selected to join the group
    • Kicking instruction
      • The robot recognizes the instructions and automatically removes members from the group chat
  • My good friend
    • Send a separate message to a friend
  • My group chat
    • Group chat list, manage all group chats
    • Set the name of group chat, publish bulletin, and send group message
    • Set the basic information of group chat, welcome words, upper limit of violation times of members and whether it is controlled by robot
  • Timing task
    • Set timed tasks for friends/a group chat/all group chats, and the robot will trigger message push at the specified time
  • Intelligent chat
    • Low IQ dialogue
    • Idiom solitaryong, check the weather, check the hotel, Xiehouyu…

technology

  • The server Node. Js
  • SSR framework NuxtJS
  • Front end framework Vue
  • Ant Design of Vue UI components
  • Persistent mongo
  • The agreement wechaty puppet — padplus

Here is a direct introduction to robot module

| - server / | -- - / lib | -- -- -- -- -- - FriendShip. Js# Friendship relationship, friends add monitor
|------ Login.js		# Robot login and logout
|------ Message.js	        # message listening processing
|------ Room.js 	        # Join a group and quit the group chat
|------ Task		        # Robot timed tasks| -- - index. Js# import file
Copy the code

Online instance

Interested friends can reward a star 😊

Wxbot (Username/password: guest/111111)

Quick start

Preparation conditions

Install Node.js (v10 or later) and MongoDB. CNPM installation is recommended

Install dependencies

$ cnpm i
Copy the code

Start the service

  • Development mode
$ npm run dev
Copy the code
  • Production mode

Compile the project first

$ npm run build
Copy the code

Restarting the service

$ npm start
Copy the code

Open your browser and visit http://localhost:3000/

The system configuration

Modify the config.js configuration file as required. The modification takes effect only after the service is restarted. Parameter Description:

host

The value is a String containing the host name. If the value is 0.0.0.0, listening on any host.

port

Number Indicates the port Number.

mongoUrl

String, MongoDB link.

secret

String, JWT key.

tianApiKey

String Day row data key

Online deployment

The use of PM2

Pm2 is recommended for node.js process management and persistent running.

The installation
$ cnpm i -g pm2
Copy the code
Start the
$ pm2 start pm2.config.js
Copy the code

The last

After nearly a month of agonizing, there are still many functions that can be improved, and there are some inconveniences in the code. Welcome your comments and study together to make the platform more perfect.

Fun things always want to experience a first, scan code plus my small assistant, verify the message writing robot can be directly through, add group communication is also possible. You can also add robots to your group chat to play with. Log on to the online example above and set the keywords to experience it.