@@ -91,6 +91,23 @@ module Slack : Api.Slack = struct
91
91
92
92
let bearer_token_header access_token = sprintf " Authorization: Bearer %s" (Uri. pct_encode access_token)
93
93
94
+ let request_token_auth ~name ?headers ?body ~ctx meth path read =
95
+ log#info " %s: starting request" name;
96
+ let secrets = Context. get_secrets_exn ctx in
97
+ match secrets.slack_access_token with
98
+ | None -> Lwt. return @@ fmt_error " %s: failed to retrieve Slack access token" name
99
+ | Some access_token ->
100
+ let headers = bearer_token_header access_token :: Option. value ~default: [] headers in
101
+ let url = sprintf " https://slack.com/api/%s" path in
102
+ ( match % lwt slack_api_request ?body ~headers meth url read with
103
+ | Ok res -> Lwt. return @@ Ok res
104
+ | Error e -> Lwt. return @@ fmt_error " %s: failure : %s" name e
105
+ )
106
+
107
+ let read_unit s l =
108
+ (* must read whole response to update lexer state *)
109
+ ignore (Slack_j. read_ok_res s l)
110
+
94
111
(* * [send_notification ctx msg] notifies [msg.channel] with the payload [msg];
95
112
uses web API with access token if available, or with webhook otherwise *)
96
113
let send_notification ~(ctx : Context.t ) ~(msg : Slack_t.post_message_req ) =
@@ -123,31 +140,11 @@ module Slack : Api.Slack = struct
123
140
end
124
141
125
142
let send_chat_unfurl ~(ctx : Context.t ) req =
126
- log#info " unfurling Slack links" ;
127
- let secrets = Context. get_secrets_exn ctx in
128
- match secrets.slack_access_token with
129
- | None -> Lwt. return @@ fmt_error " failed to retrieve Slack access token"
130
- | Some access_token ->
131
- let data = Slack_j. string_of_chat_unfurl_req req in
132
- log#info " %s" data;
133
- let url = " https://slack.com/api/chat.unfurl" in
134
- let headers = [ bearer_token_header access_token ] in
135
- let body = `Raw (" application/json" , data) in
136
- ( match % lwt slack_api_request ~body ~headers `POST url Slack_j. read_chat_unfurl_res with
137
- | Ok _res -> Lwt. return @@ Ok ()
138
- | Error e -> Lwt. return @@ fmt_error " %s\n failed to unfurl Slack links" e
139
- )
143
+ let data = Slack_j. string_of_chat_unfurl_req req in
144
+ request_token_auth ~name: " unfurl slack links"
145
+ ~body: (`Raw (" application/json" , data))
146
+ ~ctx `POST " chat.unfurl" read_unit
140
147
141
148
let send_auth_test ~(ctx : Context.t ) () =
142
- log#info " retrieving bot information" ;
143
- let secrets = Context. get_secrets_exn ctx in
144
- match secrets.slack_access_token with
145
- | None -> Lwt. return @@ Error " failed to retrieve Slack access token"
146
- | Some access_token ->
147
- let url = " https://slack.com/api/auth.test" in
148
- let headers = [ bearer_token_header access_token ] in
149
- ( match % lwt slack_api_request ~headers `GET url Slack_j. read_auth_test_res with
150
- | Ok res -> Lwt. return @@ Ok res
151
- | Error e -> Lwt. return @@ fmt_error " %s\n failed to retrieve Slack auth info" e
152
- )
149
+ request_token_auth ~name: " retrieve bot information" ~ctx `POST " auth.test" Slack_j. read_auth_test_res
153
150
end
0 commit comments