前言:自从我进入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}."