npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nodejs-wechat

v0.0.8

Published

nodejs wrapper of wechat(weixin) api - well tested and bug-free

Downloads

12

Readme

Nodejs Wechat

NPM version Build Status

Nodejs wrapper of wechat api

Usage

Work with native http server

var http = require('http');
var xmlBodyParser = require('express-xml-parser');
var Wechat = require('nodejs-wechat');

var opt = {
  token: 'TOKEN',
  url: '/'
};
var parse = xmlBodyParser({
  type: 'text/xml'
});
var wechat = new Wechat(opt);
wechat.on('event.subscribe', function(session) {
  session.replyTextMsg('欢迎您关注我们的订阅号');
});
var server = http.createServer(function(req, res) {
  if (req.method === 'GET') {
    wechat.verifyRequest(req, res);
  } else {
    parse(req, res, function(err) {
      if (err) {
        res.end();
        return;
      }
      wechat.handleRequest(req, res);
    });
  }
});
server.listen(80);

Work with express

var express = require('express');
var app = express();
var middlewares = require('express-middlewares-js');
app.use('/weixin', middlewares.xmlBodyParser({
  type: 'text/xml'
}));

/*
  Alternative way

var xmlBodyParser = require('express-xml-parser');
app.use('/weixin', xmlBodyParser({
  type: 'text/xml',
  limit: '1mb'
}));

*/

var Wechat = require('nodejs-wechat');
var opt = {
  token: token,
  url: '/weixin'
};
var wechat = new Wechat(opt);

app.get('/weixin', wechat.verifyRequest.bind(wechat));
app.post('/weixin', wechat.handleRequest.bind(wechat));

// you can also work with other restful routes
app.use('/api', middlewares.bodyParser());

wechat.on('text', function(session) {
  session.replyTextMsg('Hello World');
});
wechat.on('image', function(session) {
  session.replyNewsMsg([{
    Title: '新鲜事',
    Description: '点击查看今天的新鲜事',
    PicUrl: 'http://..',
    Url: 'http://..'
  }]);
});
wechat.on('voice', function(session) {
  session.replyMsg({
    Title: 'This is Music',
    MsgType: 'music',
    Description: 'Listen to this music and guess ths singer',
    MusicUrl: 'http://..',
    HQMusicUrl: 'http://..',
    ThumbMediaId: '..'
  });
});

app.listen(80);

NOTE: We apply { type: 'text/xml' } to xmlBodyParser as weixin server send us a text/xml content type instead of application/xml.

API

Wechat

  • #verifyRequest(req, res)

    This is a express/connect middleware, which verify the signature of request from weixin server

  • #handleRequest(req, res)

    This is a express/connect middleware, which handle the request post from weixin server

  • #on(msgType, handler)

    Wechat is an inheritance from event.EventEmitter. Wechat will emit an event in incoming message's MsgType, with a Session as parameter. Valid events:

    text, image, voice, video, location, link, event.subscribe, event.unsubscribe, event.SCAN, event.LOCATION, event.CLICK, event.VIEW, error

    References: 接收普通消息, 接收事件推送

Session

  • incomingMessage

    This is a direct parse of weixin server request

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>

Becomes

{
  "ToUserName": "toUser",
  "FromUserName": "FromUser",
  "CreateTime": "123456789",
  "MsgType": "event",
  "Event": "subscribe"
}
  • req

    This is the request from weixin server

  • res

    This is the response to weixin server

  • #replyMsg(msgObject)

    Reply a message via this.res

  • #replyTextMessage(content)

    Reply a text message

  • #replyNewsMessage(articles)

    Reply a news messages.

TODO

  • Advanced interfaces

    Will finish advanced interfaces before July/2014, welcome send pull requests :)