Debian Codegraph @

examples/dialog/dialog.go

raw symbols SCIP
1 package main
2
3 import (
4 "encoding/json"
5 "io/ioutil"
6 "log"
7 "net/http"
8 "net/url"
9 "strings"
10
11 "github.com/slack-go/slack"
12 )
13
14 var api = slack.New("YOUR_TOKEN")
15 var signingSecret = "YOUR_SIGNING_SECRET"
16
17 // You can open a dialog with a user interaction. (like pushing buttons, slash commands ...)
18 // https://api.slack.com/surfaces/modals
19 // https://api.slack.com/interactivity/entry-points
20 func main() {
21 http.HandleFunc("/", handler)
22 http.ListenAndServe(":3000", nil)
23 }
24
25 func handler(w http.ResponseWriter, r *http.Request) {
26
27 // Read request body
28 defer r.Body.Close()
29 body, err := ioutil.ReadAll(r.Body)
30 if err != nil {
31 w.WriteHeader(http.StatusInternalServerError)
32 log.Printf("[ERROR] Fail to read request body: %v", err)
33 return
34 }
35
36 // Verify signing secret
37 sv, err := slack.NewSecretsVerifier(r.Header, signingSecret)
38 if err != nil {
39 w.WriteHeader(http.StatusUnauthorized)
40 log.Printf("[ERROR] Fail to verify SigningSecret: %v", err)
41 return
42 }
43 sv.Write(body)
44 if err := sv.Ensure(); err != nil {
45 w.WriteHeader(http.StatusUnauthorized)
46 log.Printf("[ERROR] Fail to verify SigningSecret: %v", err)
47 return
48 }
49
50 // Parse request body
51 str, _ := url.QueryUnescape(string(body))
52 str = strings.Replace(str, "payload=", "", 1)
53 var message slack.InteractionCallback
54 if err := json.Unmarshal([]byte(str), &message); err != nil {
55 log.Printf("[ERROR] Fail to unmarshal json: %v", err)
56 return
57 }
58
59 switch message.Type {
60 case slack.InteractionTypeInteractionMessage:
61 // Make new dialog components and open a dialog.
62 // Component-Text
63 textInput := slack.NewTextInput("TextSample", "Sample label - Text", "Default value")
64
65 // Component-TextArea
66 textareaInput := slack.NewTextAreaInput("TexaAreaSample", "Sample label - TextArea", "Default value")
67
68 // Component-Select menu
69 option1 := slack.DialogSelectOption{
70 Label: "Display name 1",
71 Value: "Inner value 1",
72 }
73 option2 := slack.DialogSelectOption{
74 Label: "Display name 2",
75 Value: "Inner value 2",
76 }
77 options := []slack.DialogSelectOption{option1, option2}
78 selectInput := slack.NewStaticSelectDialogInput("SelectSample", "Sample label - Select", options)
79
80 // Open a dialog
81 elements := []slack.DialogElement{
82 textInput,
83 textareaInput,
84 selectInput,
85 }
86 dialog := slack.Dialog{
87 CallbackID: "Callback_ID",
88 Title: "Dialog title",
89 SubmitLabel: "Submit",
90 Elements: elements,
91 }
92 api.OpenDialog(message.TriggerID, dialog)
93
94 case slack.InteractionTypeDialogSubmission:
95 // Receive a notification of a dialog submission
96 log.Printf("Successfully receive a dialog submission.")
97 }
98 }