diff --git a/comms/sms.go b/comms/sms.go index 1a5ad94a..cd8a4acc 100644 --- a/comms/sms.go +++ b/comms/sms.go @@ -14,10 +14,38 @@ import ( var VOIP_MS_API = "https://voip.ms/api/v1/rest.php" -type SendSMSResponse struct { +type VoipMSResponse struct { Status string `json:"status"` - SMS int `json:"sms"` + SMS int `json:"sms"` } + +func SendMMS(to string, content string, media ...string) error { + if len(content) > 2048 { + return errors.New("Message content is more than 160 characters") + } + params := url.Values{} + params.Add("api_password", config.VoipMSPassword) + params.Add("api_username", config.VoipMSUsername) + params.Add("method", "sendMMS") + params.Add("did", config.VoipMSNumber) + params.Add("dst", to) + params.Add("message", content) + for i, med := range media { + // These should be one of: + // 1. A full URL that the service cat GET + // 2. A base64-encoded image starting with "data:image/png;base64,iVBORw0KGgoAAAANSUh..." + params.Add(fmt.Sprintf("media%d", i+1), med) + } + params.Add(fmt.Sprintf("media%d", len(media)+1), "") + + response, err := makeVoipMSRequest(params) + if err != nil { + return fmt.Errorf("Failed to send MMS: %w", err) + } + log.Info().Str("status", response.Status).Int("sms", response.SMS).Msg("Sent MMS message") + return nil +} + func SendSMS(to string, content string) error { if len(content) > 160 { return errors.New("Message content is more than 160 characters") @@ -29,6 +57,17 @@ func SendSMS(to string, content string) error { params.Add("did", config.VoipMSNumber) params.Add("dst", to) params.Add("message", content) + + response, err := makeVoipMSRequest(params) + if err != nil { + return fmt.Errorf("Failed to send SMS: %w", err) + } + log.Info().Str("status", response.Status).Int("sms", response.SMS).Msg("Sent MMS message") + return nil +} + +func makeVoipMSRequest(params url.Values) (VoipMSResponse, error) { + result := VoipMSResponse{} // Construct the URL with query parameters full_url := VOIP_MS_API + "?" + params.Encode() @@ -36,7 +75,7 @@ func SendSMS(to string, content string) error { resp, err := http.Get(full_url) if err != nil { log.Warn().Err(err).Str("url", full_url).Msg("Failed to make request to Voip.MS") - return fmt.Errorf("Error making request: %w", err) + return result, fmt.Errorf("Error making request: %w", err) } defer resp.Body.Close() @@ -44,15 +83,15 @@ func SendSMS(to string, content string) error { body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Warn().Err(err).Str("url", full_url).Msg("Failed to read Voip.MS response body") - return fmt.Errorf("Failed to read response: %w", err) + return result, fmt.Errorf("Failed to read response: %w", err) } log.Info().Str("response", string(body)).Msg("Response from Voip.MS") // Parse the JSON response - var response SendSMSResponse + var response VoipMSResponse err = json.Unmarshal(body, &response) if err != nil { - return fmt.Errorf("Failed to unmarshal JSON response: %w", err) + return result, fmt.Errorf("Failed to unmarshal JSON response: %w", err) } - return nil + return response, nil } diff --git a/public-report/quick.go b/public-report/quick.go index 78cb5d9f..eadda73e 100644 --- a/public-report/quick.go +++ b/public-report/quick.go @@ -190,7 +190,8 @@ func postRegisterNotifications(w http.ResponseWriter, r *http.Request) { }) } if phone != "" { - err := comms.SendSMS(phone, "testing 1 2 3") + //err := comms.SendSMS(phone, "testing 1 2 3") + err := comms.SendMMS(phone, "Check out my Nidus logo", "https://dev-sync.nidus.cloud/static/img/nidus-logo-no-lettering-64.png") if err != nil { log.Error().Err(err).Msg("Failed to send SMS") }