用Perl一次性读入文件全部内容
326 2023-04-03 02:54:55
首先,在决定开发自定义脚本时,可以使用npm search命令看看是不是有类似的脚本可以使用。
$ npm search hubot-scripts githubNAME DESCRIPTIONhubot-deployer Giving Hubot the ability to deploy GitHub repos to PaaS providers hubot hubot-scripts hubot-githhubot-gh-release-pr A hubot script to create GitHub's PR for release hubot-github Giving Hubot the ability to be a vital member of your github organization …
应用NPM 包:
- npm install –save
- 将package-name添加到external-scripts.json
myhubot 项目结构
package.json 项目全局配置信息
我们编写的自定义脚本要放在scripts中,可以是.coffee或.js文件,你可以用CoffeeScript或纯JS编写他们。
module.exports = (robot) -> # your code here
首先我们在scripts中创建第一个脚本,hello.coffee
module.exports = (robot) -> robot.respond /greet/i, (res) -> res.send 'hello world.'
myhubot> myhubot greetmyhubot> hello world.
hearing可以监听房间或群组中任何消息。
respond只监听直接发送给机器人的消息,也就是需要指定机器人名称或别名,加入机器人名称是rob,别名是/,则如下格式会触发脚本:
- rob open the pod bay doors - ROB: open the pod bay doors - @ROB open the pod bay doors - /open the pod bay doors
res参数是Response实例,如果要从机器人返回消息,可以使用send或reply。
send会将消息发送到整个聊天房间或群组。
reply会将消息回复给具体的人。
更多信息请参考官网 https://hubot.github.com/docs/scripting/
Hubot是基于node.js开发的,所以它可以做到node.js所能做的,如:
scripts/calculate.coffee
child_process = require('child_process')module.exports = (robot) -> robot.respond /(cal|日历)( me)?/i, (res) -> child_process.exec 'cal', (err, stdout, stderr) -> res.send(stdout)
重启myhubot,输入myhubot cal,就可以看到控制台返回服务端日历信息
Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31
为了使机器人响应更加友好,对于未能识别的信息,可以设置一个默认返回信息。
scripts/catchAll.coffee
module.exports = (robot) -> robot.catchAll (res) -> res.send "主人,无法识别您的指令:#{res.message.text}"
hubot默认提供两种adapter,shell和campfile。
shell提供命令行方式,用于开发调试或者运维还可以,用于聊天太逊了。
campfile聊天工具在中国都没几个人听过,不用也罢。
依赖于hubot的灵活扩展机制,社区提供了多种聊天工具的集成adapter。可以在官网上找到。
不过在中国,最流行的聊天软件当然是微信,将自己的聊天机器人绑定到微信账户上,没事调戏下,是不是很有趣。
好在,已经有人做出了微信adapter,地址:https://github.com/KasperDeng/Hubot-WeChat
接下来,我们集成Hub-WeChat到我们的工程中来即可。
npm install hubot-weixin --save
运行myhubot,设置名称为bot,别名/,这样只需要输入/cal就可以激活机器人了。
binhubot.cmd -n bot -l / -a weixin
这样运行命令当然会出错,hubot-weixin工作的主要机制是hack网页版微信协议,先用手机登录微信帐号,然后模拟网页版微信登录,这样就可以接受微信消息了。
可以按照如下步骤设置:
2.在手机端登录微信;
3.在chrome浏览器打开网页微信:web.weixin.qq.com,F12打开控制台监控请求信息;
4.用手机扫描登录;
5.控制台应该会监控到很多请求信息;
6.从这些请求信息中找出如下信息,并填写到./node_modules/hubot-weixin/config.yaml:
cookie:Uin:Sid:Skey: DeviceID:
这下,重新运行命令就可以了,如果有错误,就按照刚才的步骤检查这些参数设置是否正确,也可以用postman等工具调试看看。
binhubot.cmd -n bot -l / -a weixin
正确启动后,就可以通过微信发消息给机器人了。
接下来,可以考虑做些更好玩的事情。