Making: github.com/doterlin/we…





Chatbot developed based on PhantomJS and wechat on the Web. The wechat account used (i.e. the account that acts as a robot) is a personal account and can be customized.

The effect

You can specify a wechat account:







Liao sister

– Custom commands will not be handled by AI:







Custom instruction

A group chat is better than a group chat.







WeChat group

View log in daemon:





Command line output

Usage scenarios

  • Wechat intelligent reply, monitoring, statistics and so on
  • Convenient query
  • Entertainment (liao) music (mei)

How to use

1. Installation environment

Download the source code and install the following environment:

1.node.js

2.phantomjs

3.casperjs

2. The configuration

Find and configure /config/ conster.js

Var ROBOT_NAME = "小强"; Module. exports = {//微信web version 'URL' : 'https://wx.qq.com/', // robot name 'ROBOT_NAME' : ROBOT_NAME, / / Turing robot apiKey and API address / / AI part are using a third-party robot Turing (http://www.tuling123.com/) / / here is just the sample, Please register and replace the APIKey on the Turing website. The free version is limited to 5000 calls per day. Conditional students can pay, supported by good products' TURING_APIKEY ':' 99 fecec3424d416898b91b0998e2b26a ', 'TURING_URL' : 'http://www.tuling123.com/openapi/api', / / locking WeChat ID note, note is a note; If it is a group chat, fill in the group chat name. 'HELLO_WORLD' : '[lightning]' + ROBOT_NAME + '[lightning] has been started... '}Copy the code

3. Run and log in to wechat

Install dependencies and run commands:

npm install
Copy the code
capserjs index.js
Copy the code

If you are promptedPython can't findPlease install itpythonAnd make sure it runs globally

Scan the qr code that pops up when you see the following prompt after logging in to wechat:

Loading qr code... The qr code has been saved. The path is "static/img/qr.jpg". You are using the default software to open the TWO-DIMENSIONAL code. Please scan the qr code on wechat of your mobile phone to confirm login (if not, please open it manually).Copy the code

Please use your mobile phone to scan the code and confirm login within one minute. The login is successful and the welcome message is sent. This completes the usage step. The following sections describe how to extend the functionality.

Instructions and AI

You may need two methods before doing this:

message.send()

Var message = require('/ SRC /utils/message'); . Casper.then (function(){message.send(this, 'hello '); })Copy the code

ajax() / ajax.get() / ajax.post() / ajax.getJSON()

Var ajax= require('/ SRC /utils/ajax'); . casper.then(function(){ ajax(this, "http://example.com/getInfo", 'get', {uid: 123}, function(res){ console.log(JSON.stringify(res)); }); / / or ajax. Get (this, "http://example.com/getInfo", {uid: 123}, function (res) {the console. The log (JSON. Stringify (res)); }); //jsonp ajax.getJSON(this, "http://example.com/getInfo?calback=?", {uid: 123}, function(res){ console.log(JSON.stringify(res)); }); })Copy the code

You can expand the functions of the robot as required by:

Write instructions
/ / you can write some instructions not to AI / / is divided into an exact match of instructions and fuzzy matching / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / exact match // Exact directives are written under '/ SRC /directive/exact. // The key is the directive name, and the value is a method that takes msgContent(the message sent by the user) and casperIns(instances of casper), as in: // The program prompts and exits when the other party sends the 'close' command. Module. exports = {' closed ': Function (msgContent, casperIns) {message. Send (casperIns, '[rose] thank you for using [rose]\n\r([lightning] Casperins. echo(' wechat issues a close password and the program exits. ') return casperIns.exit(); }} / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - / / / / fuzzy matching fuzzy matching instruction in ` / SRC/directive/fuzzy js ` writing. // The first two parameters are the same exact instructions, the third is the current regular expression; Var weather = require('./explain/weather'); var weather = require('./explain/weather'); Module.exports = {'/ exports /g': weather} //weather.js: var ajax = require('.. /.. /utils/ajax'); var message = require('.. /.. /utils/message'); Var formatWeather = function(local, weather) { For details, please check the source} var weather = function (msgContent, casperIns, regex) {var local = msgContent. Replace (/ / | weather, ' '); var resource = 'http://wthrcdn.etouch.cn/weather_mini?city=' + encodeURIComponent(local); ajax.get(casperIns, resource, {}, function(res){ var weather = JSON.parse(res); if (weather.status == 1000) { message.send(casperIns, formatWeather(local, weather)); } else {message.send(casperIns, 'No weather information found. Please try to enter the format such as "Guangzhou weather". ')}}); } module.exports = weather; //-------------------------------------------------------------------Copy the code
Improved AI

I’m not working on that either.

debugging

When debugging the code, you can set the logLevel field in the CapserJS configuration image to INFO to display more PhantomJslog. For more configuration, please refer to the Casperjs document.

// path: config/casper.js module.exports = {clientScripts: ['static/js/jquery.js'], pageSettings: {loadImages: True, userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.14 Safari/537.36',}, logLevel: "info", //here viewportSize: {width: 1300, height: 900}, verbose: true, waitTimeout: 1000 * 60 * 60 * 24 * 365, onWaitTimeout: function(){console.log('waitFor* method times out... ')}}Copy the code

In addition, the browser screenshots are saved to /static/img for viewing browser renderings during each step. For example, each new message is captured and overwritten as lastNewMsgContent.png.

Secondary development if convenient pleaseForkContribution to thisGithubTo improve the project together!

A version of the forecast

The next version updates but is not limited to the following:

Multiple wechat accounts can be bound. 2.UI console (node.js web synchronization command log and some operations) 3. Support picture recognition and reply (such as bucket chart) 4. More practical instructions