I am trying to upload an image to Slack using their API.
I have successfully uploaded some text message using the following function :
function send_message(msg::T, channel_id::T, token::T) where T<:AbstractString
msg = Dict("channel"=>channel_id, "text"=>"$(msg)")
msg_payload = JSON.json(msg)
token = "Bearer " * token
params = ["Content-Type" => "application/json; charset=utf-8", "Authorization" => token]
url = "https://slack.com/api/chat.postMessage"
r = HTTP.request("POST", url, params, msg_payload)
return r
end
But I am failing to do the same for files, I have to do something with HTTP.form
or HTTP.multipart
but I could not find those documentation.
Here is the code snippet I am trying to upload a file :
channel_id = "C01HJGL1MTK"
auth_key = "xoxb-1590094798692-1596646938673-3PC9eIaEa8ojPiEuzL6nJBM6"
key = "Bearer "*auth_key
url = "https://slack.com/api/files.upload"
file = open("tmp/plot2.jpg", "r")
part_file = HTTP.Form(Dict("file"=>file))
params = ["Content-Type" => "multipart/form-data", "Authorization" => key]
payload = HTTP.Form(Dict("channel"=>channel_id, "file"=>file))
# Error in the above line, but I am getting a workaround
HTTP.request("POST", url, params, payload)
Provided the channel key and bearer token if anyone wanted to reproduce.