简介
node-red-contrib-slackbot-tjun 是一个基于 Node-RED 平台开发的 npm 包,它提供了一个 Slack Bot 节点,可以将 Slack 的消息转发到 Node-RED 中,从而实现 Slack 和 Node-RED 之间的无缝连接。该 npm 包主要由中国 TOP 十大物联网企业之一 T-Jun 贡献,目前已经在 GitHub 上开源,获取了广泛关注。
安装
使用 npm 命令安装 node-red-contrib-slackbot-tjun,代码如下:
npm install node-red-contrib-slackbot-tjun
使用
1. 导入 Slack Bot 节点
将 Slack Bot 节点导入到 Node-RED 平台中,选择节点,然后拖到画布上,代码如下:
[{"id":"53e61c6e.1a220c","type":"slackbot-tjun","z":"d8097d4.a4bcbf","botname":"node-red","channelname":"#general","x":350,"y":60,"wires":[]}]
2. 配置 Slack Bot 节点
在 Slack Bot 节点的设置中,需要配置 token(Slack 账号的 API Token)、botname(机器人在 Slack 上显示的名字)、channelname(机器人发布消息的目标频道),代码如下:
[{"id":"53e61c6e.1a220c","type":"slackbot-tjun","z":"d8097d4.a4bcbf","botname":"node-red","channelname":"#general","token":"xxxxxxxxxxxxxxx","x":350,"y":60,"wires":[]}]
3. 接收 Slack 消息
在接收 Slack 消息的节点中,需要选择 Slack Bot 节点,代码如下:
[{"id":"12345678.123456","type":"slackbot-tjun in","z":"d8097d4.a4bcbf","name":"receive slack message","botname":"node-red","channelname":"#general","x":150,"y":360,"wires":[["4567890a.098765"]]}]
4. 处理 Slack 消息
在处理 Slack 消息的节点中,可以使用 msg.payload 获取到 Slack 发来的消息内容,代码如下:
[{"id":"4567890a.098765","type":"function","z":"d8097d4.a4bcbf","name":"process message","func":"msg.payload = \"Hello World!\";\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":360,"wires":[["098765b.6789ab"]]},{"id":"098765b.6789ab","type":"slackbot-tjun out","z":"d8097d4.a4bcbf","name":"reply slack message","botname":"node-red","channelname":"#general","x":550,"y":360,"wires":[]}]
5. 发送 Slack 消息
在发送 Slack 消息的节点中,需要选择 Slack Bot 节点,然后通过 msg.payload 输入消息内容,代码如下:
[{"id":"23456789.234567","type":"function","z":"d8097d4.a4bcbf","name":"prepare message","func":"msg.payload = \"Hello World!\";\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":660,"wires":[["345678c.345678"]]},{"id":"345678c.345678","type":"slackbot-tjun out","z":"d8097d4.a4bcbf","name":"send slack message","botname":"node-red","channelname":"#general","x":550,"y":660,"wires":[]}]
示例代码
完整的节点配置示例代码如下:
[{"id":"53e61c6e.1a220c","type":"slackbot-tjun","z":"d8097d4.a4bcbf","botname":"node-red","channelname":"#general","token":"xxxxxxxxxxxxxxx","x":350,"y":60,"wires":[]},{"id":"12345678.123456","type":"slackbot-tjun in","z":"d8097d4.a4bcbf","name":"receive slack message","botname":"node-red","channelname":"#general","x":150,"y":360,"wires":[["4567890a.098765"]]},{"id":"4567890a.098765","type":"function","z":"d8097d4.a4bcbf","name":"process message","func":"msg.payload = \"Hello World!\";\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":360,"wires":[["098765b.6789ab"]]},{"id":"098765b.6789ab","type":"slackbot-tjun out","z":"d8097d4.a4bcbf","name":"reply slack message","botname":"node-red","channelname":"#general","x":550,"y":360,"wires":[]},{"id":"23456789.234567","type":"function","z":"d8097d4.a4bcbf","name":"prepare message","func":"msg.payload = \"Hello World!\";\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":660,"wires":[["345678c.345678"]]},{"id":"345678c.345678","type":"slackbot-tjun out","z":"d8097d4.a4bcbf","name":"send slack message","botname":"node-red","channelname":"#general","x":550,"y":660,"wires":[]}]
结尾
以上是 node-red-contrib-slackbot-tjun 的使用教程,它让 Node-RED 和 Slack 之间的通信变得更加简单。这个 npm 包是 T-Jun 团队在打造智能家居平台时积累的经验,具有很强的指导意义。特别是在物联网领域,与用户交互的平台已经从 App 转向了对话机器人,所以能够使用这样的 npm 包,将大大提高智能家居平台的用户体验。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/600572c481e8991b448e8de1