0

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}."
jwilso48
  • 1
  • 2
  • Jay, please provide more info on the specific issue you are experiencing. Is it Smartsheet specific? Is there an error code + message? – avioing Jul 15 '15 at 19:04
  • I don't think it's Smartsheet specific because it works fine when I test it on POSTMAN (Screenshot: http://i.imgur.com/l0hhqAF.png). Could it be that 'Authorization' needs to be in quotes as well? – jwilso48 Jul 15 '15 at 19:17
  • Although -- and I'm not sure if this is related -- POSTMAN goes completely unresponsive after I make that GET request. – jwilso48 Jul 15 '15 at 19:34
  • A few suggestions: 1) when building the header -- put 'Authorization' in single quotes AND try temporarily hardcoding its value ('Bearer: ACCESS_TOKEN'); 2) use Fiddler (or similar tool) to inspect the actual/full request (including headers) that are being sent over the wire; 3) get the request working in POSTMAN and compare that (working) request with the request that your script is generating. If you're not able to resolve the issue with these suggestions, then please post more specifics here regarding exactly what error message you're receiving when your program executes. – Kim Brandl Jul 15 '15 at 22:30
  • Kim, I used Fiddler and had it target only my Slack window, and it appears that _nothing_ is going through. Here is the [link](http://bit.ly/jay-code) to my current code. I've compared it to the 'Advanced HTTP' code from [this](http://bit.ly/github-article) article by a GitHub employee and it matches up perfectly as far as processes go, but it still gets hung up every time. A note for my code: the line `res.send "Well, I got this far. What do you want from me, eh?"`, was designed as a debug line to see if my code even cleared the `data = JSON.parse(body)` line. It did not. Thoughts? – jwilso48 Jul 16 '15 at 20:40

0 Answers0