diff --git a/alerting/provider/messagebird/messagebird.go b/alerting/provider/messagebird/messagebird.go index 5c54def6..f2f665f2 100644 --- a/alerting/provider/messagebird/messagebird.go +++ b/alerting/provider/messagebird/messagebird.go @@ -2,6 +2,7 @@ package messagebird import ( "bytes" + "encoding/json" "fmt" "io" "net/http" @@ -33,7 +34,7 @@ func (provider *AlertProvider) IsValid() bool { // Send an alert using the provider // Reference doc for messagebird: https://developers.messagebird.com/api/sms-messaging/#send-outbound-sms func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) error { - buffer := bytes.NewBuffer([]byte(provider.buildRequestBody(endpoint, alert, result, resolved))) + buffer := bytes.NewBuffer(provider.buildRequestBody(endpoint, alert, result, resolved)) request, err := http.NewRequest(http.MethodPost, restAPIURL, buffer) if err != nil { return err @@ -51,19 +52,26 @@ func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, return err } +type Body struct { + Originator string `json:"originator"` + Recipients string `json:"recipients"` + Body string `json:"body"` +} + // buildRequestBody builds the request body for the provider -func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) string { +func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) []byte { var message string if resolved { message = fmt.Sprintf("RESOLVED: %s - %s", endpoint.DisplayName(), alert.GetDescription()) } else { message = fmt.Sprintf("TRIGGERED: %s - %s", endpoint.DisplayName(), alert.GetDescription()) } - return fmt.Sprintf(`{ - "originator": "%s", - "recipients": "%s", - "body": "%s" -}`, provider.Originator, provider.Recipients, message) + body, _ := json.Marshal(Body{ + Originator: provider.Originator, + Recipients: provider.Recipients, + Body: message, + }) + return body } // GetDefaultAlert returns the provider's default alert configuration diff --git a/alerting/provider/messagebird/messagebird_test.go b/alerting/provider/messagebird/messagebird_test.go index 2696fc02..584f5e3f 100644 --- a/alerting/provider/messagebird/messagebird_test.go +++ b/alerting/provider/messagebird/messagebird_test.go @@ -118,14 +118,14 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Provider: AlertProvider{AccessKey: "1", Originator: "2", Recipients: "3"}, Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: false, - ExpectedBody: "{\n \"originator\": \"2\",\n \"recipients\": \"3\",\n \"body\": \"TRIGGERED: endpoint-name - description-1\"\n}", + ExpectedBody: "{\"originator\":\"2\",\"recipients\":\"3\",\"body\":\"TRIGGERED: endpoint-name - description-1\"}", }, { Name: "resolved", Provider: AlertProvider{AccessKey: "4", Originator: "5", Recipients: "6"}, Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: true, - ExpectedBody: "{\n \"originator\": \"5\",\n \"recipients\": \"6\",\n \"body\": \"RESOLVED: endpoint-name - description-2\"\n}", + ExpectedBody: "{\"originator\":\"5\",\"recipients\":\"6\",\"body\":\"RESOLVED: endpoint-name - description-2\"}", }, } for _, scenario := range scenarios { @@ -141,7 +141,7 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { }, scenario.Resolved, ) - if body != scenario.ExpectedBody { + if string(body) != scenario.ExpectedBody { t.Errorf("expected %s, got %s", scenario.ExpectedBody, body) } out := make(map[string]interface{})