File: dialog.go

package info (click to toggle)
golang-github-slack-go-slack 0.11.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,720 kB
  • sloc: makefile: 54
file content (98 lines) | stat: -rw-r--r-- 2,618 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strings"

	"github.com/slack-go/slack"
)

var api = slack.New("YOUR_TOKEN")
var signingSecret = "YOUR_SIGNING_SECRET"

// You can open a dialog with a user interaction. (like pushing buttons, slash commands ...)
// https://api.slack.com/surfaces/modals
// https://api.slack.com/interactivity/entry-points
func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":3000", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {

	// Read request body
	defer r.Body.Close()
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Printf("[ERROR] Fail to read request body: %v", err)
		return
	}

	// Verify signing secret
	sv, err := slack.NewSecretsVerifier(r.Header, signingSecret)
	if err != nil {
		w.WriteHeader(http.StatusUnauthorized)
		log.Printf("[ERROR] Fail to verify SigningSecret: %v", err)
		return
	}
	sv.Write(body)
	if err := sv.Ensure(); err != nil {
		w.WriteHeader(http.StatusUnauthorized)
		log.Printf("[ERROR] Fail to verify SigningSecret: %v", err)
		return
	}

	// Parse request body
	str, _ := url.QueryUnescape(string(body))
	str = strings.Replace(str, "payload=", "", 1)
	var message slack.InteractionCallback
	if err := json.Unmarshal([]byte(str), &message); err != nil {
		log.Printf("[ERROR] Fail to unmarshal json: %v", err)
		return
	}

	switch message.Type {
	case slack.InteractionTypeInteractionMessage:
		// Make new dialog components and open a dialog.
		// Component-Text
		textInput := slack.NewTextInput("TextSample", "Sample label - Text", "Default value")

		// Component-TextArea
		textareaInput := slack.NewTextAreaInput("TexaAreaSample", "Sample label - TextArea", "Default value")

		// Component-Select menu
		option1 := slack.DialogSelectOption{
			Label: "Display name 1",
			Value: "Inner value 1",
		}
		option2 := slack.DialogSelectOption{
			Label: "Display name 2",
			Value: "Inner value 2",
		}
		options := []slack.DialogSelectOption{option1, option2}
		selectInput := slack.NewStaticSelectDialogInput("SelectSample", "Sample label - Select", options)

		// Open a dialog
		elements := []slack.DialogElement{
			textInput,
			textareaInput,
			selectInput,
		}
		dialog := slack.Dialog{
			CallbackID:  "Callback_ID",
			Title:       "Dialog title",
			SubmitLabel: "Submit",
			Elements:    elements,
		}
		api.OpenDialog(message.TriggerID, dialog)

	case slack.InteractionTypeDialogSubmission:
		// Receive a notification of a dialog submission
		log.Printf("Successfully receive a dialog submission.")
	}
}