Skip to content

Commit 35a28b9

Browse files
committed
add api route to send slack unfurl request
1 parent 760fe2f commit 35a28b9

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

lib/api.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ end
1010

1111
module type Slack = sig
1212
val send_notification : ctx:Context.t -> msg:post_message_req -> (unit, string) Result.t Lwt.t
13+
14+
val send_chat_unfurl : ctx:Context.t -> chat_unfurl_req -> (unit, string) Result.t Lwt.t
1315
end

lib/api_local.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ module Github : Api.Github = struct
2020
| Ok file -> Lwt.return @@ Ok (Github_j.api_commit_of_string file)
2121
end
2222

23+
module Slack_base : Api.Slack = struct
24+
let send_notification ~ctx:_ ~msg:_ = Lwt.return @@ Error "undefined for local setup"
25+
26+
let send_chat_unfurl ~ctx:_ _ = Lwt.return @@ Error "undefined for local setup"
27+
end
28+
2329
module Slack : Api.Slack = struct
30+
include Slack_base
31+
2432
let send_notification ~ctx:_ ~msg =
2533
let json = msg |> Slack_j.string_of_post_message_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
2634
Stdio.printf "will notify #%s\n" msg.channel;
@@ -29,6 +37,8 @@ module Slack : Api.Slack = struct
2937
end
3038

3139
module Slack_simple : Api.Slack = struct
40+
include Slack_base
41+
3242
let log = Log.from "slack"
3343

3444
let send_notification ~ctx:_ ~(msg : Slack_t.post_message_req) =
@@ -41,6 +51,8 @@ module Slack_simple : Api.Slack = struct
4151
end
4252

4353
module Slack_json : Api.Slack = struct
54+
include Slack_base
55+
4456
let log = Log.from "slack"
4557

4658
let send_notification ~ctx:_ ~(msg : Slack_t.post_message_req) =

lib/api_remote.ml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,26 @@ module Slack : Api.Slack = struct
8181
)
8282
| Error e -> Lwt.return @@ build_query_error url e
8383
)
84+
85+
let send_chat_unfurl ~(ctx : Context.t) req =
86+
log#info "unfurling Slack links";
87+
let secrets = Context.get_secrets_exn ctx in
88+
match secrets.slack_access_token with
89+
| None -> Lwt.return @@ fmt_error "failed to retrieve Slack access token"
90+
| Some access_token ->
91+
let data = Slack_j.string_of_chat_unfurl_req req in
92+
log#info "%s" data;
93+
let url = "https://slack.com/api/chat.unfurl" in
94+
let headers = [ bearer_token_header access_token ] in
95+
let body = `Raw ("application/json", data) in
96+
( match%lwt http_request ~body ~headers `POST url with
97+
| Ok s ->
98+
let res = Slack_j.chat_unfurl_res_of_string s in
99+
if res.ok then Lwt.return @@ Ok ()
100+
else (
101+
let msg = Option.value ~default:"an unknown error occurred" res.error in
102+
Lwt.return @@ fmt_error "%s\nfailed to unfurl Slack links" msg
103+
)
104+
| Error e -> Lwt.return @@ fmt_error "error while querying %s: %s\nfailed to unfurl Slack links" url e
105+
)
84106
end

0 commit comments

Comments
 (0)