Giter Club home page Giter Club logo

wechat's Introduction

wechat

微信公共平台自动回复消息接口服务中间件

Wechat document in English

模块状态

  • NPM version
  • Build Status
  • Dependencies Status
  • Coverage Status

功能列表

  • 自动回复(文本、图片、语音、视频、音乐、图文)
  • 等待回复(用于调查问卷、问答等场景)
  • 会话支持(创新功能)

详细参见API文档

Installation

$ npm install wechat

Use with Connect/Express

var wechat = require('wechat');
var config = {
  token: 'token',
  appid: 'appid',
  encodingAESKey: 'encodinAESKey',
  checkSignature: true // 可选,默认为true。由于微信公众平台接口调试工具在明文模式下不发送签名,所以如要使用该测试工具,请将其设置为false
};

app.use(express.query());
app.use('/wechat', wechat(config, function (req, res, next) {
  // 微信输入信息都在req.weixin上
  var message = req.weixin;
  if (message.FromUserName === 'diaosi') {
    // 回复屌丝(普通回复)
    res.reply('hehe');
  } else if (message.FromUserName === 'text') {
    //你也可以这样回复text类型的信息
    res.reply({
      content: 'text object',
      type: 'text'
    });
  } else if (message.FromUserName === 'hehe') {
    // 回复一段音乐
    res.reply({
      type: "music",
      content: {
        title: "来段音乐吧",
        description: "一无所有",
        musicUrl: "http://mp3.com/xx.mp3",
        hqMusicUrl: "http://mp3.com/xx.mp3",
        thumbMediaId: "thisThumbMediaId"
      }
    });
  } else {
    // 回复高富帅(图文回复)
    res.reply([
      {
        title: '你来我家接我吧',
        description: '这是女神与高富帅之间的对话',
        picurl: 'http://nodeapi.cloudfoundry.com/qrcode.jpg',
        url: 'http://nodeapi.cloudfoundry.com/'
      }
    ]);
  }
}));

备注:token在微信平台的开发者中心申请

回复消息

当用户发送消息到微信公众账号,自动回复一条消息。这条消息可以是文本、图片、语音、视频、音乐、图文。详见:官方文档

回复文本

res.reply('Hello world!');
// 或者
res.reply({type: "text", content: 'Hello world!'});

回复图片

res.reply({
  type: "image",
  content: {
    mediaId: 'mediaId'
  }
});

回复语音

res.reply({
  type: "voice",
  content: {
    mediaId: 'mediaId'
  }
});

回复视频

res.reply({
  type: "video",
  content: {
    title: '来段视频吧',
    description: '女神与高富帅',
    mediaId: 'mediaId'
  }
});

回复音乐

res.reply({
  title: "来段音乐吧",
  description: "一无所有",
  musicUrl: "http://mp3.com/xx.mp3",
  hqMusicUrl: "http://mp3.com/xx.mp3",
  thumbMediaId: "thisThumbMediaId"
});

回复图文

res.reply([
  {
    title: '你来我家接我吧',
    description: '这是女神与高富帅之间的对话',
    picurl: 'http://nodeapi.cloudfoundry.com/qrcode.jpg',
    url: 'http://nodeapi.cloudfoundry.com/'
  }
]);

回复设备社交功能消息

res.reply({
    type: 'hardware',
    HardWare:{
      MessageView: 'myrank',
      MessageAction: 'ranklist'
    }
});

将用户消息转发到多客服

将普通微信用户向公众号发的消息,转发到多客服系统

res.transfer2CustomerService();

回复设备消息

模块可以对类型为device_text或device_event的消息作出特定格式的响应.

var wechat = require('wechat');
var config = {
  token: 'token',
  appid: 'appid',
  encodingAESKey: 'encodinAESKey',
  checkSignature: true // 可选,默认为true。由于微信公众平台接口调试工具在明文模式下不发送签名,所以如要使用该测试工具,请将其设置为false
};

app.use(express.query());
app.use('/wechat', wechat(config, function (req, res, next) {
  // 微信输入信息都在req.weixin上
  var message = req.weixin;
  if (message.MsgType === 'device_text') {
    // 设备文本消息
    res.reply('这条回复会推到设备里去.');
  } else if (message.MsgType === 'device_event') {
    if (message.Event === 'subscribe_status' ||
      message.Event === 'unsubscribe_status') {
    //WIFI设备状态订阅,回复设备状态(1或0)
      res.reply(1);
    } else {
      res.reply('这条回复会推到设备里去.')
    }
  }
}));

OAuth

OAuth功能请前往:https://github.com/node-webot/wechat-oauth

WXSession支持

由于公共平台应用的客户端实际上是微信,所以采用传统的Cookie来实现会话并不现实,为此中间件模块在openid的基础上添加了Session支持。一旦服务端启用了connect.session中间件,在业务中就可以访问req.wxsession属性。这个属性与req.session行为类似。

app.use(connect.cookieParser());
app.use(connect.session({secret: 'keyboard cat', cookie: {maxAge: 60000}}));
app.use('/wechat', wechat('some token', wechat.text(function (info, req, res, next) {
  if (info.Content === '=') {
    var exp = req.wxsession.text.join('');
    req.wxsession.text = '';
    res.reply(exp);
  } else {
    req.wxsession.text = req.wxsession.text || [];
    req.wxsession.text.push(info.Content);
    res.reply('收到' + info.Content);
  }
})));

req.wxsessionreq.session采用相同的存储引擎,这意味着如果采用redis作为存储,这样wxsession可以实现跨进程共享。

等待回复

等待回复,类似于电话拨号业务。该功能在WXSession的基础上提供。需要为等待回复预置操作,中间件将其抽象为List对象,在提供服务前需要添加服务。

var List = require('wechat').List;
List.add('view', [
  ['回复{a}查看我的性别', function (info, req, res) {
    res.reply('我是个妹纸哟');
  }],
  ['回复{b}查看我的年龄', function (info, req, res) {
    res.reply('我今年18岁');
  }],
  ['回复{c}查看我的性取向', '这样的事情怎么好意思告诉你啦- -']
]);

然后在业务中触发等待回复事务,如下示例,当收到用户发送list后,调用res.wait('view')进入事务view中。

var app = connect();
app.use(connect.query());
app.use(connect.cookieParser());
app.use(connect.session({secret: 'keyboard cat', cookie: {maxAge: 60000}}));
app.use('/wechat', wechat('some token', wechat.text(function (info, req, res, next) {
  if (info.Content === 'list') {
    res.wait('view');
  } else {
    res.reply('hehe');
    // 或者中断等待回复事务
    // res.nowait('hehe');
  }
})));

用户将收到如下回复:

回复a查看我的性别
回复b查看我的年龄
回复c查看我的性取向

用户回复其中的abc将会由注册的方法接管回复。回复可以是一个函数,也可以是一个字符串:

List.add('view', [
  ['回复{a}查看我的性别', function (info, req, res, next) {
    res.reply('我是个妹纸哟');
  }],
  // 或者字符串
  ['回复{c}查看我的性取向', '这样的事情怎么好意思告诉你啦- -']
]);

如果用户触发等待回复事务后,没有按照{}中的进行回复,那么将会由原有的默认函数进行处理。在原有函数中,可以选择调用res.nowait()中断事务。nowait()除了能中断事务外,与reply的行为一致。

Show cases

Node.js API自动回复

Node.js API自动回复机器人

欢迎关注。

代码:https://github.com/JacksonTian/api-doc-service

你可以在CloudFoundryappfogBAE等搭建自己的机器人。

详细API

原始API文档请参见:消息接口指南

目前微信公共平台能接收到7种内容:文字、图片、音频、视频、位置、链接、事件。支持6种回复:纯文本、图文、音乐、音频、图片、视频。 针对目前的业务形态,发布了0.6.x版本,该版本支持六种内容分别处理,以保持业务逻辑的简洁性。

app.use('/wechat', wechat('some token', wechat.text(function (message, req, res, next) {
  // message为文本内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359125035',
  // MsgType: 'text',
  // Content: 'http',
  // MsgId: '5837397576500011341' }
}).image(function (message, req, res, next) {
  // message为图片内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359124971',
  // MsgType: 'image',
  // PicUrl: 'http://mmsns.qpic.cn/mmsns/bfc815ygvIWcaaZlEXJV7NzhmA3Y2fc4eBOxLjpPI60Q1Q6ibYicwg/0',
  // MediaId: 'media_id',
  // MsgId: '5837397301622104395' }
}).voice(function (message, req, res, next) {
  // message为音频内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359125022',
  // MsgType: 'voice',
  // MediaId: 'OMYnpghh8fRfzHL8obuboDN9rmLig4s0xdpoNT6a5BoFZWufbE6srbCKc_bxduzS',
  // Format: 'amr',
  // MsgId: '5837397520665436492' }
}).video(function (message, req, res, next) {
  // message为视频内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359125022',
  // MsgType: 'video',
  // MediaId: 'OMYnpghh8fRfzHL8obuboDN9rmLig4s0xdpoNT6a5BoFZWufbE6srbCKc_bxduzS',
  // ThumbMediaId: 'media_id',
  // MsgId: '5837397520665436492' }
}).shortvideo(function (message, req, res, next) {
  // message为短视频内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359125022',
  // MsgType: 'shortvideo',
  // MediaId: 'OMYnpghh8fRfzHL8obuboDN9rmLig4s0xdpoNT6a5BoFZWufbE6srbCKc_bxduzS',
  // ThumbMediaId: 'media_id',
  // MsgId: '5837397520665436492' }
}).location(function (message, req, res, next) {
  // message为位置内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359125311',
  // MsgType: 'location',
  // Location_X: '30.283950',
  // Location_Y: '120.063139',
  // Scale: '15',
  // Label: {},
  // MsgId: '5837398761910985062' }
}).link(function (message, req, res, next) {
  // message为链接内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359125022',
  // MsgType: 'link',
  // Title: '公众平台官网链接',
  // Description: '公众平台官网链接',
  // Url: 'http://1024.com/',
  // MsgId: '5837397520665436492' }
}).event(function (message, req, res, next) {
  // message为事件内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359125022',
  // MsgType: 'event',
  // Event: 'LOCATION',
  // Latitude: '23.137466',
  // Longitude: '113.352425',
  // Precision: '119.385040',
  // MsgId: '5837397520665436492' }
}).device_text(function (message, req, res, next) {
  // message为设备文本消息内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359125022',
  // MsgType: 'device_text',
  // DeviceType: 'gh_d3e07d51b513'
  // DeviceID: 'dev1234abcd',
  // Content: 'd2hvc3lvdXJkYWRkeQ==',
  // SessionID: '9394',
  // MsgId: '5837397520665436492',
  // OpenID: 'oPKu7jgOibOA-De4u8J2RuNKpZRw' }
}).device_event(function (message, req, res, next) {
  // message为设备事件内容
  // { ToUserName: 'gh_d3e07d51b513',
  // FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
  // CreateTime: '1359125022',
  // MsgType: 'device_event',
  // Event: 'bind'
  // DeviceType: 'gh_d3e07d51b513'
  // DeviceID: 'dev1234abcd',
  // OpType : 0, //Event为subscribe_status/unsubscribe_status时存在
  // Content: 'd2hvc3lvdXJkYWRkeQ==', //Event不为subscribe_status/unsubscribe_status时存在
  // SessionID: '9394',
  // MsgId: '5837397520665436492',
  // OpenID: 'oPKu7jgOibOA-De4u8J2RuNKpZRw' }
})));

注意: text, image, voice, video, location, link, event, device_text, device_event方法请至少指定一个。 这六个方法的设计适用于按内容类型区分处理的场景。如果需要更复杂的场景,请使用第一个例子中的API。

更简化的API设计

示例如下:

app.use('/wechat', wechat('some token').text(function (message, req, res, next) {
  // TODO
}).image(function (message, req, res, next) {
  // TODO
}).voice(function (message, req, res, next) {
  // TODO
}).video(function (message, req, res, next) {
  // TODO
}).location(function (message, req, res, next) {
  // TODO
}).link(function (message, req, res, next) {
  // TODO
}).event(function (message, req, res, next) {
  // TODO
}).device_text(function (message, req, res, next) {
  // TODO
}).device_event(function (message, req, res, next) {
  // TODO
}).middlewarify());

该接口从0.3.x提供。

流程图

graph

诸多细节由wechat中间件提供,用户只要关注蓝色部分的业务逻辑即可。

交流群

QQ群:157964097,使用疑问,开发,贡献代码请加群。

感谢

感谢以下贡献者:

$ git summary

 project  : wechat
 repo age : 2 years, 5 months
 active   : 136 days
 commits  : 318
 files    : 32
 authors  :
   265  Jackson Tian  83.3%
    10  ifeiteng      3.1%
    10  yelo          3.1%
     4  realdog       1.3%
     4  Bruce Lee     1.3%
     3  Guo Yu        0.9%
     2  zhongao       0.6%
     2  Jesse Yang    0.6%
     2  Lu Jun        0.6%
     2  dan           0.6%
     2  wxhuang       0.6%
     1  Rogerz Zhang  0.3%
     1  Foghost       0.3%
     1  feichang.wyl  0.3%
     1  feit          0.3%
     1  feitian124    0.3%
     1  LiSheep       0.3%
     1  p13766        0.3%
     1  Lance Li      0.3%
     1  Chen Wei      0.3%
     1  xianda        0.3%
     1  Qun Lin       0.3%
     1  TooBug        0.3%

捐赠

如果您觉得Wechat对您有帮助,欢迎请作者一杯咖啡

捐赠wechat

或者

License

The MIT license.

wechat's People

Contributors

chenweiyj avatar cody2333 avatar feifeiiiiiiiiiii avatar feitian124 avatar foghostcn avatar greenkeeperio-bot avatar guo-yu avatar ifivebig avatar imyelo avatar ityao avatar jacksontian avatar jinasonlin avatar jkshare avatar ktmud avatar lanceli avatar lisheep avatar luj1985 avatar p13766 avatar rogerz avatar shawnduxy avatar suzukua avatar terry-fei avatar toobug avatar wxhuang avatar xjmalm 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  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

wechat's Issues

微信小店API接口

  • 商品管理接口
  • 库存管理接口(@bzshow 认领)
  • 邮费模版管理接口
  • 分组管理接口(@feichang 认领)
  • 货架管理接口(@ifeit 认领)
  • 订单管理接口(@ifeit 认领)
  • 功能接口

欢迎开发者或用户赞助该功能的开发,细节较多,买杯咖啡也不错。或者直接代码的Pull request也是不错的。

支付宝赞助:
捐赠wechat

赞助者将会罗列在首页README文件中。

开发者可以参见#82 进行contribute。

adapt to express/connect api change

connect.query, connect.cookieParser, connect.session ...... are no longer available.

  1. This lib won't work with connect anymore due to lack of req.query in connect
  2. Lots of examples need to be fixed

等待回复加载问题

我是一个node.js新手,我在使用您的等待回复功能时候遇到一个问题:
我依样画葫芦维护着一个List,但是这个List出现了下面两个情况
1、程序的List是在服务器启动时候访问一个url加载的,那么服务器奔溃后,无法自动从新加载List,我的代码: https://github.com/LiSheep/sam_wechat/blob/master/wechat/chat.js#L96
2、这个List是在数据库中维护的,因此可能在运行时改变它。

不知道您有没有什么方法能解决它,或者您在设计这个API的时候,是希望我们怎么使用呢?
Many thanks

安装失败, why ?

npm http GET https://registry.npmjs.org/wechat
npm http 304 https://registry.npmjs.org/wechat
npm http GET https://registry.npmjs.org/wechat/-/wechat-0.7.2.tgz
npm http 200 https://registry.npmjs.org/wechat/-/wechat-0.7.2.tgz
npm ERR! Error: shasum check failed for /var/folders/2m/jc7ycxx97qd6n1nd_xhg8y7r0000gn/T/npm-2041-p2FB497V/1404319660472-0.6132024084217846/tmp.tgz
npm ERR! Expected: a3e47bc8ce0c22a829e0b888a50f877e53ad3fb1
npm ERR! Actual:   8fc39577777eada28da294adaa0809a58653bb47
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/sha/index.js:38:8
npm ERR!     at ReadStream.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/sha/index.js:85:7)
npm ERR!     at ReadStream.EventEmitter.emit (events.js:117:20)
npm ERR!     at _stream_readable.js:920:16
npm ERR!     at process._tickCallback (node.js:415:13)
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR!     <http://github.com/isaacs/npm/issues>

npm ERR! System Darwin 13.3.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "wechat"
npm ERR! cwd /Users/rocky/DEV/weixin
npm ERR! node -v v0.10.24
npm ERR! npm -v 1.3.21
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/rocky/DEV/weixin/npm-debug.log
npm ERR! not ok code 0

谢谢 :)

QRCode URL内容为空

还有个问题哈,生成出来的QRCode URL页面是空白的。代码如下

app.get('/qrcode', function(req, res) {
  var codeType = req.query.codeType || 'temp';
  var codeVal = req.query.codeVal || 1;
  var codeExp = req.query.codeExp || 1800;
  var response = function(err, data) {
    if(err) console.log(err);
    res.end( api.showQRCodeURL(data.ticket) );
  };

  if( codeType.toLowerCase() == 'temp' ) {

    api.createTmpQRCode(codeVal, codeExp, response);

  } else if( codeType.toLowerCase() == 'perm' ) {

    api.createLimitQRCode(codeVal, response);

  }

});

附赠刚刚生成的URL一个 https://api.weixin.qq.com/cgi-bin/showqrcode?ticket=gQG38DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3JFenpFcDNsRkVSdFZ5N0FBMkt3AAIEbauRUgMECAcAAA==

为什么要对回复 music 消息特殊处理?

Explicit is better than implicit.

如果非文本消息都强制成

res.reply({
  type: 'voice',
  content: {
      /// props...
 }
})

我能理解,但为什么要对 music 特殊处理?因为比较常用?还是为了向后兼容?但其实现在并没有兼容 0.4.x 以下的写法:

res.reply({
  type: 'music',
  title: 'xxx',
  musicurl: '',
  thumb_media_id: ''
});

询一下关于wechat获得token的问题

首先,我下载“api-doc-service”示例工程
在根目录下增加了config.js文件,内容如下:

var wechat = require('wechat');
module.exports={
secret:'myweixin',
url:'mongodb://localhost/WeixinManage',
domain:'http://www.aaa.cn',
token:function(callback){
var API = require('wechat').API;
var api = new API('11111111111', '22222222222222222222222222222');
return api.getAccessToken(callback);
}
};

同时,在微信公众平台,将认证信息修改如下:
URL: http://www.aaa.cn/wechat
Token : bbbbb

App.js去掉了不相关的部分,主要内容如下:
var app = connect();
app.set('port', process.env.PORT || 80);
app.use(connect.query());
app.use(connect.cookieParser());
app.use(connect.session({secret: config.secret}));

/下面这行代码一直没有进入呢?是引入token的方式有问题?/
app.use('/wechat', wechat(config.token, wechat.text(function (message, req, res) {
console.log(message);
})));

http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});

我在微信上输入的任何消息,都没有响应,控制台输出也没有反映,我主要怀疑是config.js在处理AccessToken有问题?

用户在POI信息没完全刷出来的时候发送地理位置会导致报错

/var/api/node_modules/wechat/lib/wechat.js:76
message[key] = (result.xml[key][0] || '').trim();
^
TypeError: Object # has no method 'trim'
at formatMessage (/var/api/node_modules/wechat/lib/wechat.js:76:47)
at respond (/var/api/node_modules/wechat/lib/wechat.js:85:21)
at exports.Parser.Parser.parseString (/var/api/node_modules/wechat/node_modules/xml2js/lib/xml2js.js:235:20)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)

能否考虑与 weixin-robot 项目合并?

@atian25 的大力推广下, weixin-robot 模块有了一定的用户量,
也很高兴看到你的中间件关注人数也越来越多。

目前看来,两边做的事情有大部分都是相似的,不同的地方则可以互补。

之前看到你也给 weixin-robot 发过 PR ,不知为何后来还是决定自己写一个呢?

如果觉得代码不符合自己习惯,可以发 PR 来重构嘛,比如 @atian25 就基本把我之前
写的 0.0.x 版本完全重写了。而现在我又删了他很多代码。
如果觉得项目不在自己名下,无法掌控,可以建一个组织来协作嘛。

weixin-robot 项目已经分拆成 weixin-robotwebot-cliwebot-dashboard 三个项目。
计划中这是一整套公共平台API的解决方案,包括命令行工具和一个网页端的后台。

不希望看到更多重复造轮子的事,前端界已经够分裂了。

node 社区也不是没有这样的先例:

比如 node_mailer 就被合并到了 Nodemailer 。

用app.use时报404

当我用你们给出的文档, 用app.use('/wechat', wechat.token('xxx') 实用时打开路径localhost/wechat时直接报404错误,
但我用app.get('/wechat', 时报401的invalid signature,
是我少设置了什么地方吗?
望帮忙, 谢谢:)

调用getMedia网络连接超时抛出异常

调用getMedia网络连接超时,node抛出来异常了。

[2014-07-29 10:08:02.006] [ERROR] console - TypeError: Cannot read property 'headers' of undefined
at /home/hanscc/nodejs/wechat-service/node_modules/wechat/lib/common.js:958:26
at /home/hanscc/nodejs/wechat-service/node_modules/wechat/lib/util.js:9:14
at done (/home/hanscc/nodejs/wechat-service/node_modules/wechat/node_modules/urllib/lib/urllib.js:257:5)
at ClientRequest. (/home/hanscc/nodejs/wechat-service/node_modules/wechat/node_modules/urllib/lib/urllib.js:463:5)
at ClientRequest.emit (events.js:95:17)
at Socket.socketErrorListener (http.js:1547:9)
at Socket.emit (events.js:95:17)
at net.js:440:14
at process._tickDomainCallback (node.js:463:13)

Stupid question continued

Sorry I close the issue by mistake.

The following code based on the sample in readme ends immediately without error message. Please help.

// code starts

var express = require('express')
var connect = require('connect')

var app = express();
var wechat = require('wechat');

app.use(connect.query()); // Or app.use(express.query());
app.use('/wechat', wechat('albert', function (req, res, next) {
// 微信输入信息都在req.weixin上
var message = req.weixin;
if (message.MsgType = 'text') res.reply('Testing. Please ignore');
}));

npm install error

npm install wechat reported "shasum check failed for wechat-0.8.0.tgz"
Expected: 8504e98ceab33823321b52f74f206124bf8d43c4
Actual: 1f577417ef8d6f7ec43552dbcb68d73d2e05f1d8

奇怪的 preRequest()

首先想跟作者说一声谢谢,Contribute 这些代码出来,非常感谢。

我并不是 JS 高手,所以没有了范例代码就完全没有了办法。按照 wechat 的源代码,我必须将 api.sendText() 和用到的参数弄进去 preRequest() 里面... Comment 里面说需要 “封装” 进去 preRequest(),而我是真的想破了头都不知道怎么在 JS 进行代码封装... 我在用 wechat 的时候,也遇到一些怪咖语法的问题。如:

wechat = require 'wechat'
api.use '/wechat', wechat api.locals.wechat_token, (req, res, next) ->

我的想法是,为什么不直接 api.sendText('Hello World') 呢?如果真的需要 preRequest(),为什么不采用 api.preRequest().sendText('') 的方式呢?Mongoose 也是采用这种方式,像我这些对编程不熟悉的人而言,wechat 的接口设计太难懂了。如果代码能够变成下面的方式的话,我觉得比较好理解...

wechat = require 'wechat'
api.use wechat('url','token')

wechat.receivedData = (data,req,res,next) ->
  wechat.api.getUser data.FromUserName, (error, result) ->
    console.log result

这是我的一些很幼稚的想法。见笑了。如果我的想法有问题的话,不知道可不可以请教一下怎么用 preRequest()。万分感谢。

获取access_token有问题

API.prototype.preRequest方法139行

如果从全局获取到的access_token为空,则应该想微信api发起请求.目前逻辑不是这样做的

Stupid question

I am new to everything. You are great to provide this module. I want to write a program to process messages sent from wechat users. I tried your sample (those with express/connect in readme) but the program terminates immediately without error message. What's wrong? Please help. Many thanks.

getFollowers方法Invalid OpenID

我只想获取前一万名用户,如果使用

api.getFollowers(callback); 
api.getFollowers('', callback);

但是getFollowers总是回复{ [WeChatAPIError: invalid openid] name: 'WeChatAPIError' }
而且有时还会报错说callback里面的res没有定义

请求增加等待回复的功能

譬如给用户下发一个列表,由用户回复列表中的选项,类似于运营商的业务短信那样,可否由你的组件代劳实现个大体框架?

隐藏接口

回复

图片回复

 <xml>
 <ToUserName><![CDATA[toUser]]></ToUserName>
 <FromUserName><![CDATA[fromUser]]></FromUserName>
 <CreateTime>12345678</CreateTime>
 <MsgType><![CDATA[image]]></MsgType>
 <Image>
 <MediaId><![CDATA[media_id]]></MediaId>
 </Image>
 <FuncFlag>0</FuncFlag>
 </xml>

语音回复

<xml>
 <ToUserName><![CDATA[toUser]]></ToUserName>
 <FromUserName><![CDATA[fromUser]]></FromUserName>
 <CreateTime>12345678</CreateTime>
 <MsgType><![CDATA[voice]]></MsgType>
 <Voice>
 <MediaId><![CDATA[media_id]]></MediaId>
 </Voice>
 <FuncFlag>0</FuncFlag>
 </xml>

视频回复

<xml>
 <ToUserName><![CDATA[toUser]]></ToUserName>
 <FromUserName><![CDATA[fromUser]]></FromUserName>
 <CreateTime>12345678</CreateTime>
 <MsgType><![CDATA[video]]></MsgType>
 <Video>
 <MediaId><![CDATA[media_id]]></MediaId>
 <ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
 </Video> 
 <FuncFlag>0</FuncFlag>
 </xml>

其余接口

用户信息接口

媒体文件上传接口

媒体文件获取接口

客服发送消息接口

拉取关注接口

自定义模板消息推送接口

安装失败

在我的项目的package.json里:
"wechat": ">= 0.3.5"

执行npm install时报错:
npm ERR! Error: No compatible version found: bufferhelper@'>=0.2.0'

但是我看 bufferhelper 这个包已然是0.2.0版本的了。。。

上传文件超时

试了两个文件,一个3.1M,一个350KB左右。都是超时。文件读取正常。

{ [WeChatAPIConnectionTimeoutError: Request#2 timeout for 60000ms, POST http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=Eb0562_W9pFq4EY0IM5pHc-ybbeuA00DfBav48ZUGK0JFw4WC__HAZy8_okXJdEwJCQHClRn14ZTz2SiWxKUPf5bBHnZJLVqXLY-ZVuvY1Pz0pp4ZWX0ByA_IAJRQcYmdY64MjHhEdfh9aR88SDhiw&type=voice] name: 'WeChatAPIConnectionTimeoutError' }

服务器没有在大陆,但是反应速度也是可以的。

checkSignature() is not exported?

I have found that I cannot call the function checkSignature() in lib/wechat. Although I can write the checkSignature() in my code, it is better to export it for others to use. Thank you for such nice library.

用uploadMedia批量上传抛40001错误

我要上传一个文件夹的图片到微信服务器,遍历文件夹然后循环对每个文件进行uploadMedia操作,但是返回

{ errcode: 40001, errmsg: 'invalid credential' }

初步猜测是异步的问题,可能微信服务器对特定的access_token一次只能调用一次上传接口。现在不知如何解决。请问有什么方法?谢谢

OAuth.getAccessToken() 返回result结构错误

结果被'data'包起来了

console.log(result):

{ data:
   { access_token: 'OezXcEiiBSKSxW0eoylIeBjK8ITRYrk_JWfjtULHy5fpq4tnIrgI27BpD7psevpeUCC85evE8hR9KqrEfIg0TcgoVm3XT6HdabAxijfvBPo4uQPu_lMealUR1Kycd9YzwTf6BSR578O4xEwyLxvE0w',
     expires_in: 7200,
     refresh_token: 'OezXcEiiBSKSxW0eoylIeBjK8ITRYrk_JWfjtULHy5fpq4tnIrgI27BpD7psevpeoFH82IgRFDEGO2OkY3A8wsTkzGJcrus1hhJJfS5kWnCVcQTxl4ZbTn-8ngJSr-niJVzOjIUy2P5na6qoALwNtg',
     openid: 'ow6lZuE3X5iCHO2UcCFFwx1qx-bo',
     scope: 'snsapi_base',
     create_at: 1409305069037 } }

全局 access_token实现建议

除了access_token的问题,还发现access_token实现方式有待商榷.

有2个服务,如果某个服务在access_token未过期时刷新access_token,则另一个进程将无法继续提供服务,直到上一次的access_token过期为止.

解决办法是在全局access_token模式下, 每次发起request时候重新从redis获取access_token.

No docs in English

Would you consider translating the documentation to English?

I can't read Chinese but am in need of your library. ;)

谢谢

微信内置表情代码

//微信内置表情代码
module.exports = {
  wx : '/::)', //微笑
  pz : '/::~', //撇嘴
  se : '/::B', //色
  fd : '/::|', //发呆
  dy : '/:8-)', //得意
  ll : '/::<', //流泪
  hx : '/::$', //害羞
  bz : '/::X', //闭嘴
  shui : '/::Z', //睡
  dk : '/::\'(', //大哭
  gg : '/::-|', //尴尬
  fn : '/::@', //发怒
  tp : '/::P', //调皮
  cy : '/::D', //呲牙
  jy : '/::O', //惊讶
  ng : '/::(', //难过
  kuk : '/::+', //酷
  lengh : '/:--b', //冷汗
  zk : '/::Q', //抓狂
  tuu : '/::T', //吐
  tx : '/:,@P', //偷笑
  ka : '/:,@-D', //可爱
  baiy : '/::d', //白眼
  am : '/:,@o', //傲慢
  jie : '/::g', //饥饿
  kun : '/:|-)', //困
  jk : '/::!', //惊恐
  lh : '/::L', //流汗
  hanx : '/::>', //憨笑
  db : '/::,@', //大兵
  fendou : '/:,@f', //奋斗
  zhm : '/::-S', //咒骂
  yiw : '/:?', //疑问
  xu : '/:,@x', //嘘...
  yun : '/:,@@', //晕
  zhem : '/::8', //折磨
  shuai : '/:,@!', //衰
  kl : '/:!!!', //骷髅
  qiao : '/:xx', //敲打
  zj : '/:bye', //再见
  ch : '/:wipe', //擦汗
  kb : '/:dig', //抠鼻
  gz : '/:handclap', //鼓掌
  qd : '/:&-(', //糗大了
  huaix : '/:B-)', //坏笑
  zhh : '/:<@', //左哼哼
  yhh : '/:@>', //右哼哼
  hq : '/::-O', //哈欠
  bs : '/:>-|', //鄙视
  wq : '/:P-(', //委屈
  kk : '/::\'|', //快哭了
  yx : '/:X-)', //阴险
  qq : '/::*', //亲亲
  xia : '/:@x', //吓
  kel : '/:8*', //可怜
  cd : '/:pd', //菜刀
  xig : '/:<W>', //西瓜
  pj : '/:beer', //啤酒
  lq : '/:basketb', //篮球
  pp : '/:oo', //乒乓
  kf : '/:coffee', //咖啡
  fan : '/:eat', //饭
  zt : '/:pig', //猪头
  mg : '/:rose', //玫瑰
  dx : '/:fade', //凋谢
  sa : '/:showlove', //示爱
  xin : '/:heart', //爱心
  xs : '/:break', //心碎
  dg : '/:cake', //蛋糕
  shd : '/:li', //闪电
  zhd : '/:bome', //炸弹
  dao : '/:kn', //刀
  zq : '/:footb', //足球
  pch : '/:ladybug', //瓢虫
  bb : '/:shit', //便便
  yl : '/:moon', //月亮
  ty : '/:sun', //太阳
  lw : '/:gift', //礼物
  yb : '/:hug', //拥抱
  qiang : '/:strong', //强
  ruo : '/:weak', //弱
  ws : '/:share', //握手
  shl : '/:v', //胜利
  bq : '/:@)', //抱拳
  gy : '/:jj', //勾引
  qt : '/:@@', //拳头
  cj : '/:bad', //差劲
  aini : '/:lvu', //爱你
  bu : '/:no', //NO
  hd : '/:ok', //OK
  aiq : '/:love', //爱情
  fw : '/:<L>', //绯闻
  tiao : '/:jump', //跳跳
  fad : '/:shake', //发抖
  oh : '/:<O>', //怄火
  zhq : '/:circle', //转圈
  kt : '/:kotow', //磕头
  ht : '/:turn', //回头
  tsh : '/:skip', //跳绳
  hsh : '/:oY', //挥手
  kaixin : String.fromCharCode(58389), //开心
  kouzhao : String.fromCharCode(58380), //口罩
  daxiao : String.fromCharCode(58386), //笑出泪来
  tushe : String.fromCharCode(58377), //吐舌头
  mengle : String.fromCharCode(58384), //懵了
  jingya : String.fromCharCode(58381), //惊讶
  guichu : String.fromCharCode(57607), //鬼怵
  beishang : String.fromCharCode(58371), //悲伤
  zhayan : String.fromCharCode(58373), //眨眼
  qieyi : String.fromCharCode(58378), //惬意
  qie : String.fromCharCode(58382), //切~
  mogui : String.fromCharCode(57626), //魔鬼
  youling : String.fromCharCode(57627), //幽灵
  aixin : String.fromCharCode(58423), //爱心
  heshi : String.fromCharCode(58397), //双手合十
  jirou : String.fromCharCode(57676), //肌肉
  qiandai : String.fromCharCode(57647), //钱袋
  dangao : String.fromCharCode(58187), //蛋糕
  qingzhu : String.fromCharCode(58130), //庆祝
  liwu : String.fromCharCode(57618) //礼物
}

https://npmjs.org/package/node-wechat-expression

getAccessToken result of oauth

准备升级到最新版本时候发现, d03e9dd 修改了getAccessToken的result, 增加了一层data, 是有意的吗? 不过注释部分没修改~

new result:

{
  data: 
  {
   "access_token": "ACCESS_TOKEN",
   "expires_in": 7200,
   "refresh_token": "REFRESH_TOKEN",
   "openid": "OPENID",
   "scope": "SCOPE"
  }
}

old result:

{
 "access_token": "ACCESS_TOKEN",
 "expires_in": 7200,
 "refresh_token": "REFRESH_TOKEN",
 "openid": "OPENID",
 "scope": "SCOPE"
}

workflow

添加工作流支持,能够像电话问答一样。

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.