@@ -67,14 +67,34 @@ func main() {
6767 log .Fatalf ("Failed to get file info: %v" , err )
6868 }
6969
70+ // there is a bug where occasionally replacing the entire canvas doesn't work and we end up with multiple
71+ // images in the canvas. This is a workaround to delete all existing sections and replace the entire canvas.
72+ sections , err := api .LookupCanvasSections (slack.LookupCanvasSectionsParams {
73+ CanvasID : canvasId ,
74+ })
75+ if err != nil {
76+ log .Fatalf ("Failed to get canvas sections: %v" , err )
77+ }
78+
79+ var changes []slack.CanvasChange
80+
81+ if len (sections ) > 1 {
82+ log .Printf ("Detected multiple sections in canvas, deleting all sections" )
83+ for _ , section := range sections {
84+ changes = append (changes , slack.CanvasChange {
85+ Operation : "delete" ,
86+ SectionID : section .ID ,
87+ })
88+ }
89+ }
90+
7091 // edit canvas
7192 err = api .EditCanvas (slack.EditCanvasParams {
7293 CanvasID : canvasId ,
73- Changes : []slack.CanvasChange {
74- {
75- Operation : "replace" ,
76- SectionID : canvasSectionId , // ok if this is empty string, will replace the entire canvas
77- DocumentContent : slack.DocumentContent {
94+ Changes : append (changes , slack.CanvasChange {
95+ Operation : "replace" ,
96+ SectionID : canvasSectionId , // ok if this is empty string, will replace the entire canvas
97+ DocumentContent : slack.DocumentContent {
7898 Type : "markdown" ,
7999 Markdown : fmt .Sprintf (`
80100### Overview
@@ -83,9 +103,8 @@ func main() {
83103
84104---
85105` , fileSummary .ID , fileInfo .Permalink ),
86- },
87106 },
88- },
107+ }) ,
89108 })
90109 if err != nil {
91110 log .Fatalf ("Failed to edit canvas: %v" , err )
0 commit comments