|
7 | 7 | "io" |
8 | 8 | "net/http" |
9 | 9 | "net/url" |
| 10 | + "strings" |
10 | 11 |
|
11 | 12 | "github.com/gookit/color" |
12 | 13 | "github.com/innogames/slack-bot/v2/bot" |
@@ -80,32 +81,40 @@ func messageHandler(w http.ResponseWriter, r *http.Request, output io.Writer) { |
80 | 81 | var blocks []map[string]any |
81 | 82 | _ = json.Unmarshal([]byte(blockJSON), &blocks) |
82 | 83 |
|
| 84 | + var blockText strings.Builder |
83 | 85 | for _, block := range blocks { |
84 | | - text += formatBlock(block) + "\n" |
| 86 | + blockText.WriteString(formatBlock(block) + "\n") |
85 | 87 | } |
| 88 | + text += blockText.String() |
86 | 89 | } else if text == "" && query.Get("attachments") != "" { |
87 | 90 | attachmentJSON := query.Get("attachments") |
88 | 91 | var attachments []map[string]any |
89 | 92 | _ = json.Unmarshal([]byte(attachmentJSON), &attachments) |
90 | 93 |
|
| 94 | + var attachmentTitles strings.Builder |
| 95 | + var attachmentActions strings.Builder |
91 | 96 | for _, attachment := range attachments { |
92 | 97 | if txt, ok := attachment["title"].(string); ok { |
93 | | - text += txt + "\n" |
| 98 | + attachmentTitles.WriteString(txt + "\n") |
94 | 99 | } |
| 100 | + var actionText strings.Builder |
95 | 101 | for _, action := range attachment["actions"].([]any) { |
96 | 102 | actionMap := action.(map[string]any) |
97 | 103 |
|
98 | 104 | if actionMap["type"] == "button" { |
99 | | - text += fmt.Sprintf( |
| 105 | + fmt.Fprintf(&actionText, |
100 | 106 | "<%s|%s>\n", |
101 | 107 | actionMap["url"], |
102 | 108 | actionMap["text"], |
103 | 109 | ) |
104 | 110 | } else { |
105 | | - text += fmt.Sprintf("Attachment-actions are not supported yet:\n%v\n", action) |
| 111 | + fmt.Fprintf(&actionText, "Attachment-actions are not supported yet:\n%v\n", action) |
106 | 112 | } |
107 | 113 | } |
| 114 | + attachmentActions.WriteString(actionText.String()) |
108 | 115 | } |
| 116 | + text += attachmentActions.String() |
| 117 | + text += attachmentTitles.String() |
109 | 118 | } |
110 | 119 |
|
111 | 120 | _, _ = fmt.Fprint(output, formatSlackMessage(text)+"\n") |
@@ -139,11 +148,11 @@ func formatBlock(block map[string]any) string { |
139 | 148 | // bit hacky way to extract the text from some kind of block element |
140 | 149 | func extractText(block map[string]any) string { |
141 | 150 | if fields, ok := block["fields"]; ok { |
142 | | - result := "" |
| 151 | + var result strings.Builder |
143 | 152 | for _, field := range fields.([]any) { |
144 | | - result += extractText(field.(map[string]any)) |
| 153 | + result.WriteString(extractText(field.(map[string]any))) |
145 | 154 | } |
146 | | - return result |
| 155 | + return result.String() |
147 | 156 | } |
148 | 157 |
|
149 | 158 | // contains "text" element |
|
0 commit comments