Preface: It's been a while (2 years) since I was in AP Comp Sci, and I've never worked with CoffeeScript, hubot, HTTP requests, or an API before last week. Pls be gentle.
At my job, I've been making a bot (implementing GitHub's hubot) for our Slack channel that's supposed to tell and interpret for the user some info from a Smartsheet sheet and search Google News for articles about clients. I've started small with this and tried to just get the name of the default document the bot is supposed to search, and I can't even seem to do that. Could y'all help me out here? Code is below. Note: 'jeeves' is the name of the bot.
# Description:
# Tells the user general info about the Smartsheet data that jeeves is
# interacting with.
#
# Dependencies: none.
#
# Configuration:
# HUBOT_SMARTSHEET_API_KEY - Access token from Smartsheet.
# HUBOT_SMARTSHEET_DEFAULT_SHEET_ID - ID number of the default document.
#
# Commands:
# ss-default - Tells the user the current default sheet.
#
# Notes:
# When interacting with Smartsheet, there will be a default sheet that jeeves
# will search if no additional sheet is specified. This default sheet should
# contain all of our client info from OTHER-DATABASE.
module.exports = (robot) ->
robot.hear /ss-default/i, (res) ->
url = "https://api.smartsheet.com/2.0/sheets/#{process.env.HUBOT_SMARTSHEET_DEFAULT_SHEET_ID}"
robot.http(url)
# Smartsheet API requires that the header contain 'Authorization: "Bearer
# <API key>"'. 'Content-Type' is something I saw on StackOverflow and
# the hubot docs as something I should put in there. Not sure if the
# command is '.header' or '.headers'.
.header(Authorization: "Bearer #{process.env.HUBOT_SMARTSHEET_API_KEY}", 'Content-Type': 'application/json')
# The GET request. err = possible error, res = response specified in
# ss-default's constructor, body = the info from Smartsheet in JSON format.
.get(err, res, body) ->
# 'data' contains the info from Smartsheet in JSON format.
data = JSON.parse body
if err
# Tell the user that there was an error and the error code. Listings
# for each error code can be found on the Smartsheet API website.
res.send "Encountered an error: #{err}."
return
else
# Tell the user the name of the current default sheet.
res.send "The current default sheet is #{data.name}."