| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | |
| 7 | "github.com/slack-go/slack" |
| 8 | ) |
| 9 | |
| 10 | // The functions below mock the different templates slack has as examples on their website. |
| 11 | // |
| 12 | // Refer to README.md for more information on the examples and how to use them. |
| 13 | |
| 14 | func main() { |
| 15 | |
| 16 | fmt.Println("--- Begin Example One ---") |
| 17 | exampleOne() |
| 18 | fmt.Println("--- End Example One ---") |
| 19 | |
| 20 | fmt.Println("--- Begin Example Two ---") |
| 21 | exampleTwo() |
| 22 | fmt.Println("--- End Example Two ---") |
| 23 | |
| 24 | fmt.Println("--- Begin Example Three ---") |
| 25 | exampleThree() |
| 26 | fmt.Println("--- End Example Three ---") |
| 27 | |
| 28 | fmt.Println("--- Begin Example Four ---") |
| 29 | exampleFour() |
| 30 | fmt.Println("--- End Example Four ---") |
| 31 | |
| 32 | fmt.Println("--- Begin Example Five ---") |
| 33 | exampleFive() |
| 34 | fmt.Println("--- End Example Five ---") |
| 35 | |
| 36 | fmt.Println("--- Begin Example Six ---") |
| 37 | exampleSix() |
| 38 | fmt.Println("--- End Example Six ---") |
| 39 | |
| 40 | fmt.Println("--- Begin Example Unmarshalling ---") |
| 41 | unmarshalExample() |
| 42 | fmt.Println("--- End Example Unmarshalling ---") |
| 43 | } |
| 44 | |
| 45 | // approvalRequest mocks the simple "Approval" template located on block kit builder website |
| 46 | func exampleOne() { |
| 47 | |
| 48 | // Header Section |
| 49 | headerText := slack.NewTextBlockObject("mrkdwn", "You have a new request:\n*<fakeLink.toEmployeeProfile.com|Fred Enriquez - New device request>*", false, false) |
| 50 | headerSection := slack.NewSectionBlock(headerText, nil, nil) |
| 51 | |
| 52 | // Fields |
| 53 | typeField := slack.NewTextBlockObject("mrkdwn", "*Type:*\nComputer (laptop)", false, false) |
| 54 | whenField := slack.NewTextBlockObject("mrkdwn", "*When:*\nSubmitted Aut 10", false, false) |
| 55 | lastUpdateField := slack.NewTextBlockObject("mrkdwn", "*Last Update:*\nMar 10, 2015 (3 years, 5 months)", false, false) |
| 56 | reasonField := slack.NewTextBlockObject("mrkdwn", "*Reason:*\nAll vowel keys aren't working.", false, false) |
| 57 | specsField := slack.NewTextBlockObject("mrkdwn", "*Specs:*\n\"Cheetah Pro 15\" - Fast, really fast\"", false, false) |
| 58 | |
| 59 | fieldSlice := make([]*slack.TextBlockObject, 0) |
| 60 | fieldSlice = append(fieldSlice, typeField) |
| 61 | fieldSlice = append(fieldSlice, whenField) |
| 62 | fieldSlice = append(fieldSlice, lastUpdateField) |
| 63 | fieldSlice = append(fieldSlice, reasonField) |
| 64 | fieldSlice = append(fieldSlice, specsField) |
| 65 | |
| 66 | fieldsSection := slack.NewSectionBlock(nil, fieldSlice, nil) |
| 67 | |
| 68 | // Approve and Deny Buttons |
| 69 | approveBtnTxt := slack.NewTextBlockObject("plain_text", "Approve", false, false) |
| 70 | approveBtn := slack.NewButtonBlockElement("", "click_me_123", approveBtnTxt) |
| 71 | |
| 72 | denyBtnTxt := slack.NewTextBlockObject("plain_text", "Deny", false, false) |
| 73 | denyBtn := slack.NewButtonBlockElement("", "click_me_123", denyBtnTxt) |
| 74 | |
| 75 | actionBlock := slack.NewActionBlock("", approveBtn, denyBtn) |
| 76 | |
| 77 | // Build Message with blocks created above |
| 78 | |
| 79 | msg := slack.NewBlockMessage( |
| 80 | headerSection, |
| 81 | fieldsSection, |
| 82 | actionBlock, |
| 83 | ) |
| 84 | |
| 85 | b, err := json.MarshalIndent(msg, "", " ") |
| 86 | if err != nil { |
| 87 | fmt.Println(err) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | fmt.Println(string(b)) |
| 92 | |
| 93 | } |
| 94 | |
| 95 | // exampleTwo mocks the more complex "Approval" template located on block kit builder website |
| 96 | // which includes an accessory image next to the approval request |
| 97 | func exampleTwo() { |
| 98 | |
| 99 | // Header Section |
| 100 | headerText := slack.NewTextBlockObject("mrkdwn", "You have a new request:\n*<google.com|Fred Enriquez - Time Off request>*", false, false) |
| 101 | headerSection := slack.NewSectionBlock(headerText, nil, nil) |
| 102 | |
| 103 | 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) |
| 104 | approvalImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/approvalsNewDevice.png", "computer thumbnail") |
| 105 | |
| 106 | fieldsSection := slack.NewSectionBlock(approvalText, nil, slack.NewAccessory(approvalImage)) |
| 107 | |
| 108 | // Approve and Deny Buttons |
| 109 | approveBtnTxt := slack.NewTextBlockObject("plain_text", "Approve", false, false) |
| 110 | approveBtn := slack.NewButtonBlockElement("", "click_me_123", approveBtnTxt) |
| 111 | |
| 112 | denyBtnTxt := slack.NewTextBlockObject("plain_text", "Deny", false, false) |
| 113 | denyBtn := slack.NewButtonBlockElement("", "click_me_123", denyBtnTxt) |
| 114 | |
| 115 | actionBlock := slack.NewActionBlock("", approveBtn, denyBtn) |
| 116 | |
| 117 | // Build Message with blocks created above |
| 118 | |
| 119 | msg := slack.NewBlockMessage( |
| 120 | headerSection, |
| 121 | fieldsSection, |
| 122 | actionBlock, |
| 123 | ) |
| 124 | |
| 125 | b, err := json.MarshalIndent(msg, "", " ") |
| 126 | if err != nil { |
| 127 | fmt.Println(err) |
| 128 | return |
| 129 | } |
| 130 | |
| 131 | fmt.Println(string(b)) |
| 132 | |
| 133 | } |
| 134 | |
| 135 | // exampleThree generates the notification example from the block kit builder website |
| 136 | func exampleThree() { |
| 137 | |
| 138 | // Shared Assets for example |
| 139 | chooseBtnText := slack.NewTextBlockObject("plain_text", "Choose", true, false) |
| 140 | chooseBtnEle := slack.NewButtonBlockElement("", "click_me_123", chooseBtnText) |
| 141 | divSection := slack.NewDividerBlock() |
| 142 | |
| 143 | // Header Section |
| 144 | headerText := slack.NewTextBlockObject("plain_text", "Looks like you have a scheduling conflict with this event:", false, false) |
| 145 | headerSection := slack.NewSectionBlock(headerText, nil, nil) |
| 146 | |
| 147 | // Schedule Info Section |
| 148 | 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) |
| 149 | scheduleAccessory := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/notifications.png", "calendar thumbnail") |
| 150 | schedeuleSection := slack.NewSectionBlock(scheduleText, nil, slack.NewAccessory(scheduleAccessory)) |
| 151 | |
| 152 | // Conflict Section |
| 153 | conflictImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/notificationsWarningIcon.png", "notifications warning icon") |
| 154 | conflictText := slack.NewTextBlockObject("mrkdwn", "*Conflicts with Team Huddle: 4:15-4:30pm*", false, false) |
| 155 | |
| 156 | conflictSection := slack.NewContextBlock( |
| 157 | "", |
| 158 | []slack.MixedElement{conflictImage, conflictText}..., |
| 159 | ) |
| 160 | |
| 161 | // Proposese Text |
| 162 | proposeText := slack.NewTextBlockObject("mrkdwn", "*Propose a new time:*", false, false) |
| 163 | proposeSection := slack.NewSectionBlock(proposeText, nil, nil) |
| 164 | |
| 165 | // Option 1 |
| 166 | optionOneText := slack.NewTextBlockObject("mrkdwn", "*Today - 4:30-5pm*\nEveryone is available: @iris, @zelda", false, false) |
| 167 | optionOneSection := slack.NewSectionBlock(optionOneText, nil, slack.NewAccessory(chooseBtnEle)) |
| 168 | |
| 169 | // Option 2 |
| 170 | optionTwoText := slack.NewTextBlockObject("mrkdwn", "*Tomorrow - 4-4:30pm*\nEveryone is available: @iris, @zelda", false, false) |
| 171 | optionTwoSection := slack.NewSectionBlock(optionTwoText, nil, slack.NewAccessory(chooseBtnEle)) |
| 172 | |
| 173 | // Option 3 |
| 174 | optionThreeText := slack.NewTextBlockObject("mrkdwn", "*Tomorrow - 6-6:30pm*\nSome people aren't available: @iris, ~@zelda~", false, false) |
| 175 | optionThreeSection := slack.NewSectionBlock(optionThreeText, nil, slack.NewAccessory(chooseBtnEle)) |
| 176 | |
| 177 | // Show More Times Link |
| 178 | showMoreText := slack.NewTextBlockObject("mrkdwn", "*<fakelink.ToMoreTimes.com|Show more times>*", false, false) |
| 179 | showMoreSection := slack.NewSectionBlock(showMoreText, nil, nil) |
| 180 | |
| 181 | // Build Message with blocks created above |
| 182 | msg := slack.NewBlockMessage( |
| 183 | headerSection, |
| 184 | divSection, |
| 185 | schedeuleSection, |
| 186 | conflictSection, |
| 187 | divSection, |
| 188 | proposeSection, |
| 189 | optionOneSection, |
| 190 | optionTwoSection, |
| 191 | optionThreeSection, |
| 192 | showMoreSection, |
| 193 | ) |
| 194 | |
| 195 | b, err := json.MarshalIndent(msg, "", " ") |
| 196 | if err != nil { |
| 197 | fmt.Println(err) |
| 198 | return |
| 199 | } |
| 200 | |
| 201 | fmt.Println(string(b)) |
| 202 | |
| 203 | } |
| 204 | |
| 205 | // exampleFour profiles a poll example block |
| 206 | func exampleFour() { |
| 207 | |
| 208 | // Shared Assets for example |
| 209 | divSection := slack.NewDividerBlock() |
| 210 | voteBtnText := slack.NewTextBlockObject("plain_text", "Vote", true, false) |
| 211 | voteBtnEle := slack.NewButtonBlockElement("", "click_me_123", voteBtnText) |
| 212 | profileOne := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/profile_1.png", "Michael Scott") |
| 213 | profileTwo := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/profile_2.png", "Dwight Schrute") |
| 214 | profileThree := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/profile_3.png", "Pam Beasely") |
| 215 | profileFour := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/profile_4.png", "Angela") |
| 216 | |
| 217 | // Header Section |
| 218 | headerText := slack.NewTextBlockObject("mrkdwn", "*Where should we order lunch from?* Poll by <fakeLink.toUser.com|Mark>", false, false) |
| 219 | headerSection := slack.NewSectionBlock(headerText, nil, nil) |
| 220 | |
| 221 | // Option One Info |
| 222 | optOneText := slack.NewTextBlockObject("mrkdwn", ":sushi: *Ace Wasabi Rock-n-Roll Sushi Bar*\nThe best landlocked sushi restaurant.", false, false) |
| 223 | optOneSection := slack.NewSectionBlock(optOneText, nil, slack.NewAccessory(voteBtnEle)) |
| 224 | |
| 225 | // Option One Votes |
| 226 | optOneVoteText := slack.NewTextBlockObject("plain_text", "3 votes", true, false) |
| 227 | optOneContext := slack.NewContextBlock("", []slack.MixedElement{profileOne, profileTwo, profileThree, optOneVoteText}...) |
| 228 | |
| 229 | // Option Two Info |
| 230 | optTwoText := slack.NewTextBlockObject("mrkdwn", ":hamburger: *Super Hungryman Hamburgers*\nOnly for the hungriest of the hungry.", false, false) |
| 231 | optTwoSection := slack.NewSectionBlock(optTwoText, nil, slack.NewAccessory(voteBtnEle)) |
| 232 | |
| 233 | // Option Two Votes |
| 234 | optTwoVoteText := slack.NewTextBlockObject("plain_text", "2 votes", true, false) |
| 235 | optTwoContext := slack.NewContextBlock("", []slack.MixedElement{profileFour, profileTwo, optTwoVoteText}...) |
| 236 | |
| 237 | // Option Three Info |
| 238 | optThreeText := slack.NewTextBlockObject("mrkdwn", ":ramen: *Kagawa-Ya Udon Noodle Shop*\nDo you like to shop for noodles? We have noodles.", false, false) |
| 239 | optThreeSection := slack.NewSectionBlock(optThreeText, nil, slack.NewAccessory(voteBtnEle)) |
| 240 | |
| 241 | // Option Three Votes |
| 242 | optThreeVoteText := slack.NewTextBlockObject("plain_text", "No votes", true, false) |
| 243 | optThreeContext := slack.NewContextBlock("", []slack.MixedElement{optThreeVoteText}...) |
| 244 | |
| 245 | // Suggestions Action |
| 246 | btnTxt := slack.NewTextBlockObject("plain_text", "Add a suggestion", false, false) |
| 247 | nextBtn := slack.NewButtonBlockElement("", "click_me_123", btnTxt) |
| 248 | actionBlock := slack.NewActionBlock("", nextBtn) |
| 249 | |
| 250 | // Build Message with blocks created above |
| 251 | msg := slack.NewBlockMessage( |
| 252 | headerSection, |
| 253 | divSection, |
| 254 | optOneSection, |
| 255 | optOneContext, |
| 256 | optTwoSection, |
| 257 | optTwoContext, |
| 258 | optThreeSection, |
| 259 | optThreeContext, |
| 260 | divSection, |
| 261 | actionBlock, |
| 262 | ) |
| 263 | |
| 264 | b, err := json.MarshalIndent(msg, "", " ") |
| 265 | if err != nil { |
| 266 | fmt.Println(err) |
| 267 | return |
| 268 | } |
| 269 | |
| 270 | fmt.Println(string(b)) |
| 271 | |
| 272 | } |
| 273 | |
| 274 | func exampleFive() { |
| 275 | |
| 276 | // Build Header Section Block, includes text and overflow menu |
| 277 | |
| 278 | headerText := slack.NewTextBlockObject("mrkdwn", "We found *205 Hotels* in New Orleans, LA from *12/14 to 12/17*", false, false) |
| 279 | |
| 280 | // Build Text Objects associated with each option |
| 281 | overflowOptionTextOne := slack.NewTextBlockObject("plain_text", "Option One", false, false) |
| 282 | overflowOptionTextTwo := slack.NewTextBlockObject("plain_text", "Option Two", false, false) |
| 283 | overflowOptionTextThree := slack.NewTextBlockObject("plain_text", "Option Three", false, false) |
| 284 | |
| 285 | // Build each option, providing a value for the option |
| 286 | overflowOptionOne := slack.NewOptionBlockObject("value-0", overflowOptionTextOne, nil) |
| 287 | overflowOptionTwo := slack.NewOptionBlockObject("value-1", overflowOptionTextTwo, nil) |
| 288 | overflowOptionThree := slack.NewOptionBlockObject("value-2", overflowOptionTextThree, nil) |
| 289 | |
| 290 | // Build overflow section |
| 291 | overflow := slack.NewOverflowBlockElement("", overflowOptionOne, overflowOptionTwo, overflowOptionThree) |
| 292 | |
| 293 | // Create the header section |
| 294 | headerSection := slack.NewSectionBlock(headerText, nil, slack.NewAccessory(overflow)) |
| 295 | |
| 296 | // Shared Divider |
| 297 | divSection := slack.NewDividerBlock() |
| 298 | |
| 299 | // Shared Objects |
| 300 | locationPinImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/tripAgentLocationMarker.png", "Location Pin Icon") |
| 301 | |
| 302 | // First Hotel Listing |
| 303 | hotelOneInfo := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toHotelPage.com|Windsor Court Hotel>*\n★★★★★\n$340 per night\nRated: 9.4 - Excellent", false, false) |
| 304 | hotelOneImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/tripAgent_1.png", "Windsor Court Hotel thumbnail") |
| 305 | hotelOneLoc := slack.NewTextBlockObject("plain_text", "Location: Central Business District", true, false) |
| 306 | |
| 307 | hotelOneSection := slack.NewSectionBlock(hotelOneInfo, nil, slack.NewAccessory(hotelOneImage)) |
| 308 | hotelOneContext := slack.NewContextBlock("", []slack.MixedElement{locationPinImage, hotelOneLoc}...) |
| 309 | |
| 310 | // Second Hotel Listing |
| 311 | hotelTwoInfo := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toHotelPage.com|The Ritz-Carlton New Orleans>*\n★★★★★\n$340 per night\nRated: 9.1 - Excellent", false, false) |
| 312 | hotelTwoImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/tripAgent_2.png", "Ritz-Carlton New Orleans thumbnail") |
| 313 | hotelTwoLoc := slack.NewTextBlockObject("plain_text", "Location: French Quarter", true, false) |
| 314 | |
| 315 | hotelTwoSection := slack.NewSectionBlock(hotelTwoInfo, nil, slack.NewAccessory(hotelTwoImage)) |
| 316 | hotelTwoContext := slack.NewContextBlock("", []slack.MixedElement{locationPinImage, hotelTwoLoc}...) |
| 317 | |
| 318 | // Third Hotel Listing |
| 319 | hotelThreeInfo := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toHotelPage.com|Omni Royal Orleans Hotel>*\n★★★★★\n$419 per night\nRated: 8.8 - Excellent", false, false) |
| 320 | 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") |
| 321 | hotelThreeLoc := slack.NewTextBlockObject("plain_text", "Location: French Quarter", true, false) |
| 322 | |
| 323 | hotelThreeSection := slack.NewSectionBlock(hotelThreeInfo, nil, slack.NewAccessory(hotelThreeImage)) |
| 324 | hotelThreeContext := slack.NewContextBlock("", []slack.MixedElement{locationPinImage, hotelThreeLoc}...) |
| 325 | |
| 326 | // Action button |
| 327 | btnTxt := slack.NewTextBlockObject("plain_text", "Next 2 Results", false, false) |
| 328 | nextBtn := slack.NewButtonBlockElement("", "click_me_123", btnTxt) |
| 329 | actionBlock := slack.NewActionBlock("", nextBtn) |
| 330 | |
| 331 | // Build Message with blocks created above |
| 332 | msg := slack.NewBlockMessage( |
| 333 | headerSection, |
| 334 | divSection, |
| 335 | hotelOneSection, |
| 336 | hotelOneContext, |
| 337 | divSection, |
| 338 | hotelTwoSection, |
| 339 | hotelTwoContext, |
| 340 | divSection, |
| 341 | hotelThreeSection, |
| 342 | hotelThreeContext, |
| 343 | divSection, |
| 344 | actionBlock, |
| 345 | ) |
| 346 | |
| 347 | b, err := json.MarshalIndent(msg, "", " ") |
| 348 | if err != nil { |
| 349 | fmt.Println(err) |
| 350 | return |
| 351 | } |
| 352 | |
| 353 | fmt.Println(string(b)) |
| 354 | |
| 355 | } |
| 356 | |
| 357 | func exampleSix() { |
| 358 | |
| 359 | // Shared Assets for example |
| 360 | divSection := slack.NewDividerBlock() |
| 361 | |
| 362 | // Shared Available Options |
| 363 | manageTxt := slack.NewTextBlockObject("plain_text", "Manage", true, false) |
| 364 | editTxt := slack.NewTextBlockObject("plain_text", "Edit it", false, false) |
| 365 | readTxt := slack.NewTextBlockObject("plain_text", "Read it", false, false) |
| 366 | saveTxt := slack.NewTextBlockObject("plain_text", "Save it", false, false) |
| 367 | |
| 368 | editOpt := slack.NewOptionBlockObject("value-0", editTxt, nil) |
| 369 | readOpt := slack.NewOptionBlockObject("value-1", readTxt, nil) |
| 370 | saveOpt := slack.NewOptionBlockObject("value-2", saveTxt, nil) |
| 371 | |
| 372 | availableOption := slack.NewOptionsSelectBlockElement("static_select", manageTxt, "", editOpt, readOpt, saveOpt) |
| 373 | |
| 374 | // Header Section |
| 375 | headerText := slack.NewTextBlockObject("mrkdwn", ":mag: Search results for *Cata*", false, false) |
| 376 | headerSection := slack.NewSectionBlock(headerText, nil, nil) |
| 377 | |
| 378 | // Result One |
| 379 | resultOneTxt := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toYourApp.com|Use Case Catalogue>*\nUse Case Catalogue for the following departments/roles...", false, false) |
| 380 | resultOneSection := slack.NewSectionBlock(resultOneTxt, nil, slack.NewAccessory(availableOption)) |
| 381 | |
| 382 | // Result Two |
| 383 | resultTwoTxt := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toYourApp.com|Customer Support - Workflow Diagram Catalogue>*\nThis resource was put together by members of...", false, false) |
| 384 | resultTwoSection := slack.NewSectionBlock(resultTwoTxt, nil, slack.NewAccessory(availableOption)) |
| 385 | |
| 386 | // Result Three |
| 387 | resultThreeTxt := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toYourApp.com|Self-Serve Learning Options Catalogue>*\nSee the learning and development options we...", false, false) |
| 388 | resultThreeSection := slack.NewSectionBlock(resultThreeTxt, nil, slack.NewAccessory(availableOption)) |
| 389 | |
| 390 | // Result Four |
| 391 | 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) |
| 392 | resultFourSection := slack.NewSectionBlock(resultFourTxt, nil, slack.NewAccessory(availableOption)) |
| 393 | |
| 394 | // Result Five |
| 395 | resultFiveTxt := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toYourApp.com|Comprehensive Benefits Catalogue - 2019>*\nInformation about all the benfits we offer is...", false, false) |
| 396 | resultFiveSection := slack.NewSectionBlock(resultFiveTxt, nil, slack.NewAccessory(availableOption)) |
| 397 | |
| 398 | // Next Results Button |
| 399 | // Suggestions Action |
| 400 | btnTxt := slack.NewTextBlockObject("plain_text", "Next 5 Results", false, false) |
| 401 | nextBtn := slack.NewButtonBlockElement("", "click_me_123", btnTxt) |
| 402 | actionBlock := slack.NewActionBlock("", nextBtn) |
| 403 | |
| 404 | // Build Message with blocks created above |
| 405 | msg := slack.NewBlockMessage( |
| 406 | headerSection, |
| 407 | divSection, |
| 408 | resultOneSection, |
| 409 | resultTwoSection, |
| 410 | resultThreeSection, |
| 411 | resultFourSection, |
| 412 | resultFiveSection, |
| 413 | divSection, |
| 414 | actionBlock, |
| 415 | ) |
| 416 | |
| 417 | b, err := json.MarshalIndent(msg, "", " ") |
| 418 | if err != nil { |
| 419 | fmt.Println(err) |
| 420 | return |
| 421 | } |
| 422 | |
| 423 | fmt.Println(string(b)) |
| 424 | |
| 425 | } |
| 426 | |
| 427 | func unmarshalExample() { |
| 428 | var msgBlocks []slack.Block |
| 429 | |
| 430 | // Append ActionBlock for marshalling |
| 431 | btnTxt := slack.NewTextBlockObject("plain_text", "Add a suggestion", false, false) |
| 432 | nextBtn := slack.NewButtonBlockElement("", "click_me_123", btnTxt) |
| 433 | approveBtnTxt := slack.NewTextBlockObject("plain_text", "Approve", false, false) |
| 434 | approveBtn := slack.NewButtonBlockElement("", "click_me_123", approveBtnTxt) |
| 435 | msgBlocks = append(msgBlocks, slack.NewActionBlock("", nextBtn, approveBtn)) |
| 436 | |
| 437 | // Append ContextBlock for marshalling |
| 438 | profileOne := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/profile_1.png", "Michael Scott") |
| 439 | profileTwo := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/profile_2.png", "Dwight Schrute") |
| 440 | textBlockObj := slack.NewTextBlockObject("mrkdwn", "*<fakeLink.toHotelPage.com|Omni Royal Orleans Hotel>*\n★★★★★\n$419 per night\nRated: 8.8 - Excellent", false, false) |
| 441 | msgBlocks = append(msgBlocks, slack.NewContextBlock("", []slack.MixedElement{profileOne, profileTwo, textBlockObj}...)) |
| 442 | |
| 443 | // Append ImageBlock for marshalling |
| 444 | msgBlocks = append(msgBlocks, slack.NewImageBlock("https://api.slack.com/img/blocks/bkb_template_images/profile_2.png", "some profile", "image-block", textBlockObj)) |
| 445 | |
| 446 | // Append DividerBlock for marshalling |
| 447 | msgBlocks = append(msgBlocks, slack.NewDividerBlock()) |
| 448 | |
| 449 | // Append SectionBlock for marshalling |
| 450 | 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) |
| 451 | approvalImage := slack.NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/approvalsNewDevice.png", "computer thumbnail") |
| 452 | msgBlocks = append(msgBlocks, slack.NewSectionBlock(approvalText, nil, slack.NewAccessory(approvalImage)), nil) |
| 453 | |
| 454 | // Build Message with blocks created above |
| 455 | msg := slack.NewBlockMessage(msgBlocks...) |
| 456 | |
| 457 | b, err := json.Marshal(&msg) |
| 458 | if err != nil { |
| 459 | fmt.Println(err) |
| 460 | return |
| 461 | } |
| 462 | |
| 463 | fmt.Println(string(b)) |
| 464 | |
| 465 | // Unmarshal message |
| 466 | m := slack.Message{} |
| 467 | if err := json.Unmarshal(b, &m); err != nil { |
| 468 | fmt.Println(err) |
| 469 | return |
| 470 | } |
| 471 | |
| 472 | var respBlocks []slack.Block |
| 473 | for _, block := range m.Blocks.BlockSet { |
| 474 | // Need to implement a type switch to determine Block type since the |
| 475 | // response from Slack could include any/all types under "blocks" key |
| 476 | switch block.BlockType() { |
| 477 | case slack.MBTContext: |
| 478 | var respMixedElements []slack.MixedElement |
| 479 | contextElements := block.(*slack.ContextBlock).ContextElements.Elements |
| 480 | // Need to implement a type switch for ContextElements for same reason as Blocks |
| 481 | for _, elem := range contextElements { |
| 482 | switch elem.MixedElementType() { |
| 483 | case slack.MixedElementImage: |
| 484 | // Assert the block's type to manipulate/extract values |
| 485 | imageBlockElem := elem.(*slack.ImageBlockElement) |
| 486 | imageBlockElem.ImageURL = "https://api.slack.com/img/blocks/bkb_template_images/profile_1.png" |
| 487 | imageBlockElem.AltText = "MichaelScott" |
| 488 | respMixedElements = append(respMixedElements, imageBlockElem) |
| 489 | case slack.MixedElementText: |
| 490 | textBlockElem := elem.(*slack.TextBlockObject) |
| 491 | textBlockElem.Text = "go go go go go" |
| 492 | respMixedElements = append(respMixedElements, textBlockElem) |
| 493 | } |
| 494 | } |
| 495 | respBlocks = append(respBlocks, slack.NewContextBlock("new block", respMixedElements...)) |
| 496 | case slack.MBTAction: |
| 497 | actionBlock := block.(*slack.ActionBlock) |
| 498 | // Need to implement a type switch for BlockElements for same reason as Blocks |
| 499 | for _, elem := range actionBlock.Elements.ElementSet { |
| 500 | switch elem.ElementType() { |
| 501 | case slack.METImage: |
| 502 | imageElem := elem.(*slack.ImageBlockElement) |
| 503 | fmt.Printf("do something with image block element: %v\n", imageElem) |
| 504 | case slack.METButton: |
| 505 | buttonElem := elem.(*slack.ButtonBlockElement) |
| 506 | fmt.Printf("do something with button block element: %v\n", buttonElem) |
| 507 | case slack.METOverflow: |
| 508 | overflowElem := elem.(*slack.OverflowBlockElement) |
| 509 | fmt.Printf("do something with overflow block element: %v\n", overflowElem) |
| 510 | case slack.METDatepicker: |
| 511 | datepickerElem := elem.(*slack.DatePickerBlockElement) |
| 512 | fmt.Printf("do something with datepicker block element: %v\n", datepickerElem) |
| 513 | case slack.METTimepicker: |
| 514 | timepickerElem := elem.(*slack.TimePickerBlockElement) |
| 515 | fmt.Printf("do something with timepicker block element: %v\n", timepickerElem) |
| 516 | } |
| 517 | } |
| 518 | respBlocks = append(respBlocks, block) |
| 519 | case slack.MBTImage: |
| 520 | // Simply re-append the block if you want to include it in the response |
| 521 | respBlocks = append(respBlocks, block) |
| 522 | case slack.MBTSection: |
| 523 | respBlocks = append(respBlocks, block) |
| 524 | case slack.MBTDivider: |
| 525 | respBlocks = append(respBlocks, block) |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // Build new Message with Blocks obtained/edited from callback |
| 530 | respMsg := slack.NewBlockMessage(respBlocks...) |
| 531 | |
| 532 | b, err = json.Marshal(&respMsg) |
| 533 | if err != nil { |
| 534 | fmt.Println(err) |
| 535 | return |
| 536 | } |
| 537 | |
| 538 | fmt.Println(string(b)) |
| 539 | } |