GithubHelp home page GithubHelp logo

node-wechat's Introduction

Node.js 微信公众平台 API

安装 ###

npm install node-wechat

使用示例 1

var http = require('http'),
    wechat = require('node-wechat')(your_token);

http.createServer(function (req, res) {
  //检验 token
  wechat.checkSignature(req, res);
  //预处理
  wechat.handler(req, res);

  //监听文本信息
  wechat.text(function (data) {
    //console.log(data.ToUserName);
    //console.log(data.FromUserName);
    //console.log(data.CreateTime);
    //console.log(data.MsgType);
    //...
    var msg = {
      FromUserName : data.ToUserName,
      ToUserName : data.FromUserName,
      //MsgType : "text",
      Content : "这是文本回复",
      //FuncFlag : 0
    }
    //回复信息
    wechat.send(msg);
  });

  //监听图片信息
  //wechat.image(function (data) { ... });

  //监听地址信息
  //wechat.location(function (data) { ... });

  //监听链接信息
  //wechat.link(function (data) { ... });

  //监听事件信息
  //wechat.event(function (data) { ... });

  //监听语音信息
  //wechat.voice(function (data) { ... });

  //监听视频信息
  //wechat.video(function (data) { ... });

  //监听所有信息
  //wechat.all(function (data) { ... });
}).listen(80);

详见 example.js

使用示例 2

var http = require('http'),
    wechat = require('node-wechat')(your_token);

http.createServer(function (req, res) {
  //检验 token
  wechat.checkSignature(req, res);
  //预处理
  wechat.handler(req, res);

  //链式监听
  wechat.text(function (data) {
    // TODO
  }).image(function (data) {
    // TODO
  }).location(function (data) {
    // TODO
  }).link(function (data) {
    // TODO
  }).event(function (data) {
    // TODO
  }).voice(function (data) {
    // TODO
  }).video(function (data) {
    // TODO
  }).all(function (data) {
    var msg = {
      FromUserName : data.ToUserName,
      ToUserName : data.FromUserName,
      //MsgType : "news",
      Articles : [...]
    }
    wechat.send(msg);
  });

}).listen(80);

详见 example2.js

如需更多功能,你可以尝试 wechat

node-wechat's People

Contributors

johnnyzheng avatar nswbmw avatar yufan-zhao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-wechat's Issues

Need Collaborators~

@ALL 这个模块是我 3 年前尝试做微信订阅号写的,之后没再研究也没有精力去维护,但我发现仍然有不少朋友使用该模块,所以想找些 Collaborators 帮忙维护,如果有意愿的话在这留言,我会简单的筛选合适的人选。

处理逻辑存在异步代码,就导致重复执行

如果在某个响应回调中,如果有异步的操作,就会导致异步操作重复执行。
比如:
wechat.text(function(data){
setTimeout(function(){
console.log('call async')
}, 10);
});
console.log('call async');会被重复执行,1次、2次、3次、以此类推。求解答,不知道问题出在哪.
是不是类库内的事件机制没有处理好

使用node-wechat的send方法时一直提示时间之类的错误

[15:23:00]
[15:23:00] events.js:72
[15:23:00] throw er; // Unhandled 'error' event
^
[15:23:00] Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:690:11)
at ServerResponse.setWriteHeadHeaders (/Users/marlon/code/zm-www/node_modules/express-session/node_modules/on-headers/index.js:80:19)
at ServerResponse.writeHead (/Users/marlon/code/zm-www/node_modules/express-session/node_modules/on-headers/index.js:39:36)
at [object Object].Wechat.send (/Users/marlon/code/zm-www/node_modules/node-wechat/index.js:259:12)
at EventEmitter. (/Users/marlon/code/zm-www/server/api/wechat.ts:71:20)
at EventEmitter.emit (events.js:117:20)
at /Users/marlon/code/zm-www/node_modules/node-wechat/index.js:66:17
at Parser. (/Users/marlon/code/zm-www/node_modules/node-wechat/node_modules/xml2js/lib/xml2js.js:462:18)
at Parser.emit (events.js:95:17)
at Object.onclosetag (/Users/marlon/code/zm-www/node_modules/node-wechat/node_modules/xml2js/lib/xml2js.js:423:26)
at emit (/Users/marlon/code/zm-www/node_modules/node-wechat/node_modules/xml2js/node_modules/sax/lib/sax.js:625:33)
at emitNode (/Users/marlon/code/zm-www/node_modules/node-wechat/node_modules/xml2js/node_modules/sax/lib/sax.js:630:3)
at closeTag (/Users/marlon/code/zm-www/node_modules/node-wechat/node_modules/xml2js/node_modules/sax/lib/sax.js:874:5)
at Object.write (/Users/marlon/code/zm-www/node_modules/node-wechat/node_modules/xml2js/node_modules/sax/lib/sax.js:1314:29)
at Parser.exports.Parser.Parser.parseString (/Users/marlon/code/zm-www/node_modules/node-wechat/node_modules/xml2js/lib/xml2js.js:481:31)
at Parser.parseString (/Users/marlon/code/zm-www/node_modules/node-wechat/node_modules/xml2js/lib/xml2js.js:7:59)

xml2js嵌套解决方法

如果给微信公众号发送的信息就是一个xml格式的话,xml2js可能就会出问题。


1476069979


6339672286824092206

比如为微信公众号发送以上信息,可能就出问题了

同时两条消息发过来会有bug

同时有两条消息发过来,第一条异步处理,没有调用res.send,紧接着第二条就来了。
现在的RES是全局变量,RES被替换为第二条消息的response。然后第一条消息会通过第二条消息的RES返回结果。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.