Executing a shell script using hubot.
If haven’t read already, here’s an article on how to setup hubot on slack: Your new Ops assistant: Hubot!. Well, you can call it whatever you want… | by Chandamala vijay | Feb, 2023 | Medium
Hubot out of the box doesn’t do too much but it is an extensible, scriptable robot friend. There are hundreds of scripts written and maintained by the community and it’s easy to write your own. You can create a custom script in hubot’s scripts
directory or create a script package for sharing with the community!
You can refer Scripting | HUBOT (github.com) for writing hubot scripts in coffee script / js.
But we’re here for the shell scripting, yes, I’m getting to that.
We won’t be replacing the coffee scripts but will be using them to call a shell script instead.
Here’s how :
Add the following scripts to scripts directory within your hubot directory
scripts/add.sh
#!/bin/bash
echo "$1 plus $2 equals $(($1+$2))"
scripts/adder.coffee
module.exports = (robot) ->
robot.respond /add (.*) to (.*)/i, (res) ->
a = res.match[1]
b = res.match[2]
{ spawn } = require('node:child_process');
fs = require("fs");
{ Console } = require("console");
build = spawn "/home/ubuntu/hubot/scripts/add.sh", ["#{a}","#{b}"]
build.stdout.on 'data', ( data ) -> res.reply "#{data}"
build.stderr.on 'data', ( data ) -> res.reply "#{data}"
Restart hubot and test.
Ofcourse you’d probably use something more sophisticated than a dumb addition script 😅
Have a great one 🫡