From 30d3d95f90cc3942b9e5920b99f164810767c45f Mon Sep 17 00:00:00 2001 From: harshkumar8789 Date: Tue, 5 Dec 2023 15:56:09 +0530 Subject: [PATCH] check whether phone number contains country code or not. if not, added country code from the env of sms-service.added logger when sms is sent successfully. --- src/main/java/org/bahmni/sms/SMSProperties.java | 1 + src/main/java/org/bahmni/sms/impl/DefaultSmsSender.java | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/bahmni/sms/SMSProperties.java b/src/main/java/org/bahmni/sms/SMSProperties.java index a2b90b7..003cc38 100644 --- a/src/main/java/org/bahmni/sms/SMSProperties.java +++ b/src/main/java/org/bahmni/sms/SMSProperties.java @@ -17,6 +17,7 @@ public class SMSProperties { private String openmrsHost; private String openmrsPort; private String openmrsRootURL; + private String countryCode; public String getOpenmrsRootURL() { return "http://" + openmrsHost + ":" + openmrsPort + "/openmrs/ws/rest/v1"; diff --git a/src/main/java/org/bahmni/sms/impl/DefaultSmsSender.java b/src/main/java/org/bahmni/sms/impl/DefaultSmsSender.java index 3f13365..f13fd5f 100644 --- a/src/main/java/org/bahmni/sms/impl/DefaultSmsSender.java +++ b/src/main/java/org/bahmni/sms/impl/DefaultSmsSender.java @@ -38,8 +38,13 @@ public ResponseEntity send(String phoneNumber, String messageText) { Message message = new Message(); message.setChannel("sms"); message.setMsg_type("text"); + + if ((phoneNumber.length()==10 || phoneNumber.length()==11) && !phoneNumber.startsWith("+")) { + phoneNumber = smsProperties.getCountryCode() + phoneNumber; + } + String finalPhoneNumber = phoneNumber; message.setRecipients(new ArrayList() {{ - add(phoneNumber); + add(finalPhoneNumber); }}); message.setOriginator(smsProperties.getOriginator()); message.setContent(messageText); @@ -60,7 +65,9 @@ public ResponseEntity send(String phoneNumber, String messageText) { HttpResponse response = httpClient.execute(request); if(response.getStatusLine().getStatusCode()==HttpStatus.NOT_FOUND.value()){ logger.warn("Invalid API URL"); + return new ResponseEntity<>(response.getStatusLine().getReasonPhrase(), HttpStatus.valueOf(response.getStatusLine().getStatusCode())); } + logger.info(response.getStatusLine().getReasonPhrase()); return new ResponseEntity<>(response.getStatusLine().getReasonPhrase(), HttpStatus.valueOf(response.getStatusLine().getStatusCode())); } catch (Exception e) { logger.warn("Error in sending sms", e);