Debian Codegraph @

examples/modal/modal.go

raw symbols SCIP
1 // Modal example - How to respond to a slash command with an interactive modal and parse the response
2 // The flow of this example:
3 // 1. User trigers your app with a slash command (e.g. /modaltest) that will send a request to http://URL/slash and respond with a request to open a modal
4 // 2. User fills out fields first and last name in modal and hits submit
5 // 3. This will send a request to http://URL/modal and send a greeting message to the user
6
7 // Note: Within your slack app you will need to enable and provide a URL for "Interactivity & Shortcuts" and "Slash Commands"
8 // Note: Be sure to update YOUR_SIGNING_SECRET_HERE and YOUR_TOKEN_HERE
9 // You can use ngrok to test this example: https://api.slack.com/tutorials/tunneling-with-ngrok
10 // Helpful slack documentation to learn more: https://api.slack.com/interactivity/handling
11
12 package main
13
14 import (
15 "bytes"
16 "encoding/json"
17 "fmt"
18 "io/ioutil"
19 "net/http"
20
21 "github.com/slack-go/slack"
22 )
23
24 func generateModalRequest() slack.ModalViewRequest {
25 // Create a ModalViewRequest with a header and two inputs
26 titleText := slack.NewTextBlockObject("plain_text", "My App", false, false)
27 closeText := slack.NewTextBlockObject("plain_text", "Close", false, false)
28 submitText := slack.NewTextBlockObject("plain_text", "Submit", false, false)
29
30 headerText := slack.NewTextBlockObject("mrkdwn", "Please enter your name", false, false)
31 headerSection := slack.NewSectionBlock(headerText, nil, nil)
32
33 firstNameText := slack.NewTextBlockObject("plain_text", "First Name", false, false)
34 firstNameHint := slack.NewTextBlockObject("plain_text", "First Name Hint", false, false)
35 firstNamePlaceholder := slack.NewTextBlockObject("plain_text", "Enter your first name", false, false)
36 firstNameElement := slack.NewPlainTextInputBlockElement(firstNamePlaceholder, "firstName")
37 // Notice that blockID is a unique identifier for a block
38 firstName := slack.NewInputBlock("First Name", firstNameText, firstNameHint, firstNameElement)
39
40 lastNameText := slack.NewTextBlockObject("plain_text", "Last Name", false, false)
41 lastNameHint := slack.NewTextBlockObject("plain_text", "Last Name Hint", false, false)
42 lastNamePlaceholder := slack.NewTextBlockObject("plain_text", "Enter your first name", false, false)
43 lastNameElement := slack.NewPlainTextInputBlockElement(lastNamePlaceholder, "lastName")
44 lastName := slack.NewInputBlock("Last Name", lastNameText, lastNameHint, lastNameElement)
45
46 blocks := slack.Blocks{
47 BlockSet: []slack.Block{
48 headerSection,
49 firstName,
50 lastName,
51 },
52 }
53
54 var modalRequest slack.ModalViewRequest
55 modalRequest.Type = slack.ViewType("modal")
56 modalRequest.Title = titleText
57 modalRequest.Close = closeText
58 modalRequest.Submit = submitText
59 modalRequest.Blocks = blocks
60 return modalRequest
61 }
62
63 // This was taken from the slash example
64 // https://github.com/slack-go/slack/blob/master/examples/slash/slash.go
65 func verifySigningSecret(r *http.Request) error {
66 signingSecret := "YOUR_SIGNING_SECRET_HERE"
67 verifier, err := slack.NewSecretsVerifier(r.Header, signingSecret)
68 if err != nil {
69 fmt.Println(err.Error())
70 return err
71 }
72
73 body, err := ioutil.ReadAll(r.Body)
74 if err != nil {
75 fmt.Println(err.Error())
76 return err
77 }
78 // Need to use r.Body again when unmarshalling SlashCommand and InteractionCallback
79 r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
80
81 verifier.Write(body)
82 if err = verifier.Ensure(); err != nil {
83 fmt.Println(err.Error())
84 return err
85 }
86
87 return nil
88 }
89
90 func handleSlash(w http.ResponseWriter, r *http.Request) {
91
92 err := verifySigningSecret(r)
93 if err != nil {
94 fmt.Printf(err.Error())
95 w.WriteHeader(http.StatusUnauthorized)
96 return
97 }
98
99 s, err := slack.SlashCommandParse(r)
100 if err != nil {
101 w.WriteHeader(http.StatusInternalServerError)
102 fmt.Println(err.Error())
103 return
104 }
105
106 switch s.Command {
107 case "/humboldttest":
108 api := slack.New("YOUR_TOKEN_HERE")
109 modalRequest := generateModalRequest()
110 _, err = api.OpenView(s.TriggerID, modalRequest)
111 if err != nil {
112 fmt.Printf("Error opening view: %s", err)
113 }
114 default:
115 w.WriteHeader(http.StatusInternalServerError)
116 return
117 }
118 }
119
120 func handleModal(w http.ResponseWriter, r *http.Request) {
121
122 err := verifySigningSecret(r)
123 if err != nil {
124 fmt.Printf(err.Error())
125 w.WriteHeader(http.StatusUnauthorized)
126 return
127 }
128
129 var i slack.InteractionCallback
130 err = json.Unmarshal([]byte(r.FormValue("payload")), &i)
131 if err != nil {
132 fmt.Printf(err.Error())
133 w.WriteHeader(http.StatusUnauthorized)
134 return
135 }
136
137 // Note there might be a better way to get this info, but I figured this structure out from looking at the json response
138 firstName := i.View.State.Values["First Name"]["firstName"].Value
139 lastName := i.View.State.Values["Last Name"]["lastName"].Value
140
141 msg := fmt.Sprintf("Hello %s %s, nice to meet you!", firstName, lastName)
142
143 api := slack.New("YOUR_TOKEN_HERE")
144 _, _, err = api.PostMessage(i.User.ID,
145 slack.MsgOptionText(msg, false),
146 slack.MsgOptionAttachments())
147 if err != nil {
148 fmt.Printf(err.Error())
149 w.WriteHeader(http.StatusUnauthorized)
150 return
151 }
152 }
153
154 func main() {
155 http.HandleFunc("/slash", handleSlash)
156 http.HandleFunc("/modal", handleModal)
157 http.ListenAndServe(":4390", nil)
158 }