Debian Codegraph @

examples/modal_users/users.go

raw symbols SCIP
1 package main
2
3 import (
4 "fmt"
5
6 "github.com/slack-go/slack"
7 )
8
9 // An example how to open a modal with different kinds of input fields
10 func main() {
11
12 // Create a ModalViewRequest with a header and two inputs
13 titleText := slack.NewTextBlockObject(slack.PlainTextType, "Create channel demo", false, false)
14 closeText := slack.NewTextBlockObject(slack.PlainTextType, "Close", false, false)
15 submitText := slack.NewTextBlockObject(slack.PlainTextType, "Submit", false, false)
16
17 contextText := slack.NewTextBlockObject(slack.MarkdownType, "This app demonstrates the use of different fields", false, false)
18 contextBlock := slack.NewContextBlock("context", contextText)
19
20 // Only the inputs in input blocks will be included in view_submission’s view.state.values: https://slack.dev/java-slack-sdk/guides/modals
21 // This means the inputs will not be interactive either because they do not trigger block_actions messages: https://api.slack.com/surfaces/modals/using#interactions
22 channelNameText := slack.NewTextBlockObject(slack.PlainTextType, "Channel Name", false, false)
23 channelNameHint := slack.NewTextBlockObject(slack.PlainTextType, "Channel names may only contain lowercase letters, numbers, hyphens, and underscores, and must be 80 characters or less", false, false)
24 channelPlaceholder := slack.NewTextBlockObject(slack.PlainTextType, "New channel name", false, false)
25 channelNameElement := slack.NewPlainTextInputBlockElement(channelPlaceholder, "channel_name")
26 // Slack channel names can be maximum 80 characters: https://api.slack.com/methods/conversations.create
27 channelNameElement.MaxLength = 80
28 channelNameBlock := slack.NewInputBlock("channel_name", channelNameText, channelNameHint, channelNameElement)
29
30 // Provide a static list of users to choose from, those provided now are just made up user IDs
31 // Get user IDs by right clicking on them in Slack, select "Copy link", and inspect the last part of the link
32 // The user ID should start with "U" followed by 8 random characters
33 memberOptions := createOptionBlockObjects([]string{"U9911MMAA", "U2233KKNN", "U00112233"}, true)
34 inviteeText := slack.NewTextBlockObject(slack.PlainTextType, "Invitee from static list", false, false)
35 inviteeOption := slack.NewOptionsSelectBlockElement(slack.OptTypeStatic, nil, "invitee", memberOptions...)
36 inviteeBlock := slack.NewInputBlock("invitee", inviteeText, nil, inviteeOption)
37
38 // Section with users select - this input will not be included in the view_submission's view.state.values,
39 // but instead be sent as a "block_actions" request
40 additionalInviteeText := slack.NewTextBlockObject(slack.PlainTextType, "Invitee from complete list of users", false, false)
41 additionalInviteeHintText := slack.NewTextBlockObject(slack.PlainTextType, "", false, false)
42 additionalInviteeOption := slack.NewOptionsSelectBlockElement(slack.OptTypeUser, additionalInviteeText, "user")
43 additionalInviteeSection := slack.NewSectionBlock(additionalInviteeText, nil, slack.NewAccessory(additionalInviteeOption))
44
45 // Input with users select - this input will be included in the view_submission's view.state.values
46 // It can be fetched as for example "payload.View.State.Values["user"]["user"].SelectedUser"
47 additionalInviteeBlock := slack.NewInputBlock("user", additionalInviteeText, additionalInviteeHintText, additionalInviteeOption)
48
49 checkboxTxt := slack.NewTextBlockObject(slack.PlainTextType, "Checkbox", false, false)
50 checkboxOptions := createOptionBlockObjects([]string{"option 1", "option 2", "option 3"}, false)
51 checkboxOptionsBlock := slack.NewCheckboxGroupsBlockElement("chkbox", checkboxOptions...)
52 checkboxBlock := slack.NewInputBlock("chkbox", checkboxTxt, nil, checkboxOptionsBlock)
53
54 summaryText := slack.NewTextBlockObject(slack.PlainTextType, "Summary", false, false)
55 summaryHint := slack.NewTextBlockObject(slack.PlainTextType, "Summary Hint", false, false)
56 summaryPlaceholder := slack.NewTextBlockObject(slack.PlainTextType, "Summary of reason for creating channel", false, false)
57 summaryElement := slack.NewPlainTextInputBlockElement(summaryPlaceholder, "summary")
58 // Just set an arbitrary max length to avoid too prose summary
59 summaryElement.MaxLength = 200
60 summaryElement.Multiline = true
61 summaryBlock := slack.NewInputBlock("summary", summaryText, summaryHint, summaryElement)
62
63 blocks := slack.Blocks{
64 BlockSet: []slack.Block{
65 contextBlock,
66 channelNameBlock,
67 inviteeBlock,
68 additionalInviteeSection,
69 additionalInviteeBlock,
70 checkboxBlock,
71 summaryBlock,
72 },
73 }
74
75 var modalRequest slack.ModalViewRequest
76 modalRequest.Type = slack.ViewType("modal")
77 modalRequest.Title = titleText
78 modalRequest.Close = closeText
79 modalRequest.Submit = submitText
80 modalRequest.Blocks = blocks
81 modalRequest.CallbackID = "create_channel"
82
83 api := slack.New("YOUR_BOT_TOKEN_HERE")
84
85 // Using a trigger ID you can open a modal
86 // The trigger ID is provided through certain events and interactions
87 // More information can be found here: https://api.slack.com/interactivity/handling#modal_responses
88 _, err := api.OpenView("YOUR_TRIGGERID_HERE", modalRequest)
89 if err != nil {
90 fmt.Printf("Error opening view: %s", err)
91 }
92 }
93
94 // createOptionBlockObjects - utility function for generating option block objects
95 func createOptionBlockObjects(options []string, users bool) []*slack.OptionBlockObject {
96 optionBlockObjects := make([]*slack.OptionBlockObject, 0, len(options))
97 var text string
98 for _, o := range options {
99 if users {
100 text = fmt.Sprintf("<@%s>", o)
101 } else {
102 text = o
103 }
104 optionText := slack.NewTextBlockObject(slack.PlainTextType, text, false, false)
105 optionBlockObjects = append(optionBlockObjects, slack.NewOptionBlockObject(o, optionText, nil))
106 }
107 return optionBlockObjects
108 }