forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
99 lines (84 loc) · 4.88 KB
/
users.go
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
99
package main
import (
"fmt"
"github.com/melisa92/slack"
)
// An example how to open a modal with different kinds of input fields
func main() {
// Create a ModalViewRequest with a header and two inputs
titleText := slack.NewTextBlockObject(slack.PlainTextType, "Create channel demo", false, false)
closeText := slack.NewTextBlockObject(slack.PlainTextType, "Close", false, false)
submitText := slack.NewTextBlockObject(slack.PlainTextType, "Submit", false, false)
contextText := slack.NewTextBlockObject(slack.MarkdownType, "This app demonstrates the use of different fields", false, false)
contextBlock := slack.NewContextBlock("context", contextText)
// Only the inputs in input blocks will be included in view_submission’s view.state.values: https://slack.dev/java-slack-sdk/guides/modals
// 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
channelNameText := slack.NewTextBlockObject(slack.PlainTextType, "Channel Name", false, false)
channelPlaceholder := slack.NewTextBlockObject(slack.PlainTextType, "New channel name", false, false)
channelNameElement := slack.NewPlainTextInputBlockElement(channelPlaceholder, "channel_name")
// Slack channel names can be maximum 80 characters: https://api.slack.com/methods/conversations.create
channelNameElement.MaxLength = 80
channelNameBlock := slack.NewInputBlock("channel_name", channelNameText, channelNameElement)
channelNameBlock.Hint = slack.NewTextBlockObject(slack.PlainTextType, "Channel names may only contain lowercase letters, numbers, hyphens, and underscores, and must be 80 characters or less", false, false)
// Provide a static list of users to choose from, those provided now are just made up user IDs
// Get user IDs by right clicking on them in Slack, select "Copy link", and inspect the last part of the link
// The user ID should start with "U" followed by 8 random characters
memberOptions := createOptionBlockObjects([]string{"U9911MMAA", "U2233KKNN", "U00112233"}, true)
inviteeText := slack.NewTextBlockObject(slack.PlainTextType, "Invitee from static list", false, false)
inviteeOption := slack.NewOptionsSelectBlockElement(slack.OptTypeStatic, nil, "invitee", memberOptions...)
inviteeBlock := slack.NewInputBlock("invitee", inviteeText, inviteeOption)
additionalInviteeText := slack.NewTextBlockObject(slack.PlainTextType, "Invitee from complete list of users", false, false)
additionalInviteeOption := slack.NewOptionsSelectBlockElement(slack.OptTypeUser, additionalInviteeText, "")
additionalInviteeSection := slack.NewSectionBlock(additionalInviteeText, nil, slack.NewAccessory(additionalInviteeOption))
checkboxTxt := slack.NewTextBlockObject(slack.PlainTextType, "Checkbox", false, false)
checkboxOptions := createOptionBlockObjects([]string{"option 1", "option 2", "option 3"}, false)
checkboxOptionsBlock := slack.NewCheckboxGroupsBlockElement("chkbox", checkboxOptions...)
checkboxBlock := slack.NewInputBlock("chkbox", checkboxTxt, checkboxOptionsBlock)
summaryText := slack.NewTextBlockObject(slack.PlainTextType, "Summary", false, false)
summaryPlaceholder := slack.NewTextBlockObject(slack.PlainTextType, "Summary of reason for creating channel", false, false)
summaryElement := slack.NewPlainTextInputBlockElement(summaryPlaceholder, "summary")
// Just set an arbitrary max length to avoid too prose summary
summaryElement.MaxLength = 200
summaryElement.Multiline = true
summaryBlock := slack.NewInputBlock("summary", summaryText, summaryElement)
blocks := slack.Blocks{
BlockSet: []slack.Block{
contextBlock,
channelNameBlock,
inviteeBlock,
additionalInviteeSection,
checkboxBlock,
summaryBlock,
},
}
var modalRequest slack.ModalViewRequest
modalRequest.Type = slack.ViewType("modal")
modalRequest.Title = titleText
modalRequest.Close = closeText
modalRequest.Submit = submitText
modalRequest.Blocks = blocks
modalRequest.CallbackID = "create_channel"
api := slack.New("YOUR_BOT_TOKEN_HERE")
// Using a trigger ID you can open a modal
// The trigger ID is provided through certain events and interactions
// More information can be found here: https://api.slack.com/interactivity/handling#modal_responses
_, err := api.OpenView("YOUR_TRIGGERID_HERE", modalRequest)
if err != nil {
fmt.Printf("Error opening view: %s", err)
}
}
// createOptionBlockObjects - utility function for generating option block objects
func createOptionBlockObjects(options []string, users bool) []*slack.OptionBlockObject {
optionBlockObjects := make([]*slack.OptionBlockObject, 0, len(options))
var text string
for _, o := range options {
if users {
text = fmt.Sprintf("<@%s>", o)
} else {
text = o
}
optionText := slack.NewTextBlockObject(slack.PlainTextType, text, false, false)
optionBlockObjects = append(optionBlockObjects, slack.NewOptionBlockObject(o, optionText, nil))
}
return optionBlockObjects
}