From b54bd857f4350d0b241379233582a5c407766e97 Mon Sep 17 00:00:00 2001 From: kouwakai Date: Tue, 13 Dec 2022 09:42:09 +0900 Subject: [PATCH 1/8] Add example of updating modal --- examples/modal/modal.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/examples/modal/modal.go b/examples/modal/modal.go index e628e853f..60466ae5d 100644 --- a/examples/modal/modal.go +++ b/examples/modal/modal.go @@ -60,6 +60,30 @@ func generateModalRequest() slack.ModalViewRequest { return modalRequest } +func updateModal() slack.ModalViewRequest { + // Create a ModalViewRequest with a header and two inputs + titleText := slack.NewTextBlockObject("plain_text", "My App", false, false) + closeText := slack.NewTextBlockObject("plain_text", "Close", false, false) + submitText := slack.NewTextBlockObject("plain_text", "Submit", false, false) + + headerText := slack.NewTextBlockObject("mrkdwn", "Modal updated!", false, false) + headerSection := slack.NewSectionBlock(headerText, nil, nil) + + blocks := slack.Blocks{ + BlockSet: []slack.Block{ + headerSection, + }, + } + + var modalRequest slack.ModalViewRequest + modalRequest.Type = slack.ViewType("modal") + modalRequest.Title = titleText + modalRequest.Close = closeText + modalRequest.Submit = submitText + modalRequest.Blocks = blocks + return modalRequest +} + // This was taken from the slash example // https://github.com/slack-go/slack/blob/master/examples/slash/slash.go func verifySigningSecret(r *http.Request) error { @@ -149,6 +173,22 @@ func handleModal(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusUnauthorized) return } + + // update modal sample + switch i.Type { + //update when interaction type is view_submission + case slack.InteractionTypeViewSubmission: + api := slack.New("YOUR_TOKEN_HERE") + //you can use any modal you want to show to users just like creating modal. + updateModal := updateModal() + //you can use hash and ViewID from payload and externalID if exists + _, err := api.UpdateView(updateModal, "", i.View.Hash, i.View.ID) + if err != nil { + fmt.Printf("Error updating view: %s", err) + w.WriteHeader(http.StatusUnauthorized) + return + } + } } func main() { From 7589264eeb0012925b52b577c58f40ef6a8c3747 Mon Sep 17 00:00:00 2001 From: KouWakai Date: Sun, 25 Dec 2022 13:24:44 +0900 Subject: [PATCH 2/8] delete dupulicated code at L181 --- examples/modal/modal.go | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/modal/modal.go b/examples/modal/modal.go index 60466ae5d..b5a9001d1 100644 --- a/examples/modal/modal.go +++ b/examples/modal/modal.go @@ -178,7 +178,6 @@ func handleModal(w http.ResponseWriter, r *http.Request) { switch i.Type { //update when interaction type is view_submission case slack.InteractionTypeViewSubmission: - api := slack.New("YOUR_TOKEN_HERE") //you can use any modal you want to show to users just like creating modal. updateModal := updateModal() //you can use hash and ViewID from payload and externalID if exists From c4f3ebe4586df3cf1f61114dac0527e41b17b8ea Mon Sep 17 00:00:00 2001 From: KouWakai <35868597+KouWakai@users.noreply.github.com> Date: Sun, 25 Dec 2022 13:26:25 +0900 Subject: [PATCH 3/8] Change comments at L183 to suggestion Co-authored-by: Naoki Kanatani --- examples/modal/modal.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/modal/modal.go b/examples/modal/modal.go index b5a9001d1..dd337542d 100644 --- a/examples/modal/modal.go +++ b/examples/modal/modal.go @@ -180,7 +180,8 @@ func handleModal(w http.ResponseWriter, r *http.Request) { case slack.InteractionTypeViewSubmission: //you can use any modal you want to show to users just like creating modal. updateModal := updateModal() - //you can use hash and ViewID from payload and externalID if exists + // You must set one of external_id or view_id and you can use hash for avoiding race condition. + // More details: https://api.slack.com/surfaces/modals/using#updating_apis _, err := api.UpdateView(updateModal, "", i.View.Hash, i.View.ID) if err != nil { fmt.Printf("Error updating view: %s", err) From 824342ea812a3cd131d53c6063d7b7f166325908 Mon Sep 17 00:00:00 2001 From: KouWakai <35868597+KouWakai@users.noreply.github.com> Date: Sun, 25 Dec 2022 13:28:00 +0900 Subject: [PATCH 4/8] Change http response StatusUnauthorized at L188 to StatusInternalServerError Co-authored-by: Naoki Kanatani --- examples/modal/modal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/modal/modal.go b/examples/modal/modal.go index dd337542d..92bfc8b00 100644 --- a/examples/modal/modal.go +++ b/examples/modal/modal.go @@ -185,7 +185,7 @@ func handleModal(w http.ResponseWriter, r *http.Request) { _, err := api.UpdateView(updateModal, "", i.View.Hash, i.View.ID) if err != nil { fmt.Printf("Error updating view: %s", err) - w.WriteHeader(http.StatusUnauthorized) + w.WriteHeader(http.StatusInternalServerError) return } } From 311ff6a5944d11fdc16e41c98ad18ac2abb7f943 Mon Sep 17 00:00:00 2001 From: KouWakai <35868597+KouWakai@users.noreply.github.com> Date: Tue, 15 Aug 2023 15:35:49 +0900 Subject: [PATCH 5/8] change case "/humboldttest": to case "/slash" to handle http request correctly --- examples/modal/modal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/modal/modal.go b/examples/modal/modal.go index 92bfc8b00..890356163 100644 --- a/examples/modal/modal.go +++ b/examples/modal/modal.go @@ -128,7 +128,7 @@ func handleSlash(w http.ResponseWriter, r *http.Request) { } switch s.Command { - case "/humboldttest": + case "/slash": api := slack.New("YOUR_TOKEN_HERE") modalRequest := generateModalRequest() _, err = api.OpenView(s.TriggerID, modalRequest) From 7b783e650bcbfa5f773dcbd1c8b79dc31497e8c6 Mon Sep 17 00:00:00 2001 From: KouWakai <35868597+KouWakai@users.noreply.github.com> Date: Tue, 15 Aug 2023 15:37:30 +0900 Subject: [PATCH 6/8] remove post message because it's not related to sample of updating modal --- examples/modal/modal.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/examples/modal/modal.go b/examples/modal/modal.go index 890356163..b38c8e3d8 100644 --- a/examples/modal/modal.go +++ b/examples/modal/modal.go @@ -158,16 +158,7 @@ func handleModal(w http.ResponseWriter, r *http.Request) { return } - // Note there might be a better way to get this info, but I figured this structure out from looking at the json response - firstName := i.View.State.Values["First Name"]["firstName"].Value - lastName := i.View.State.Values["Last Name"]["lastName"].Value - - msg := fmt.Sprintf("Hello %s %s, nice to meet you!", firstName, lastName) - api := slack.New("YOUR_TOKEN_HERE") - _, _, err = api.PostMessage(i.User.ID, - slack.MsgOptionText(msg, false), - slack.MsgOptionAttachments()) if err != nil { fmt.Printf(err.Error()) w.WriteHeader(http.StatusUnauthorized) From 6fe1bec52fc953d4a91206c633ff27824a293921 Mon Sep 17 00:00:00 2001 From: KouWakai <35868597+KouWakai@users.noreply.github.com> Date: Tue, 15 Aug 2023 15:40:50 +0900 Subject: [PATCH 7/8] add time.sleep to wait to see updated modal --- examples/modal/modal.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/modal/modal.go b/examples/modal/modal.go index b38c8e3d8..49ffa863c 100644 --- a/examples/modal/modal.go +++ b/examples/modal/modal.go @@ -174,6 +174,8 @@ func handleModal(w http.ResponseWriter, r *http.Request) { // You must set one of external_id or view_id and you can use hash for avoiding race condition. // More details: https://api.slack.com/surfaces/modals/using#updating_apis _, err := api.UpdateView(updateModal, "", i.View.Hash, i.View.ID) + // Wait for a few seconds to see result this code is necesarry due to slack server modal is going to be closed after the update + time.Sleep(time.Second * 2) if err != nil { fmt.Printf("Error updating view: %s", err) w.WriteHeader(http.StatusInternalServerError) From cf4e8bcedf039e5c9a6d9bcdaad069a5a9561db1 Mon Sep 17 00:00:00 2001 From: KouWakai <35868597+KouWakai@users.noreply.github.com> Date: Sat, 19 Aug 2023 12:26:03 +0900 Subject: [PATCH 8/8] add time pkg --- examples/modal/modal.go | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/modal/modal.go b/examples/modal/modal.go index 49ffa863c..155f2f714 100644 --- a/examples/modal/modal.go +++ b/examples/modal/modal.go @@ -19,6 +19,7 @@ import ( "net/http" "github.com/slack-go/slack" + "time" ) func generateModalRequest() slack.ModalViewRequest {