# ***CoffeeScript (Hubot)*** **`created: 04/01/2019`**
**`updated: 04/01/2019`** [[back to Lab: CoffeeScript (Hubot)](/lab/coffeescript-hubot.html)]
[[back to Lab: root](/lab/root.html)] `@gapbot help` --- # **Functions** ## 1. *not* asyncronously proceeding before a function returns... ``` module.exports = (robot) -> robot.respond /test command$/i, (msg) -> functionone (response) -> myvar = response robot.logger.debug "got #{myvar} back from functionone" functiontwo myvar, (response) -> myothervar = response robot.logger.debug "got #{myothervar} back from functiontwo" ``` the functions are defined at same level as `robot.respond` ``` functionone = (callback) -> something = "lalala" try do_stuff something, (response) -> blah = response return callback(blah) catch err robot.logger.error "there was a problem: #{err}" return callback("uh oh") functiontwo = (something, callback) -> thing = something try do_other_stuff thing, (response) -> tada = response return callback(tada) catch err robot.logger.error "there was a problem: #{err}" return callback("uh oh again") ``` ### robot.http example **`auth test` command** ``` hostname = "https://something.example.com" bot_username = "bot" bot_password = "topsy_kret" module.exports = (robot) -> robot.respond /auth test$/i, (msg) -> login (response) -> sessionid = response robot.logger.debug "got session ID: #{sessionid}" do_something sessionid, (response) -> data = response robot.logger.debug "something replied with: #{data}" msg.send "here is the #{data}" ``` **login function** ``` login = (callback) -> endpoint_login = "api/login" creds = { username: bot_username password: bot_password } credsjson = JSON.stringify(creds) try robot .http("#{hostname}/#{endpoint_login}") .header('Content-Type', 'application/json') .post(credsjson) (err, response, body) -> if response.statusCode isnt 200 robot.logger.error "login failed: #{response.statusCode}" return callback("login failed non-200 response") else robot.logger.debug "login successful" login_response = JSON.parse(body) return callback(login_response.session.value) catch err robot.logger.error "login error: #{err}" return callback("login failed error") ``` **do_something function** ``` do_something = (sessionid, callback) -> endpoint_magic = "api/magic" details = { lalala: "blahblahblah" } details_json = JSON.stringify(details) try robot .http("#{hostname}/#{endpoint_magic}") .header('Content-Type', 'application/json') .header('Cookie', "SESSIONID=#{sessionid}") .post(details_json) (err, response, body) -> if response.statusCode isnt 200 robot.logger.error "perform magic failed: #{response.statusCode}" return callback("no magic") else robot.logger.debug "magic performed successfully: #{body}" return callback(body) catch err robot.logger.error "error performing magic: #{err}" return callback("magic error") ``` ## 2. importing a function from a library asdf ## 3. another thing asdfqwerty Markdeep diagrams: ******************************************* Here's a text to the right of the diagram, * +-----------------+ .-. * ain't that fancy. Pretty fancy indeed, I * |\ | .-+ | * must say! Markdeep diagrams are generally * | \ A-B *---+--> .--+ '--. * enclosed into a rectangle full made of `*` * | \ | | Cloud! | * symbols; and are "drawn" using ASCII-art * +---+-------------+ '-------------' * style, with `- | + / \ * o` etc. ******************************************* Suh-weet! Another random diagram, just because: ******************** * +-+-+-+-*-o * * / / ^ / * * / v / / * * +-+-+-+ * ********************
This is a note by the way!
!!! Note Hey I'm a note. Don't mind me, I'm just sitting here. !!! WARNING I'm a warning, perhaps. *Something might happen!* And other admonitions: !!! Error: Never Pass `nullptr` to a Shader Invoking a shader with a null argument can seg fault. This is a multi-line admonition. Seriously, don't call shaders like that. The table of contents that Markdeep produces is stuffed on the right side, if the browser window is wide enough. Otherwise it is hidden. # **Slack** asdfqwerty ## 1. message formatting asdfqwerty ## 2. reactions asdfqwerty ## 3. uploading images asdfqwerty ## 4. threading messages asdfqwerty ## 5. send message to self What why? *To trigger multiple other commands...* Say you wanted to make a bot populate a thread with the output from multiple commands, *via a single command*, i.e. a `handover` command that makes the bot start a thread and upload several images into it for example. Maybe the images could be graphs, because you have the `graf` Grafana command configured in your bot? Or you could fill the thread with text-based responses from other commands. The `handover` command could "automate the running of *X* `graf` (or other) commands" because Hubot can send messages to itself! Hubot can send any message to itself, so it could be a full `graf` command or even an alias for one (if you are using hubot-alias etc) and it should behave as though a person entered it from Slack. ## 6. Multi-line messages https://stackoverflow.com/questions/24440874/make-hubot-respond-to-a-multiline-message # **Email** `nodemailer` - [link](https://lowpowerlab.com/forum/pi-gateway/email-configuraitons-gmail-aws-ses-aws-smtp/) # **Misc** asdfqwerty ## 1. restricting commands (hubot-auth) # **CONTRIBUTING.md** ## *open a Pull Request on GitHub!* https://github.com/osgav/osgav-blog