# ***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 / / *
* +-+-+-+ *
********************