0.0.12 • Published 10 years ago

wechat-es v0.0.12

Weekly downloads
16
License
MIT
Repository
github
Last release
10 years ago

wechat-es

In Progress,Do Not Use It!

本项目起源于搭建微信公众号服务器

安装

npm i -S wechat-es

功能构成

跟微信公众号开放的API相对应,库主要提供两个服务类,分别是WaiterTalker,可以在测试中看到这两个类的主要用法。

Waiter

Waiter负责接收公众号的消息,进行签名的验证,还可以将响应封装成XML。

npm.io

下面这个例子需要用到express和xmlparser:

npm i -S express
npm i -S express-xml-bodyparser

然后创建App,定义路由处理请求:

const app = express()
//Initialize an instance of Weixin
const weixin = new Weixin({
	appId: 'YourAppId',
	appSecret: 'YourAppSecret',
	token: 'Token'
})
const waiterUrl = '/api/weixin'

app.use(express.query())
/**
* Process verify signature request with get method
*/
app.get(waiterUrl, function (req, res, next) {
  const waiter = weixin.getWaiter()
  const {
		signature,timestamp,nonce
	} = req.query

	if(waiter.verifySignature(signature,timestamp,nonce)) {
		res.send(req.query.echostr)
	} else {
		res.send('error')
	}
});
/**
* Process other requests with post method
* We use xmlparser to convert xml in request body to json
*/
app.post(waiterUrl,xmlparser({trim: true, explicitArray: false}),function (req, res, next) {
  const waiter = weixin.getWaiter()
  /**
  * Your need provide a populateReply method for waiter to
  * response what you want.The default implement just reply
  * an text message to say 'I received your message'
  */
  //waiter.populateReply = function(message) {}

  const reply = waiter.process(req)
  res.set('Content-Type', 'text/xml')
  res.send(reply)
});

Talker

Talker 负责给微信公众号服务器发送消息,可以在任何地方使用,用manager创建消息,然后send这个消息就可以了。

npm.io

const talker = weixin.getTalker()
talker.send(UserManager.getList())
		  .then(result => {
		  	if(!result.errcode) {		  		
				  console.log("Get users:",json)
		  		return CustomerMessageManager.text(result.next_openid,"感谢订阅")
		  	} else {
		  		console.log("Get user list got error:",result.errmsg)
				throw Error(result.errmsg)
		  	}
		  })
		  .then(message => talker.send(message))
		  .catch(error => {
		  	console.log("There is an error to get nextOpenId")
		  })
		  .then(result => {
			  	if(result.errcode) {		  		
					console.log(`Send text message to customer ${nextOpenId} got error:${result.errmsg}`)
			  	} else {
			  		console.log(`Send text message to ${nextOpenId} success`)		  		
			  	}
		   })

开发者参考

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago