{"id":13474110,"url":"https://github.com/node-webot/wechat-mp","last_synced_at":"2025-04-23T18:22:39.411Z","repository":{"id":8139849,"uuid":"9558535","full_name":"node-webot/wechat-mp","owner":"node-webot","description":"Utilities for wechat media platform API.","archived":false,"fork":false,"pushed_at":"2016-04-13T10:38:51.000Z","size":45,"stargazers_count":67,"open_issues_count":1,"forks_count":27,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-15T15:43:49.513Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/node-webot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-04-20T03:13:40.000Z","updated_at":"2025-04-14T17:47:02.000Z","dependencies_parsed_at":"2022-08-05T11:30:13.053Z","dependency_job_id":null,"html_url":"https://github.com/node-webot/wechat-mp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fwechat-mp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fwechat-mp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fwechat-mp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fwechat-mp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-webot","download_url":"https://codeload.github.com/node-webot/wechat-mp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250487990,"owners_count":21438697,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T16:01:09.615Z","updated_at":"2025-04-23T18:22:39.388Z","avatar_url":"https://github.com/node-webot.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# wechat-mp 微信公众平台消息接口中间件 [![Build Status](https://travis-ci.org/node-webot/wechat-mp.png?branch=master)](https://travis-ci.org/node-webot/wechat-mp)\n\nUtilities for wechat offical account messaging API.\n\n校验签名，接受并解析微信消息，处理回复内容为 XML ，并回复给微信。\n\n如需使用自定义菜单等高级接口，可使用 [wechat-api](https://www.npmjs.org/package/wechat-api) 模块。\n\n## Express Middlewares\n\n本模块主要作为 Connect/Express 框架的中间件使用：\n\n```javascript\nvar mp = require('wechat-mp')(process.env.WX_TOKEN)\nvar app = require('express')()\n\napp.use('/wechat', mp.start())\napp.post('/wechat', function(req, res, next) {\n\n  console.log(req.body)\n\n  res.body = {\n    msgType: 'text',\n    content: 'Hi.'\n  }\n\n  // or rich media message\n  res.body = {\n    msgType: 'music',\n    content: {\n      title: 'A beautiful song',\n      musicUrl: 'http://.....'\n    },\n  }\n\n  next()\n}, mp.end())\n```\n\n如果要在 [koa](http://koajs.com/) 里使用，可尝试 [koa-wechat](https://www.npmjs.org/package/koa-wechat) 模块。\n\n\n### require('wechat-mp')( *[options]* )\n\n`options` can be either the token string or an object.\nYou can use these options both when initialization(`mp = require('wechat-mp')(options)`)\nand `mp.start()`.\n\n\n#### options.token\n\nThe token for wechat to check signature.\n\n#### options.tokenProp\n\nDefault: 'wx\\_token'\n\nWill try get `req[tokenProp]` as token. Good for dynamically set token.\n\n#### options.dataProp\n\nDefault: 'body'\n\nWill put parsed data on `req[dataProp]`. So you can access wechat request message via `req.body` or `req.wx_data`, etc.\n\n##### Parsed data properties mapping\n\nWe changed some of the properties' names of Wechat's incoming message, to make it more \"JavaScript like\",\ntypically, a request datum would be:\n\n```js\n{\n  uid: 'xahfai2oHaf2ka2M41',      // FromUserName\n  sp: 'gh_xmfh2b32tmgkgagsagf',   // ToUserName\n  type: '',                       // MsgType\n  createTime: new Date(2014-12..) // CreateTime\n  text: 'Hi.',                    // when it's a text message\n  param: {\n    lat: '34.193819105',          // for a \"LOCATION\" message's Location_X\n    lng: '120.2393849201',        // Location_Y\n  }\n}\n```\n\nFor more details, please refer to `lib/xml.js`.\n\n\n#### options.session\n\nUnless `options.session` is set to `false`,\nthe `mp.start()` middleware will set `req.sessionID` and `req.sessionId`\nto `\"wx.#{toUserName}.#{fromUserName}\"`.\nSo you can use `req.session` to save information about one specific user.\n\nThe `sessionId` cannot be changed by any other following middlewares.\n\nTo make this work, `mp.start()` must go before express/connect's session middleware.\n\n```\napp.use('/wechat', mp.start())\napp.use(connect.cookieParser())\napp.use(connect.session({ store: ... }))\n```\n\n### mp.start()\n\nThe starting middleware, to parse a Wechat message request, and set `req.body` as a JS object.\n\n### mp.end()\n\nThe ending middleware, to response a xml based on `res.body`.\nFor how to set `res.body` for multi media messages, see source code `lib/xml.js`.\n\nIf your set `res.body` to a string, will reply a text message. When `res.body` is an object,\na `res.body.msgType` is expected, otherwise it will be treated as text message,\nand `res.body.content` will be replied as it was a string.\n\nA typical response:\n\n```js\n{\n  msgType: 'news',\n  content: [{\n    title: 'news 1',\n    url: 'http://...',\n    picUrl: 'http://...'\n  }, {\n    title: 'news 2',\n    url: 'http://...',\n    picUrl: 'http://...'\n  }]\n}\n```\n\n## weixin-robot\n\n使用 [wexin-robot](https://github.com/node-webot/weixin-robot) 模块，更傻瓜化地定义自动回复功能。\n\n## 调试\n\n使用 [webot-cli](https://github.com/node-webot/webot-cli) 调试发送测试消息。\n\n\n## License\n\nthe MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-webot%2Fwechat-mp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-webot%2Fwechat-mp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-webot%2Fwechat-mp/lists"}