File tree Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Expand file tree Collapse file tree 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 10
10
11
11
module type Slack = sig
12
12
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
13
15
end
Original file line number Diff line number Diff line change @@ -20,7 +20,15 @@ module Github : Api.Github = struct
20
20
| Ok file -> Lwt. return @@ Ok (Github_j. api_commit_of_string file)
21
21
end
22
22
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
+
23
29
module Slack : Api .Slack = struct
30
+ include Slack_base
31
+
24
32
let send_notification ~ctx :_ ~msg =
25
33
let json = msg |> Slack_j. string_of_post_message_req |> Yojson.Basic. from_string |> Yojson.Basic. pretty_to_string in
26
34
Stdio. printf " will notify #%s\n " msg.channel;
@@ -29,6 +37,8 @@ module Slack : Api.Slack = struct
29
37
end
30
38
31
39
module Slack_simple : Api .Slack = struct
40
+ include Slack_base
41
+
32
42
let log = Log. from " slack"
33
43
34
44
let send_notification ~ctx :_ ~(msg : Slack_t.post_message_req ) =
@@ -41,6 +51,8 @@ module Slack_simple : Api.Slack = struct
41
51
end
42
52
43
53
module Slack_json : Api .Slack = struct
54
+ include Slack_base
55
+
44
56
let log = Log. from " slack"
45
57
46
58
let send_notification ~ctx :_ ~(msg : Slack_t.post_message_req ) =
Original file line number Diff line number Diff line change @@ -81,4 +81,26 @@ module Slack : Api.Slack = struct
81
81
)
82
82
| Error e -> Lwt. return @@ build_query_error url e
83
83
)
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\n failed to unfurl Slack links" msg
103
+ )
104
+ | Error e -> Lwt. return @@ fmt_error " error while querying %s: %s\n failed to unfurl Slack links" url e
105
+ )
84
106
end
You can’t perform that action at this time.
0 commit comments