背景
前端开发中经常需要使用一些工具来帮助我们提高开发效率或实现一些自动化操作。其中ChatOps是一种在团队内部推广交流的方式,而 Hubot 即是 ChatOps 中的一种聊天机器人。
hubot-headlines 是一款 Hubot 插件,它能够通过自定义 RSS 通知来提供实时新闻。
安装
首先,在安装之前,确保先安装好 Node.js 和 npm。
当 Node.js 和 npm 安装好后,可以通过以下命令安装 hubot-headlines:
npm install --save hubot-headlines
安装完成后,打开 Hubot 的 Robot 文件,controller 文件或 coffee-script 文件,然后添加以下代码:
headlines = require 'hubot-headlines' module.exports = (robot) -> headlines robot
然后启动 Hubot,插件就会自动运行。
使用
当插件成功安装后,可以在 ChatOps 中直接向 Hubot 请求新闻。
首先,需要先指定 RSS 订阅地址。Hubot 提供了 add headlines feed
命令用于添加或更新 RSS 订阅地址。
例如,添加国内新闻 RSS 地址:
hubot add headlines feed http://www.xinhuanet.com/rss.htm
Hubot会响应添加成功的提示,同时会输出获取到的文章名称:
xinhua-rss - National news - No.1 xinhua-rss - The world today - No.1 xinhua-rss - Gavalan earthquake - No.1 ...
然后,就可以通过命令获取新闻了。以下命令可以获取最近发布的 10 条新闻:
hubot headlines
获取到的结果通常会包含文章的名称、链接和时间:
1. Four killed in Sudan during anti-Romeo protests - http://www.xinhuanet.com/english/2018-06/22/c_137267788.htm (2018-06-22T12:02:18+00:00) 2. US military to leave hundreds of troops in Syria - http://www.xinhuanet.com/english/2019-02/22/c_137844998.htm (2019-02-22T22:43:40+00:00) 3. China calls for global efforts to safeguard cultural heritage - http://www.xinhuanet.com/english/2019-02/23/c_137847477.htm (2019-02-23T10:23:38+00:00) ...
指导意义
在开发 ChatOps 时,常常需要将一些通知和提醒机械化,让机器人来主动推送这些信息,从而减少人与人之间的沟通成本,提高开发效率。
hubot-headlines 插件就是一款为 ChatOps 提供新闻订阅和推送服务的插件,它可以自动获取新闻,并通过 ChatOps 渠道向团队中的所有成员推送这些新闻。
除了新闻订阅,还可以使用同样的方式实现其他类似的订阅服务,从而为团队内部提供更加高效的知识共享和信息交流渠道。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/60057b0e81e8991b448eb781