Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of updating modal #1142

Merged
merged 9 commits into from
Feb 10, 2024
40 changes: 40 additions & 0 deletions examples/modal/modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get api.UpdateView to work under this event. I had to use slack.InteractionTypeBlockActions e.g block_action. Is this working for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback,I would check that.

Copy link
Contributor Author

@KouWakai KouWakai Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's working but modal closes right after update.

I think this is caused by slack server.
after update modal,app respond http status 200 to slack server and the server see it as over

I'v been running this code on local and connect via ngrok and this happen

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by adding time.Sleep(time.Second * 2) after api.UpdateView you can prevent sending http 200 and can see updated modal.

	case slack.InteractionTypeViewSubmission:
		//you can use any modal you want to show to users just like creating modal.
		updateModal := updateModal()
		// 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)
		time.Sleep(time.Second * 2)
		if err != nil {
			fmt.Printf("Error updating view: %s", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kanata2 @charlesoconor
sorry for kept you waiting,could you review this code?

api := slack.New("YOUR_TOKEN_HERE")
kanata2 marked this conversation as resolved.
Show resolved Hide resolved
//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
KouWakai marked this conversation as resolved.
Show resolved Hide resolved
_, err := api.UpdateView(updateModal, "", i.View.Hash, i.View.ID)
if err != nil {
fmt.Printf("Error updating view: %s", err)
w.WriteHeader(http.StatusUnauthorized)
KouWakai marked this conversation as resolved.
Show resolved Hide resolved
return
}
}
}

func main() {
Expand Down