[英]Post image to slack using httr package in R
Slack 提供了一種通過其 api 上傳文件的方法。 文檔可以在這里找到:
在此頁面上,它給出了如何發布文件的示例:
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
我試圖找到更好的例子來使用,但到目前為止沒有運氣。 任何建議表示贊賞!
在 slackr 自己的 gg_slackr 方法中有一個例子,它創建了一個 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="/")
並查看是否修復了您的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.