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

wechat-mp

v0.2.7

Published

Wechat open API utilities.

Downloads

61

Readme

wechat-mp 微信公众平台消息接口中间件 Build Status

Utilities for wechat offical account messaging API.

校验签名,接受并解析微信消息,处理回复内容为 XML ,并回复给微信。

如需使用自定义菜单等高级接口,可使用 wechat-api 模块。

Express Middlewares

本模块主要作为 Connect/Express 框架的中间件使用:

var mp = require('wechat-mp')(process.env.WX_TOKEN)
var app = require('express')()

app.use('/wechat', mp.start())
app.post('/wechat', function(req, res, next) {

  console.log(req.body)

  res.body = {
    msgType: 'text',
    content: 'Hi.'
  }

  // or rich media message
  res.body = {
    msgType: 'music',
    content: {
      title: 'A beautiful song',
      musicUrl: 'http://.....'
    },
  }

  next()
}, mp.end())

如果要在 koa 里使用,可尝试 koa-wechat 模块。

require('wechat-mp')( [options] )

options can be either the token string or an object. You can use these options both when initialization(mp = require('wechat-mp')(options)) and mp.start().

options.token

The token for wechat to check signature.

options.tokenProp

Default: 'wx_token'

Will try get req[tokenProp] as token. Good for dynamically set token.

options.dataProp

Default: 'body'

Will put parsed data on req[dataProp]. So you can access wechat request message via req.body or req.wx_data, etc.

Parsed data properties mapping

We changed some of the properties' names of Wechat's incoming message, to make it more "JavaScript like", typically, a request datum would be:

{
  uid: 'xahfai2oHaf2ka2M41',      // FromUserName
  sp: 'gh_xmfh2b32tmgkgagsagf',   // ToUserName
  type: '',                       // MsgType
  createTime: new Date(2014-12..) // CreateTime
  text: 'Hi.',                    // when it's a text message
  param: {
    lat: '34.193819105',          // for a "LOCATION" message's Location_X
    lng: '120.2393849201',        // Location_Y
  }
}

For more details, please refer to lib/xml.js.

options.session

Unless options.session is set to false, the mp.start() middleware will set req.sessionID and req.sessionId to "wx.#{toUserName}.#{fromUserName}". So you can use req.session to save information about one specific user.

The sessionId cannot be changed by any other following middlewares.

To make this work, mp.start() must go before express/connect's session middleware.

app.use('/wechat', mp.start())
app.use(connect.cookieParser())
app.use(connect.session({ store: ... }))

mp.start()

The starting middleware, to parse a Wechat message request, and set req.body as a JS object.

mp.end()

The ending middleware, to response a xml based on res.body. For how to set res.body for multi media messages, see source code lib/xml.js.

If your set res.body to a string, will reply a text message. When res.body is an object, a res.body.msgType is expected, otherwise it will be treated as text message, and res.body.content will be replied as it was a string.

A typical response:

{
  msgType: 'news',
  content: [{
    title: 'news 1',
    url: 'http://...',
    picUrl: 'http://...'
  }, {
    title: 'news 2',
    url: 'http://...',
    picUrl: 'http://...'
  }]
}

weixin-robot

使用 wexin-robot 模块,更傻瓜化地定义自动回复功能。

调试

使用 webot-cli 调试发送测试消息。

License

the MIT license.