| 177 | } |
| 178 | |
| 179 | func postMessage(context webhook.Context) error { |
| 180 | post := Webhook{ |
| 181 | MessageType: "interactive", |
| 182 | Card: getMessageCard(context), |
| 183 | } |
| 184 | body, err := json.Marshal(post) |
| 185 | if err != nil { |
| 186 | return errors.Wrapf(err, "failed to marshal webhook POST request to %s", context.URL) |
| 187 | } |
| 188 | req, err := http.NewRequest("POST", |
| 189 | context.URL, bytes.NewBuffer(body)) |
| 190 | if err != nil { |
| 191 | return errors.Wrapf(err, "failed to construct webhook POST request to %s", context.URL) |
| 192 | } |
| 193 | |
| 194 | req.Header.Set("Content-Type", "application/json") |
| 195 | client := &http.Client{ |
| 196 | Timeout: webhook.Timeout, |
| 197 | } |
| 198 | resp, err := client.Do(req) |
| 199 | if err != nil { |
| 200 | return errors.Wrapf(err, "failed to POST webhook to %s", context.URL) |
| 201 | } |
| 202 | |
| 203 | b, err := io.ReadAll(resp.Body) |
| 204 | if err != nil { |
| 205 | return errors.Wrapf(err, "failed to read POST webhook response from %s", context.URL) |
| 206 | } |
| 207 | defer resp.Body.Close() |
| 208 | |
| 209 | if resp.StatusCode != http.StatusOK { |
| 210 | return errors.Errorf("failed to POST webhook %s, status code: %d, response body: %s", context.URL, resp.StatusCode, b) |
| 211 | } |
| 212 | |
| 213 | webhookResponse := &WebhookResponse{} |
| 214 | if err := json.Unmarshal(b, webhookResponse); err != nil { |
| 215 | return errors.Wrapf(err, "malformed webhook response from %s", context.URL) |
| 216 | } |
| 217 | |
| 218 | if webhookResponse.Code != 0 { |
| 219 | return errors.Errorf("%s", webhookResponse.Message) |
| 220 | } |
| 221 | |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | func getMessageCard(context webhook.Context) *WebhookCard { |
| 226 | var markdownBuf strings.Builder |