使用R中的httr包将图像发布到松弛状态

时间:2018-09-06 18:35:35

标签: r slack httr

Slack提供了一种通过api上传文件的方法。该文档位于:

Slack files.upload method

在此页面上,提供了有关如何发布文件的示例:

curl -F file=@dramacat.gif -F "initial_comment=Shakes the cat" -F channels=C024BE91L,D032AC32T -H "Authorization: Bearer xoxa-xxxxxxxxx-xxxx" https://slack.com/api/files.upload

我正在尝试翻译如何使用R中的httr包以及R工作目录中的文件来执行此行代码。我在翻译命令的不同部分时遇到了麻烦。这是我到目前为止所拥有的。

api_token='******'
f_path='c:/mark/consulting/dreamcloud' #this is also my working directory
f_name='alert_picture.png'

res<-httr::POST(url='https://slack.com/api/files.upload', httr::add_headers(`Content-Type` = "multipart/form-data"), 
      body = list(token=api_token, channels='CCJL7TMC7', title='test', file = httr::upload_file(f_path), filename=f_name))

运行此命令时,出现以下错误:

Error in curl::curl_fetch_memory(url, handle = handle) : 
  read function returned funny value

我试图找到更好的例子来使用,但到目前为止还没有运气。任何建议表示赞赏!

1 个答案:

答案 0 :(得分:1)

There's an example in slackr's own gg_slackr method,它创建GGPlot的图像并将其上传到Slack:

  res <- POST(url="https://slack.com/api/files.upload",
              add_headers(`Content-Type`="multipart/form-data"),
              body=list(file=upload_file(ftmp),
                        token=api_token, channels=modchan))

您的代码似乎将路径传递到目录而不是文件作为file参数-请考虑将该参数更改为file=upload_file(paste(f_path, f_name, sep="/"),看看是否可以解决您的错误。

相关问题