Skip to content

Commit d527ce4

Browse files
committed
update slack oauth path and standardize slack req/res naming
In anticipation of future additional Slack workflows other than message posting and oauth, like listening to incoming events.
1 parent a80e489 commit d527ce4

File tree

8 files changed

+17
-17
lines changed

8 files changed

+17
-17
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ If you are preparing the app for distribution, you also need to enable automatic
4444

4545
You should...
4646

47-
1. In your Slack app dashboard, click on "OAuth & Permissions" in the sidebar. Set the *Redirect URL* to be `<server_domain>/slack`, and ensure your bot has a *Bot Token Scope* of `chat:write`.
47+
1. In your Slack app dashboard, click on "OAuth & Permissions" in the sidebar. Set the *Redirect URL* to be `<server_domain>/slack/oauth`, and ensure your bot has a *Bot Token Scope* of `chat:write`.
4848
1. You can optionally provide a value for `slack_oauth_state` in your secrets file to [avoid forgery attacks](https://tools.ietf.org/html/rfc6749#section-4.1.1) during the OAuth exchange.
49-
1. Launch the server with the `run` command. Make sure it's reachable externally from same the `server_domain` you used for the GitHub Payload URL. The bot server will listen on `/slack` for incoming OAuth requests.
49+
1. Launch the server with the `run` command. Make sure it's reachable externally from same the `server_domain` you used for the GitHub Payload URL. The bot server will listen on `/slack/oauth` for incoming OAuth requests.
5050
1. When the server completes an OAuth exchange and doesn't have an access token defined yet, the secrets file on the server will be regenerated to include a `slack_access_token` field.
5151

5252
Your users should...
5353

54-
1. Go to the following address, replacing the appropriate values (the `state` argument is only needed if you set `slack_oauth_state` in the previous step). `https://slack.com/oauth/v2/authorize?scope=chat:write&client_id=<slack_client_id>&redirect_uri=<server_domain>/slack&state=<slack_oauth_state>`
54+
1. Go to the following address, replacing the appropriate values (the `state` argument is only needed if you set `slack_oauth_state` in the previous step). `https://slack.com/oauth/v2/authorize?scope=chat:write&client_id=<slack_client_id>&redirect_uri=<server_domain>/slack/oauth&state=<slack_oauth_state>`
5555
1. A page should open asking for permission to install the bot to the workspace. When prompted, click "Allow".
5656
1. Add the bot to all Slack channels the user wants to send notifications to.
5757

lib/action.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
171171
| _ -> Lwt.return []
172172

173173
let send_notifications (ctx : Context.t) notifications =
174-
let notify (msg : Slack_t.message) =
174+
let notify (msg : Slack_t.post_message_req) =
175175
match%lwt Slack_api.send_notification ~ctx ~msg with
176176
| Ok () -> Lwt.return_unit
177177
| Error e -> action_error e

lib/api.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module type Github = sig
99
end
1010

1111
module type Slack = sig
12-
val send_notification : ctx:Context.t -> msg:message -> (unit, string) Result.t Lwt.t
12+
val send_notification : ctx:Context.t -> msg:post_message_req -> (unit, string) Result.t Lwt.t
1313

1414
val update_access_token_of_context : ctx:Context.t -> code:string -> (unit, string) Result.t Lwt.t
1515
end

lib/api_local.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ end
2222

2323
module Slack : Api.Slack = struct
2424
let send_notification ~ctx:_ ~msg =
25-
let json = msg |> Slack_j.string_of_message |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
25+
let json = msg |> Slack_j.string_of_post_message_req |> Yojson.Basic.from_string |> Yojson.Basic.pretty_to_string in
2626
Stdio.printf "will notify #%s\n" msg.channel;
2727
Stdio.printf "%s\n" json;
2828
Lwt.return @@ Ok ()
@@ -35,7 +35,7 @@ end
3535
module Slack_simple : Api.Slack = struct
3636
let log = Log.from "slack"
3737

38-
let send_notification ~ctx:_ ~(msg : Slack_t.message) =
38+
let send_notification ~ctx:_ ~(msg : Slack_t.post_message_req) =
3939
log#info "will notify %s%s" msg.channel
4040
( match msg.Slack_t.text with
4141
| None -> ""
@@ -50,7 +50,7 @@ module Slack_json : Api.Slack = struct
5050
let log = Log.from "slack"
5151

5252
let send_notification ~ctx:_ ~msg =
53-
let json = Slack_j.string_of_message msg in
53+
let json = Slack_j.string_of_post_message_req msg in
5454
log#info "will notify %s" msg.channel;
5555
let url = Uri.of_string "https://api.slack.com/docs/messages/builder" in
5656
let url = Uri.add_query_param url ("msg", [ json ]) in

lib/api_remote.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ module Slack : Api.Slack = struct
5656
| None -> Lwt.return @@ fmt_error "failed to retrieve Slack access token"
5757
| Some access_token ->
5858
let url = "https://slack.com/api/chat.postMessage" in
59-
let data = Slack_j.string_of_message msg in
59+
let data = Slack_j.string_of_post_message_req msg in
6060
let headers = [ Printf.sprintf "Authorization: Bearer %s" access_token ] in
6161
let body = `Raw ("application/json", data) in
6262
log#info "sending to %s : %s" msg.channel data;
6363
( match%lwt http_request ~body ~headers `POST url with
6464
| Ok s ->
65-
let res = Slack_j.message_response_of_string s in
65+
let res = Slack_j.post_message_res_of_string s in
6666
if res.ok then Lwt.return @@ Ok ()
6767
else (
6868
let msg = Option.value ~default:"an unknown error occurred" res.error in
@@ -81,7 +81,7 @@ module Slack : Api.Slack = struct
8181
match%lwt Common.http_request ~body ~headers `POST "https://slack.com/api/oauth.v2.access" with
8282
| Error e -> Lwt.return @@ Error e
8383
| Ok data ->
84-
let response = Slack_j.oauth_access_response_of_string data in
84+
let response = Slack_j.oauth_access_res_of_string data in
8585
( match response.access_token, response.error with
8686
| Some access_token, _ -> Lwt.return @@ Ok access_token
8787
| None, Some e -> Lwt.return @@ Error e

lib/slack.atd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,21 @@ type message_block = [
5353
| Divider of message_divider_block
5454
] <json adapter.ocaml="Atdgen_runtime.Json_adapter.Type_field">
5555

56-
type message = {
56+
type post_message_req = {
5757
channel: string;
5858
?text: string nullable;
5959
?attachments: message_attachment list nullable;
6060
?blocks: message_block list nullable;
6161
}
6262

6363
(* expected payload when exchanging oauth code for access token *)
64-
type oauth_access_response = {
64+
type oauth_access_res = {
6565
ok: bool;
6666
?access_token: string option;
6767
?error: string option;
6868
}
6969

70-
type message_response = {
70+
type post_message_res = {
7171
ok: bool;
7272
?channel: string option;
7373
?error: string option;

src/monorobot.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ let check_gh_action file json config secrets state =
4646
let check_slack_action file secrets =
4747
let data = Stdio.In_channel.read_all file in
4848
let ctx = Context.make ~secrets_filepath:secrets () in
49-
match Slack_j.message_of_string data with
49+
match Slack_j.post_message_req_of_string data with
5050
| exception exn -> log#error ~exn "unable to parse notification"
5151
| msg ->
5252
match Context.refresh_secrets ctx with

src/request_handler.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ let setup_http ~ctx ~signature ~port ~ip =
3838
log#info "%s" request.body;
3939
let%lwt () = Action.process_github_notification ctx request.headers request.body in
4040
ret (Lwt.return "ok")
41-
| _, [ "slack" ] ->
42-
log#info "%s" request.body;
41+
| _, [ "slack"; "oauth" ] ->
42+
log#info "slack oauth authorization request received";
4343
let%lwt () = Action.process_slack_oauth ctx request.args in
4444
ret (Lwt.return "ok")
4545
| _, _ ->

0 commit comments

Comments
 (0)