Comments (3)
现在就是中间件模式啊。如果你读一下代码,会发现 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.
受你这个规则引擎、wait功能的启发,我用自己相对熟悉的python写了一个engine,实现类似的规则配置、wait功能,用在了我的几个基于django框架的服务号项目里面。
但惭愧,node-webot整个框架的底层流转逻辑,我看得还不是很明白。
大侠能否写几个片段,示意一下如何装配parser中间件,以及中间件的接口框架。接口内部解析封装的粗活我倒是能写写。
from weixin-robot.
你对 express 的中间件熟悉吗?上面链接里的源码应该已经很清楚了。
简而言之,Webot 回复规则的接口实际是 webot.reply(info, ...) ,接收的参数是一个 info 对象。为了应用到实际中,处理 http 请求的流程为:
- 验证请求有效性(verify)
- 解析请求为 info (parse)
- webot.reply(info, ...) 获得回复内容
- 输出回复(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)
- noReply = true无效 HOT 3
- Doc里面似乎有个小错误 HOT 2
- handler里使用异步事件 HOT 1
- 请问新出的语音辨识接口加上了吗? HOT 5
- 语音识别接口还是有bug HOT 5
- 链接菜单没有跳转 HOT 4
- 经常会出现204请求 HOT 1
- 微信多次发送消息 HOT 4
- 能否拿到发信的人的ip地址? HOT 3
- 机器人自动回复时, 回复内容可以在别的回调函数里面吗(wechat单独使用,指定res.reply是可以的) HOT 4
- 在handler中return图文信息的时候url无法带入多个参数 HOT 2
- 该公众号暂时无法提供服务,请稍后再试 HOT 4
- 请教下 如何把yaml修改为json么? HOT 2
- 有计划支持安全模式么? HOT 4
- 代码全是express3.x的,有没有express4.x以上版本? HOT 1
- 多客服系统没有接入么? HOT 5
- 配置正常,但是没有自动回复 HOT 1
- 为什么老会显示don't konw what you are saying HOT 2
- 规则库
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from weixin-robot.