Giter Site home page Giter Site logo

wecom-api's Introduction

wecom-api

version license downloads fork

根据个人需求不定时不定量更新。欢迎提 Issues。

目前 License 为 GPL,个人用途可闭源,若有需求或者商用请联系我

参考官方 API 文档

实现例子:

wework.message

举例应用:

<> 括起来是用户自己补充的值,只需修改这三个参数(和 http 传值)即可使用。

weToken:在 API 接收消息页面填写的 Token

weEncodingAESKey:在 API 接收消息页面填写的 EncodingAESKey

weReceiveId:在“我的企业”页面最下面的“企业 ID”

  const { WXBizMsgCrypt } = require('wecom-api')

  verifyURL (ctx) {
    const { msg_signature, timestamp, nonce, echostr } = ctx.request.query
    try {
      const wXBizMsgCrypt = new WXBizMsgCrypt(<weToken>, <weEncodingAESKey>, <weReceiveId>)
      ctx.body = wXBizMsgCrypt.verifyURL(msg_signature, timestamp, nonce, echostr).message
    } catch (e) {
      ctx.body = e
    }
  }
  
  async receiveMsg (ctx) {
    const { msg_signature, timestamp, nonce } = ctx.request.query
    const postData = ctx.request.body // 注意程序可以接收 xml 格式数据

    try {
      const wXBizMsgCrypt = new WXBizMsgCrypt(<weToken>, <weEncodingAESKey>, <weReceiveId>)
      const receivedmsg = wXBizMsgCrypt.decryptMsg(msg_signature, timestamp, nonce, postData)
      const { ToUserName, FromUserName, CreateTime, MsgType, Content, MsgId, AgentID } = receivedmsg
      // 示例回复,之后补充 message 格式引用
      const xmlText = `<xml><ToUserName><![CDATA[${FromUserName}]]></ToUserName><FromUserName><![CDATA[${ToUserName}]]></FromUserName><CreateTime>${CreateTime}</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[${new Date(Date.now())} ${Date.now().toString()}: ${Content}]]></Content></xml>`
      const msg = wXBizMsgCrypt.encryptMsg(xmlText)
      ctx.body = msg
    } catch (e) {
      ctx.body = e
    }
  }
  • 添加测试

wecom-api's People

Contributors

evgo2017 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.