Git Product home page Git Product logo

co-wx's Introduction

co-wx

Yet another WeChat toolkit in generator style.

Installation

npm install --save co-wx

BodyParser

A connect style request body parser, checks signature of message and parses WeChat message body to req.body;

var WxParser = require('co-wx/parser');

app.all('/robot', WxParser('<signature-token>'), function (req, res, next) {
  var msgType = req.body['MsgType'];
  switch (msgType) {
    case 'event':
      var eventType = req.body['Event']; // => [ 'subscribe' | 'SCAN' | 'LOCATION' | 'CLICK' | 'VIEW' | 'unsubscribe' ]
      // ...
      return res.reply({
        MsgType: 'text',
        Content: 'No comment'
      });
    break;
    case 'text':
      // ...
    break;
  }
  return res.status(200).send(); // Stuffy sound made big money o-o
});

Use res.reply will reply a xml document described as official documentation. FromUserName and ToUserName in the reply object defaults to the value of ToUserName and FromUserName from the incoming message. And a CreateTime record will be generated automatically in the xml document.

Platform API

var WxAPIProvider = require('co-wx/api');

var wxApi = new WxAPIProvider({
  appId: '<your-app-id>',
  secret: '<and-the-secret>'
});

Retrieve information of specific subscriber

var co = require('co');

co(function* () {
  var subscriberInfo = yield wxApi.getSubscriberInfo('<open-id>');
  // => { openId, unionId, nickname, gender, photo, country, province, city, lang }
})
.then(function () { /* ... */ });

Use with a co-router

var Router = require('co-router');

var router = Router();

router.get('/user/:openId', function* (req, res, next) {
  return res.status(200).send(yield wxApi.getSubscriberInfo(req.params.openId));
});

app.use('/some-prefix', router);

Sending a service template message to subscriber

yield wxApi.sendTemplateMessageToSubscriber('<template-id>', openId, url, {
  /* template data */
});

Create a temporary scene based QRCode

var ticket = yield wxApi.createTemporaryQrCode(sceneId, expiresIn);

expiresIn parameter is optional and defaults to maximum value of 7 days. returns a ticket object contains QRCode ticket and url data.

JSSDK parameter API for wx.config (use co-router)

router.get('/config', function* (req, res, next) {
  return res.status(200).send(yield req.wx.getJsApiConfig(req.query.url || req.get('Referer')));
});

Debug

Start with DEBUG variable set to wx:* should turn on debug output of the module.

DEBUG=wx:* npm start

Roadmap

  • Basic message handling
  • Support AES message security mode
  • Support customer service account interface
  • Support service template message
  • Message broadcast
  • Media management
  • Menu management
  • Subscriber group/tag management
  • Support OAuth 2.0
  • Generate scene based QRCode
  • Generate JS-SDK config
  • URL shortener
  • Statistics API
  • Koa-compatible middleware
  • ...

Contribution

The module is developed and used in some of my personal works. I may not implement a full set of APIs described in WeChat documentation. Feel free to open an issue for any feature request or contribution. I'll pick them up in my spare time :-)

License

(The MIT License)

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.