-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathModalViewDemo.cs
249 lines (235 loc) · 11.8 KB
/
ModalViewDemo.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System.Text.Json;
using SlackNet;
using SlackNet.Blocks;
using SlackNet.Events;
using SlackNet.Interaction;
using SlackNet.WebApi;
using Button = SlackNet.Blocks.Button;
using Option = SlackNet.Blocks.Option;
namespace SlackNetDemo;
/// <summary>
/// Opens a modal view with a range of different inputs.
/// </summary>
class ModalViewDemo(ISlackApiClient slack) : IEventHandler<MessageEvent>, IBlockActionHandler<ButtonAction>, IViewSubmissionHandler
{
public const string Trigger = "modal demo";
public const string OpenModal = "open_modal";
private const string InputActionId = "text_input";
private const string SingleSelectActionId = "single_select";
private const string MultiSelectActionId = "multi_select";
private const string DatePickerActionId = "date_picker";
private const string TimePickerActionId = "time_picker";
private const string RadioActionId = "radio";
private const string CheckboxActionId = "checkbox";
private const string SingleUserActionId = "single_user";
private const string RichTextActionId = "rich_text_input";
private const string FileInputActionId = "file_input";
public const string ModalCallbackId = "modal_demo";
public async Task Handle(MessageEvent slackEvent)
{
if (slackEvent.Text?.Contains(Trigger, StringComparison.OrdinalIgnoreCase) == true)
{
Console.WriteLine($"{(await slack.Users.Info(slackEvent.User)).Name} asked for a modal view demo in the {(await slack.Conversations.Info(slackEvent.Channel)).Name} channel");
await slack.Chat.PostMessage(new Message
{
Channel = slackEvent.Channel,
Blocks =
{
new SectionBlock { Text = "Here's the modal view demo" },
new ActionsBlock
{
Elements =
{
new Button
{
ActionId = OpenModal,
Text = "Open modal"
}
}
}
}
});
}
}
public async Task Handle(ButtonAction action, BlockActionRequest request)
{
Console.WriteLine($"{request.User.Name} clicked the Open modal button in the {request.Channel.Name} channel");
await slack.Views.Open(request.TriggerId, new ModalViewDefinition
{
Title = "Example Modal",
CallbackId = ModalCallbackId,
Blocks =
{
new InputBlock
{
Label = "Input",
BlockId = "input_block",
Optional = true,
Element = new PlainTextInput
{
ActionId = InputActionId,
Placeholder = "Enter some text"
}
},
new InputBlock
{
Label = "Single-select",
BlockId = "single_select_block",
Optional = true,
Element = new StaticSelectMenu
{
ActionId = SingleSelectActionId,
Options = ExampleOptions()
}
},
new InputBlock
{
Label = "Multi-select",
BlockId = "multi_select_block",
Optional = true,
Element = new StaticMultiSelectMenu
{
ActionId = MultiSelectActionId,
Options = ExampleOptions()
}
},
new InputBlock
{
Label = "Date",
BlockId = "date_block",
Optional = true,
Element = new DatePicker { ActionId = DatePickerActionId }
},
new InputBlock
{
Label = "Time",
BlockId = "time_block",
Optional = true,
Element = new TimePicker { ActionId = TimePickerActionId }
},
new InputBlock
{
Label = "Radio options",
BlockId = "radio_block",
Optional = true,
Element = new RadioButtonGroup
{
ActionId = RadioActionId,
Options = ExampleOptions()
}
},
new InputBlock
{
Label = "Checkbox options",
BlockId = "checkbox_block",
Optional = true,
Element = new CheckboxGroup
{
ActionId = CheckboxActionId,
Options = ExampleOptions()
}
},
new InputBlock
{
Label = "Single user select",
BlockId = "single_user_block",
Optional = true,
Element = new UserSelectMenu
{
ActionId = SingleUserActionId
}
},
new InputBlock
{
Label = "Rich text",
BlockId = "rich_text_block",
Optional = true,
Element = new RichTextInput
{
ActionId = RichTextActionId,
Placeholder = "Enter some rich text"
}
},
new InputBlock
{
Label = "File upload",
BlockId = "file_input_block",
Optional = true,
Element = new FileInput
{
ActionId = FileInputActionId,
MaxFiles = 3
}
}
},
Submit = "Submit",
NotifyOnClose = true,
PrivateMetadata = JsonSerializer.Serialize(new ModalMetadata(request.Channel.Id, request.Channel.Name)) // Holding onto this info for later
});
}
private static IList<Option> ExampleOptions() =>
[
new() { Text = "One", Value = "1" },
new() { Text = "Two", Value = "2" },
new() { Text = "Three", Value = "3" }
];
public async Task<ViewSubmissionResponse> Handle(ViewSubmission viewSubmission)
{
var metadata = JsonSerializer.Deserialize<ModalMetadata>(viewSubmission.View.PrivateMetadata)!;
Console.WriteLine($"{viewSubmission.User.Name} submitted the demo modal view in the {metadata.ChannelName} channel");
var state = viewSubmission.View.State;
var values = new Dictionary<string, string>
{
{ "Input", state.GetValue<PlainTextInputValue>(InputActionId).Value ?? "none" },
{ "Single-select", state.GetValue<StaticSelectValue>(SingleSelectActionId).SelectedOption?.Text.Text ?? "none" },
{ "Multi-select", string.Join(", ", state.GetValue<StaticMultiSelectValue>(MultiSelectActionId).SelectedOptions.Select(o => o.Text).DefaultIfEmpty("none")) },
{ "Date", state.GetValue<DatePickerValue>(DatePickerActionId).SelectedDate?.ToString("yyyy-MM-dd") ?? "none" },
{ "Time", state.GetValue<TimePickerValue>(TimePickerActionId).SelectedTime?.ToString("hh\\:mm") ?? "none" },
{ "Radio options", state.GetValue<RadioButtonGroupValue>(RadioActionId).SelectedOption?.Text.Text ?? "none" },
{ "Checkbox options", string.Join(", ", state.GetValue<CheckboxGroupValue>(CheckboxActionId).SelectedOptions.Select(o => o.Text).DefaultIfEmpty("none")) },
{ "Single user select", state.GetValue<UserSelectValue>(SingleUserActionId).SelectedUser is string userId ? Link.User(userId).ToString()! : "none" },
{ "Files", string.Join(", ", state.GetValue<FileInputValue>(FileInputActionId).Files.Select(f => f.Name).DefaultIfEmpty("none")) }
};
var richText = state.GetValue<RichTextInputValue>(RichTextActionId)?.RichTextValue
?? new RichTextBlock { Elements = { new RichTextSection { Elements = { new RichTextText { Text = "none" } } } } };
await slack.Chat.PostMessage(new Message
{
Channel = metadata.ChannelId,
Text = $"You entered: {state.GetValue<PlainTextInputValue>(InputActionId).Value}",
Blocks =
{
new SectionBlock
{
Text = new Markdown("You entered:\n"
+ string.Join("\n", values.Select(kv => $"*{kv.Key}:* {kv.Value}")))
},
new RichTextBlock
{
Elements =
{
new RichTextSection
{
Elements =
{
new RichTextText { Text = "Rich text:", Style = { Bold = true } }
}
}
}
},
richText
}
});
return ViewSubmissionResponse.Null;
}
public async Task HandleClose(ViewClosed viewClosed)
{
var metadata = JsonSerializer.Deserialize<ModalMetadata>(viewClosed.View.PrivateMetadata)!;
Console.WriteLine($"{viewClosed.User.Name} cancelled the demo modal view in the {metadata.ChannelName} channel");
await slack.Chat.PostMessage(new Message
{
Channel = metadata.ChannelId,
Text = "You cancelled the modal"
});
}
record ModalMetadata(string ChannelId, string ChannelName);
}