0

前言:我在 AP Comp Sci 已经有一段时间(2 年)了,在上周之前我从未使用过 CoffeeScript、hubot、HTTP 请求或 API。请温柔一点。

在我的工作中,我一直在为我们的 Slack 频道制作一个机器人(实现 GitHub 的 hubot),它应该为用户讲述和解释 Smartsheet 工作表中的一些信息,并在 Google 新闻中搜索有关客户的文章。我从小处着手,试图获取机器人应该搜索的默认文档的名称,但我什至似乎都做不到。你们能帮我吗?代码如下。注意:“jeeves”是机器人的名称。

# 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}."
4

0 回答 0