Codebase list golang-github-nlopes-slack / 2891986
Add SectionBlockOption functionality Steve Johnson authored 4 years ago James committed 4 years ago
3 changed file(s) with 44 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
1515 return s.Type
1616 }
1717
18 // SectionBlockOption allows configuration of options for a new section block
19 type SectionBlockOption func(*SectionBlock)
20
21 func SectionBlockOptionBlockID(blockID string) SectionBlockOption {
22 return func(block *SectionBlock) {
23 block.BlockID = blockID
24 }
25 }
26
1827 // NewSectionBlock returns a new instance of a section block to be rendered
19 func NewSectionBlock(blockID string, textObj *TextBlockObject, fields []*TextBlockObject, accessory *Accessory) *SectionBlock {
20 return &SectionBlock{
28 func NewSectionBlock(textObj *TextBlockObject, fields []*TextBlockObject, accessory *Accessory, options ...SectionBlockOption) *SectionBlock {
29 block := SectionBlock{
2130 Type: MBTSection,
2231 Text: textObj,
23 BlockID: blockID,
2432 Fields: fields,
2533 Accessory: accessory,
2634 }
35
36 for _, option := range options {
37 option(&block)
38 }
39
40 return &block
2741 }
99
1010 textInfo := NewTextBlockObject("mrkdwn", "*<fakeLink.toHotelPage.com|The Ritz-Carlton New Orleans>*\n★★★★★\n$340 per night\nRated: 9.1 - Excellent", false, false)
1111
12 sectionBlock := NewSectionBlock("test_block", textInfo, nil, nil)
12 sectionBlock := NewSectionBlock(textInfo, nil, nil, SectionBlockOptionBlockID("test_block"))
1313 assert.Equal(t, string(sectionBlock.Type), "section")
1414 assert.Equal(t, string(sectionBlock.BlockID), "test_block")
1515 assert.Equal(t, len(sectionBlock.Fields), 0)
4646
4747 // Header Section
4848 headerText := slack.NewTextBlockObject("mrkdwn", "You have a new request:\n*<fakeLink.toEmployeeProfile.com|Fred Enriquez - New device request>*", false, false)
49 headerSection := slack.NewSectionBlock("", headerText, nil, nil)
49 headerSection := slack.NewSectionBlock(headerText, nil, nil, nil)
5050
5151 // Fields
5252 typeField := slack.NewTextBlockObject("mrkdwn", "*Type:*\nComputer (laptop)", false, false)
6262 fieldSlice = append(fieldSlice, reasonField)
6363 fieldSlice = append(fieldSlice, specsField)
6464
65 fieldsSection := slack.NewSectionBlock("", nil, fieldSlice, nil)
65 fieldsSection := slack.NewSectionBlock(nil, fieldSlice, nil, nil)
6666
6767 // Approve and Deny Buttons
6868 approveBtnTxt := slack.NewTextBlockObject("plain_text", "Approve", false, false)
9797
9898 // Header Section
9999 headerText := slack.NewTextBlockObject("mrkdwn", "You have a new request:\n*<google.com|Fred Enriquez - Time Off request>*", false, false)
100 headerSection := slack.NewSectionBlock("", headerText, nil, nil)
100 headerSection := slack.NewSectionBlock(headerText, nil, nil, nil)
101101
102102 approvalText := slack.NewTextBlockObject("mrkdwn", "*Type:*\nPaid time off\n*When:*\nAug 10-Aug 13\n*Hours:* 16.0 (2 days)\n*Remaining balance:* 32.0 hours (4 days)\n*Comments:* \"Family in town, going camping!\"", false, false)
103103 approvalImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/approvalsNewDevice.png", "computer thumbnail")
104104
105 fieldsSection := slack.NewSectionBlock("", approvalText, nil, slack.NewAccessory(approvalImage))
105 fieldsSection := slack.NewSectionBlock(approvalText, nil, slack.NewAccessory(approvalImage), nil)
106106
107107 // Approve and Deny Buttons
108108 approveBtnTxt := slack.NewTextBlockObject("plain_text", "Approve", false, false)
141141
142142 // Header Section
143143 headerText := slack.NewTextBlockObject("plain_text", "Looks like you have a scheduling conflict with this event:", false, false)
144 headerSection := slack.NewSectionBlock("", headerText, nil, nil)
144 headerSection := slack.NewSectionBlock(headerText, nil, nil, nil)
145145
146146 // Schedule Info Section
147147 scheduleText := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toUserProfiles.com|Iris / Zelda 1-1>*\nTuesday, January 21 4:00-4:30pm\nBuilding 2 - Havarti Cheese (3)\n2 guests", false, false)
148148 scheduleAccessory := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/notifications.png", "calendar thumbnail")
149 schedeuleSection := slack.NewSectionBlock("", scheduleText, nil, slack.NewAccessory(scheduleAccessory))
149 schedeuleSection := slack.NewSectionBlock(scheduleText, nil, slack.NewAccessory(scheduleAccessory), nil)
150150
151151 // Conflict Section
152152 conflictImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/notificationsWarningIcon.png", "notifications warning icon")
159159
160160 // Proposese Text
161161 proposeText := slack.NewTextBlockObject("mrkdwn", "*Propose a new time:*", false, false)
162 proposeSection := slack.NewSectionBlock("", proposeText, nil, nil)
162 proposeSection := slack.NewSectionBlock(proposeText, nil, nil, nil)
163163
164164 // Option 1
165165 optionOneText := slack.NewTextBlockObject("mrkdwn", "*Today - 4:30-5pm*\nEveryone is available: @iris, @zelda", false, false)
166 optionOneSection := slack.NewSectionBlock("", optionOneText, nil, slack.NewAccessory(chooseBtnEle))
166 optionOneSection := slack.NewSectionBlock(optionOneText, nil, slack.NewAccessory(chooseBtnEle), nil)
167167
168168 // Option 2
169169 optionTwoText := slack.NewTextBlockObject("mrkdwn", "*Tomorrow - 4-4:30pm*\nEveryone is available: @iris, @zelda", false, false)
170 optionTwoSection := slack.NewSectionBlock("", optionTwoText, nil, slack.NewAccessory(chooseBtnEle))
170 optionTwoSection := slack.NewSectionBlock(optionTwoText, nil, slack.NewAccessory(chooseBtnEle), nil)
171171
172172 // Option 3
173173 optionThreeText := slack.NewTextBlockObject("mrkdwn", "*Tomorrow - 6-6:30pm*\nSome people aren't available: @iris, ~@zelda~", false, false)
174 optionThreeSection := slack.NewSectionBlock("", optionThreeText, nil, slack.NewAccessory(chooseBtnEle))
174 optionThreeSection := slack.NewSectionBlock(optionThreeText, nil, slack.NewAccessory(chooseBtnEle), nil)
175175
176176 // Show More Times Link
177177 showMoreText := slack.NewTextBlockObject("mrkdwn", "*<fakelink.ToMoreTimes.com|Show more times>*", false, false)
178 showMoreSection := slack.NewSectionBlock("", showMoreText, nil, nil)
178 showMoreSection := slack.NewSectionBlock(showMoreText, nil, nil, nil)
179179
180180 // Build Message with blocks created above
181181 msg := slack.NewBlockMessage(
215215
216216 // Header Section
217217 headerText := slack.NewTextBlockObject("mrkdwn", "*Where should we order lunch from?* Poll by <fakeLink.toUser.com|Mark>", false, false)
218 headerSection := slack.NewSectionBlock("", headerText, nil, nil)
218 headerSection := slack.NewSectionBlock(headerText, nil, nil, nil)
219219
220220 // Option One Info
221221 optOneText := slack.NewTextBlockObject("mrkdwn", ":sushi: *Ace Wasabi Rock-n-Roll Sushi Bar*\nThe best landlocked sushi restaurant.", false, false)
222 optOneSection := slack.NewSectionBlock("", optOneText, nil, slack.NewAccessory(voteBtnEle))
222 optOneSection := slack.NewSectionBlock(optOneText, nil, slack.NewAccessory(voteBtnEle), nil)
223223
224224 // Option One Votes
225225 optOneVoteText := slack.NewTextBlockObject("plain_text", "3 votes", true, false)
227227
228228 // Option Two Info
229229 optTwoText := slack.NewTextBlockObject("mrkdwn", ":hamburger: *Super Hungryman Hamburgers*\nOnly for the hungriest of the hungry.", false, false)
230 optTwoSection := slack.NewSectionBlock("", optTwoText, nil, slack.NewAccessory(voteBtnEle))
230 optTwoSection := slack.NewSectionBlock(optTwoText, nil, slack.NewAccessory(voteBtnEle), nil)
231231
232232 // Option Two Votes
233233 optTwoVoteText := slack.NewTextBlockObject("plain_text", "2 votes", true, false)
235235
236236 // Option Three Info
237237 optThreeText := slack.NewTextBlockObject("mrkdwn", ":ramen: *Kagawa-Ya Udon Noodle Shop*\nDo you like to shop for noodles? We have noodles.", false, false)
238 optThreeSection := slack.NewSectionBlock("", optThreeText, nil, slack.NewAccessory(voteBtnEle))
238 optThreeSection := slack.NewSectionBlock(optThreeText, nil, slack.NewAccessory(voteBtnEle), nil)
239239
240240 // Option Three Votes
241241 optThreeVoteText := slack.NewTextBlockObject("plain_text", "No votes", true, false)
290290 overflow := slack.NewOverflowBlockElement("", overflowOptionOne, overflowOptionTwo, overflowOptionThree)
291291
292292 // Create the header section
293 headerSection := slack.NewSectionBlock("", headerText, nil, slack.NewAccessory(overflow))
293 headerSection := slack.NewSectionBlock(headerText, nil, slack.NewAccessory(overflow), nil)
294294
295295 // Shared Divider
296296 divSection := slack.NewDividerBlock()
303303 hotelOneImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/tripAgent_1.png", "Windsor Court Hotel thumbnail")
304304 hotelOneLoc := slack.NewTextBlockObject("plain_text", "Location: Central Business District", true, false)
305305
306 hotelOneSection := slack.NewSectionBlock("", hotelOneInfo, nil, slack.NewAccessory(hotelOneImage))
306 hotelOneSection := slack.NewSectionBlock(hotelOneInfo, nil, slack.NewAccessory(hotelOneImage), nil)
307307 hotelOneContext := slack.NewContextBlock("", []slack.MixedElement{locationPinImage, hotelOneLoc}...)
308308
309309 // Second Hotel Listing
311311 hotelTwoImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/tripAgent_2.png", "Ritz-Carlton New Orleans thumbnail")
312312 hotelTwoLoc := slack.NewTextBlockObject("plain_text", "Location: French Quarter", true, false)
313313
314 hotelTwoSection := slack.NewSectionBlock("", hotelTwoInfo, nil, slack.NewAccessory(hotelTwoImage))
314 hotelTwoSection := slack.NewSectionBlock(hotelTwoInfo, nil, slack.NewAccessory(hotelTwoImage), nil)
315315 hotelTwoContext := slack.NewContextBlock("", []slack.MixedElement{locationPinImage, hotelTwoLoc}...)
316316
317317 // Third Hotel Listing
319319 hotelThreeImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/tripAgent_3.png", "https://api.slack.com/img/blocks/bkb_template_images/tripAgent_3.png")
320320 hotelThreeLoc := slack.NewTextBlockObject("plain_text", "Location: French Quarter", true, false)
321321
322 hotelThreeSection := slack.NewSectionBlock("", hotelThreeInfo, nil, slack.NewAccessory(hotelThreeImage))
322 hotelThreeSection := slack.NewSectionBlock(hotelThreeInfo, nil, slack.NewAccessory(hotelThreeImage), nil)
323323 hotelThreeContext := slack.NewContextBlock("", []slack.MixedElement{locationPinImage, hotelThreeLoc}...)
324324
325325 // Action button
372372
373373 // Header Section
374374 headerText := slack.NewTextBlockObject("mrkdwn", ":mag: Search results for *Cata*", false, false)
375 headerSection := slack.NewSectionBlock("", headerText, nil, nil)
375 headerSection := slack.NewSectionBlock(headerText, nil, nil, nil)
376376
377377 // Result One
378378 resultOneTxt := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toYourApp.com|Use Case Catalogue>*\nUse Case Catalogue for the following departments/roles...", false, false)
379 resultOneSection := slack.NewSectionBlock("", resultOneTxt, nil, slack.NewAccessory(availableOption))
379 resultOneSection := slack.NewSectionBlock(resultOneTxt, nil, slack.NewAccessory(availableOption), nil)
380380
381381 // Result Two
382382 resultTwoTxt := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toYourApp.com|Customer Support - Workflow Diagram Catalogue>*\nThis resource was put together by members of...", false, false)
383 resultTwoSection := slack.NewSectionBlock("", resultTwoTxt, nil, slack.NewAccessory(availableOption))
383 resultTwoSection := slack.NewSectionBlock(resultTwoTxt, nil, slack.NewAccessory(availableOption), nil)
384384
385385 // Result Three
386386 resultThreeTxt := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toYourApp.com|Self-Serve Learning Options Catalogue>*\nSee the learning and development options we...", false, false)
387 resultThreeSection := slack.NewSectionBlock("", resultThreeTxt, nil, slack.NewAccessory(availableOption))
387 resultThreeSection := slack.NewSectionBlock(resultThreeTxt, nil, slack.NewAccessory(availableOption), nil)
388388
389389 // Result Four
390390 resultFourTxt := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toYourApp.com|Use Case Catalogue - CF Presentation - [June 12, 2018]>*\nThis is presentation will continue to be updated as...", false, false)
391 resultFourSection := slack.NewSectionBlock("", resultFourTxt, nil, slack.NewAccessory(availableOption))
391 resultFourSection := slack.NewSectionBlock(resultFourTxt, nil, slack.NewAccessory(availableOption), nil)
392392
393393 // Result Five
394394 resultFiveTxt := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toYourApp.com|Comprehensive Benefits Catalogue - 2019>*\nInformation about all the benfits we offer is...", false, false)
395 resultFiveSection := slack.NewSectionBlock("", resultFiveTxt, nil, slack.NewAccessory(availableOption))
395 resultFiveSection := slack.NewSectionBlock(resultFiveTxt, nil, slack.NewAccessory(availableOption), nil)
396396
397397 // Next Results Button
398398 // Suggestions Action
448448 // Append SectionBlock for marshalling
449449 approvalText := slack.NewTextBlockObject("mrkdwn", "*Type:*\nPaid time off\n*When:*\nAug 10-Aug 13\n*Hours:* 16.0 (2 days)\n*Remaining balance:* 32.0 hours (4 days)\n*Comments:* \"Family in town, going camping!\"", false, false)
450450 approvalImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/approvalsNewDevice.png", "computer thumbnail")
451 msgBlocks = append(msgBlocks, slack.NewSectionBlock("", approvalText, nil, slack.NewAccessory(approvalImage)))
451 msgBlocks = append(msgBlocks, slack.NewSectionBlock(approvalText, nil, slack.NewAccessory(approvalImage)), nil)
452452
453453 // Build Message with blocks created above
454454 msg := slack.NewBlockMessage(msgBlocks...)