Navigation Menu

Skip to content

dongyuwei/tiny-wechat-robot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tiny-wechat-robot

使用 code hijack 和 code injection ( phantomjs_wechat.js ) 来实现一个简单的微信机器人应用。

用法:

  1. 确保 PhantomJS(ref) 在系统路径PATH
  2. npm run serve
  3. npm run wechat (新开一个 Terminal Tab执行这个命令)
  4. 根据命令行提示信息,扫二维码登陆微信
  5. 发一个 ping 测试信息给当前登录用户,应该会自动收到一个 pong 应答

Tested with node v7.7.3 and PhantomJS V 2.1.1: phantomjs-2.1.1-macosx.zip or phantomjs-2.1.1-linux-x86_64.tar.bz2

核心功能

目前就是一个最简化的ping-pong应答机器人 (核心代码是 wechat_injection.js ),给当前登录用户发消息,则:

  1. 收到ping,回复pong
  2. 收到pingping,回复pongpong
  3. 收到pingpingping,回复pongpongpong
  4. 其他,不做特殊处理。

设计思路

  1. 使用 phantomjs 访问微信 web 登录页面;
  2. 劫持脚本: https://res.wx.qq.com/a/wx_fed/webwx/res/static/js/index_c7d281c.js (这个脚本的版本号可能会变更)通过 phantomjs networkRequest.changeUrl() 把脚本请求转发为本机脚本 wechat_injection.js;
  3. wechat_injection.js 中重载angular.bootstrap并且挂载自定义的 hook,从而可以监听微信聊天信息。同时在脚本的最后再加载并且执行原始的微信 index js,需要针对特殊的 ECMA 语言特性做特殊处理(目前只有一个 let 会有问题,已经替换为 var)。
  4. 根据自定义规则做自动应答。

如何调试?

使用任意 proxy 代理软件,如 Charles Proxy,或者chrome浏览器插件trumpet 转发资源请求。

  • 需要注意 wechat_injection.jsloadOriginalScript() 在 Chrome 中会遇到跨域请求限制问题,可以直接修改代码,把原始 script 下载后添加到该文件中即可。

Releases

No releases published

Packages

No packages published