Giter Club home page Giter Club logo

Comments (3)

ktmud avatar ktmud commented on July 1, 2024

现在就是中间件模式啊。如果你读一下代码,会发现 weixin-robot 是由 webot + wechat-mp 两个模块组合而成。wechat-mp 就是你说的解析 xml 的中间件。要兼容微博,你可以参考 weixin-robot 的 watch 方法 :
https://github.com/node-webot/weixin-robot/blob/master/lib/weixin.js#L13

不一定需要在新建 robot 实例的时候指定解析器,watch 的时候通过选项参数配置中间件就可以了: https://github.com/node-webot/webot#webotwatchapp-options

weixin-robot 的 watch 相当于把微信接口所需要的参数封装到了一个方法里。针对微博当然也可以这么干。

欢迎提PR。

from weixin-robot.

yeruizhi avatar yeruizhi commented on July 1, 2024

受你这个规则引擎、wait功能的启发,我用自己相对熟悉的python写了一个engine,实现类似的规则配置、wait功能,用在了我的几个基于django框架的服务号项目里面。

但惭愧,node-webot整个框架的底层流转逻辑,我看得还不是很明白。

大侠能否写几个片段,示意一下如何装配parser中间件,以及中间件的接口框架。接口内部解析封装的粗活我倒是能写写。

from weixin-robot.

ktmud avatar ktmud commented on July 1, 2024

你对 express 的中间件熟悉吗?上面链接里的源码应该已经很清楚了。

简而言之,Webot 回复规则的接口实际是 webot.reply(info, ...) ,接收的参数是一个 info 对象。为了应用到实际中,处理 http 请求的流程为:

  1. 验证请求有效性(verify)
  2. 解析请求为 info (parse)
  3. webot.reply(info, ...) 获得回复内容
  4. 输出回复(send)

其中 1 和 2 可以合并。因为 3 本身是异步的,所以 parser 和 dumper 最好做成两个独立的中间件,以避免 callback 嵌套。 weixin-robot 这个封装,其实就是把添加这些中间件的步骤合并为 webot.watch(app, ...) 一个接口暴露给用户。

在 koa-js 里,因为可以 yield 异步操作,使得把所有流程封装到一个中间件成为可能:
https://github.com/ktmud/koa-wechat/blob/master/index.js

from weixin-robot.

Related Issues (20)

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.