Skip to content

Commit

Permalink
check whether phone number contains country code or not. if not, adde…
Browse files Browse the repository at this point in the history
…d country code from the env of sms-service.added logger when sms is sent successfully.
  • Loading branch information
harshkumar8789 committed Dec 5, 2023
1 parent 26dd24f commit 30d3d95
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/org/bahmni/sms/SMSProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/bahmni/sms/impl/DefaultSmsSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>() {{
add(phoneNumber);
add(finalPhoneNumber);
}});
message.setOriginator(smsProperties.getOriginator());
message.setContent(messageText);
Expand All @@ -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);
Expand Down

0 comments on commit 30d3d95

Please sign in to comment.