@@ -78,20 +78,6 @@ let update_message ~(ctx : Context.t) ~(msg : Slack_t.update_message_req) =
7878 ~name: (sprintf " chat.update (%s,%s)" msg.ts msg.channel)
7979 ~ctx ~body `POST " chat.update" Slack_j. read_update_message_res
8080
81- let www_form_of_files_upload_req (file : Slack_t.files_upload_req ) =
82- let fields =
83- [
84- string_field_val file.channels " channels" ;
85- string_field_val file.content " content" ;
86- string_field_val file.filename " filename" ;
87- string_field_val file.filetype " filetype" ;
88- string_field_val file.initial_comment " initial_comment" ;
89- string_field_val file.thread_ts " thread_ts" ;
90- string_field_val file.title " title" ;
91- ]
92- in
93- list_filter_opt fields
94-
9581let get_permalink ~(ctx : Context.t ) ~(req : Slack_t.get_permalink_req ) =
9682 log#info " getting permalink for channel %s, message_ts %s" req.channel req.message_ts;
9783 let args = Web. make_url_args [ " channel" , req.channel; " message_ts" , req.message_ts ] in
@@ -100,26 +86,22 @@ let get_permalink ~(ctx : Context.t) ~(req : Slack_t.get_permalink_req) =
10086 ~name: (sprintf " chat.getPermalink (%s, %s)" req.channel req.message_ts)
10187 ~ctx `GET api_path Slack_j. read_get_permalink_res
10288
103- let www_form_of_get_upload_url_ext (req : Slack_t.get_upload_url_ext_req ) =
104- let fields =
89+ let get_upload_url_external ~(ctx : Context.t ) ~(req : Slack_t.get_upload_url_ext_req ) =
90+ log#info " getting upload url for %s" req.filename;
91+ let args =
10592 [
10693 Some (" filename" , req.filename);
10794 Some (" length" , Int. to_string req.length);
10895 string_field_val req.alt_txt " alt_txt" ;
10996 string_field_val req.snippet_type " snippet_type" ;
11097 ]
98+ |> list_filter_opt
11199 in
112- list_filter_opt fields
113-
114- let get_upload_url_external ~(ctx : Context.t ) ~(req : Slack_t.get_upload_url_ext_req ) =
115- log#info " getting upload url for %s" req.filename;
116- let args = www_form_of_get_upload_url_ext req in
117- let data = Web. make_url_args args in
118- let body = `Raw (" application/x-www-form-urlencoded" , data) in
119- log#info " data to upload req: %s" data;
100+ let args = Web. make_url_args args in
101+ let api_path = sprintf " files.getUploadURLExternal?%s" args in
120102 request_token_auth ~ctx
121103 ~name: (sprintf " files.getUploadURLExternal (%s)" req.filename)
122- ~body `POST " files.getUploadURLExternal " Slack_j. read_get_upload_url_ext_res
104+ `POST api_path Slack_j. read_get_upload_url_ext_res
123105
124106let complete_upload_external ~(ctx : Context.t ) ~(req : Slack_t.complete_upload_ext_req ) =
125107 log#info " completing upload url for %s" @@ Slack_j. string_of_files_v2 req.files;
@@ -130,27 +112,33 @@ let complete_upload_external ~(ctx : Context.t) ~(req : Slack_t.complete_upload_
130112 ~name: (sprintf " files.completeUploadExternal (%s)" @@ Slack_j. string_of_files_v2 req.files)
131113 ~body `POST " files.completeUploadExternal" Slack_j. read_complete_upload_ext_res
132114
133- (* * [upload_file ctx file] upload [file] to channels noted in [file.channels]
134- with content [file.content]; Not supporting file upload through form using
135- `file` currently
136- uses web API with access token *)
137- let upload_file ~(ctx : Context.t ) ~(file : Slack_t.files_upload_req ) =
138- match file.filename, file.content with
139- | None , _ | _ , None -> Exn. fail " need to supply both filename and content"
140- | Some filename , Some content ->
141- let length = String. length content in
142- let req = Slack_j. make_get_upload_url_ext_req ~filename ~length () in
143- ( match % lwt get_upload_url_external ~ctx ~req with
115+ (* * [upload_file ctx file] upload [file] to channels noted in
116+ [file.channels] with content [file.content]; Not supporting file
117+ upload through form using `file` currently uses web API with
118+ access token. Snippet can also be sent instead by filling the
119+ content value *)
120+ let upload_file ~(ctx : Context.t ) ~(req : Slack_t.files_upload_req ) =
121+ match req.filename with
122+ | None -> Exn. fail " need to supply filename"
123+ | Some filename ->
124+ let contents, length =
125+ match req.content with
126+ | Some v -> v, String. length v
127+ | None ->
128+ let raw_file_contents = In_channel. with_open_bin filename (fun ic -> input_all ic) in
129+ raw_file_contents, String. length raw_file_contents
130+ in
131+ let req' = Slack_j. make_get_upload_url_ext_req ~filename ~length () in
132+ ( match % lwt get_upload_url_external ~ctx ~req: req' with
144133 | Error e -> Lwt. return_error e
145134 | Ok { upload_url; file_id; _ } ->
146- let raw_file_contents = In_channel. with_open_bin filename (fun ic -> input_all ic) in
147- let body = `Raw (" " , raw_file_contents) in
135+ let body = `Raw (" text/plain" , contents) in
148136 ( match % lwt http_request ~ua: ctx.ua ~body `POST upload_url with
149137 | Error e -> slack_lib_fail " upload file failed with: %s" e
150138 | Ok _ ->
151- let files : Slack_t.files_v2 = [ { id = file_id; title = file .title } ] in
152- let req = Slack_j. make_complete_upload_ext_req ~files ?channels:file .channels ?thread_ts:file .thread_ts () in
153- ( match % lwt complete_upload_external ~ctx ~req with
139+ let files : Slack_t.files_v2 = [ { id = file_id; title = req .title } ] in
140+ let req' = Slack_j. make_complete_upload_ext_req ~files ?channels:req .channels ?thread_ts:req .thread_ts () in
141+ ( match % lwt complete_upload_external ~ctx ~req: req' with
154142 | Error e -> Lwt. return_error e
155143 | Ok { files; _ } ->
156144 let f =
0 commit comments