Skip to content

Commit

Permalink
Merge pull request #1979 from palladiumkenya/cr2-patches
Browse files Browse the repository at this point in the history
add an sms utility within kenyaemr global service, adjustment to cr2-…
  • Loading branch information
patryllus authored Aug 23, 2024
2 parents 62efcd0 + fae56b4 commit 7b63676
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
@Transactional
public interface KenyaEmrService extends OpenmrsService {

/**
* Get if this server has been properly configured
* @return whether or not all required settings in the application are configured.
Expand All @@ -35,7 +35,7 @@ public interface KenyaEmrService extends OpenmrsService {
*/
@Transactional(readOnly = true)
boolean isSetupRequired();

/**
* Sets the default location for this server, i.e. the value that should be auto-set for new
* encounters, visits, etc.
Expand Down Expand Up @@ -107,4 +107,6 @@ public interface KenyaEmrService extends OpenmrsService {
*/
@Authorized
public List<SimpleObject> search(String sqlQuery, Map<String, String[]> params);
}

public SimpleObject sendKenyaEmrSms(String recipient, String message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.openmrs.GlobalProperty;
import org.openmrs.Location;
import org.openmrs.LocationAttributeType;
import org.openmrs.Patient;
import org.openmrs.PatientIdentifierType;
import org.openmrs.Visit;
import org.openmrs.api.APIException;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.LocationService;
import org.openmrs.api.context.Context;
import org.openmrs.api.impl.BaseOpenmrsService;
Expand Down Expand Up @@ -75,7 +83,7 @@ public class KenyaEmrServiceImpl extends BaseOpenmrsService implements KenyaEmrS
public void setKenyaEmrDAO(KenyaEmrDAO dao) {
this.dao = dao;
}

/**
* @see org.openmrs.module.kenyaemr.api.KenyaEmrService#isSetupRequired()
*/
Expand Down Expand Up @@ -104,7 +112,7 @@ public void setDefaultLocation(Location location) {
gp.setValue(location);
Context.getAdministrationService().saveGlobalProperty(gp);
}

/**
* @see org.openmrs.module.kenyaemr.api.KenyaEmrService#getDefaultLocation()
*/
Expand All @@ -122,7 +130,7 @@ public Location getDefaultLocation() {
Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
}
}

/**
* @see org.openmrs.module.kenyaemr.api.KenyaEmrService#getDefaultLocationMflCode()
*/
Expand Down Expand Up @@ -302,4 +310,51 @@ private Map<String, String[]> conditionallyAddVisitLocation(Map<String, String[]
}
return updatedParams;
}
}

@Override
public SimpleObject sendKenyaEmrSms(String recipient, String message) {
SimpleObject response = new SimpleObject();
try {
response.add("response", _Sms(recipient, message));
} catch (Exception e) {
response.add("response", "Failed to send SMS " + e.getMessage());
throw new RuntimeException(e);
}

return response;
}
public static String _Sms(String recipient, String message) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
String responseMsg = null;
try {
AdministrationService administrationService = Context.getAdministrationService();
HttpPost postRequest = new HttpPost(administrationService.getGlobalProperty("kenyaemr.sms.url"));
postRequest.setHeader("api-token", administrationService.getGlobalProperty("kenyaemr.sms.apiToken"));
JSONObject json = new JSONObject();
json.put("destination", recipient);
json.put("msg", message);
json.put("sender_id", administrationService.getGlobalProperty("kenyaemr.sms.senderId"));
json.put("gateway", administrationService.getGlobalProperty("kenyaemr.sms.gateway") );

StringEntity entity = new StringEntity(json.toString());
postRequest.setEntity(entity);
postRequest.setHeader("Content-Type", "application/json");

try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());

if (statusCode == 200) {
responseMsg = "SMS sent successfully" + responseBody;
System.out.println("SMS sent successfully \n" + responseBody);
} else {
responseMsg = "Failed to send SMS " + responseBody;
System.err.println("Failed to send SMS \n" + responseBody);
}
}
} finally {
httpClient.close();
}
return responseMsg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.kenyaemr.util;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.junit.Test;

import java.util.Base64;

public class SentSmsUtilityTest {

private static final String SMS_SENDER_ID ="";
private static final String SMS_GATEWAY= "";
private static final String SMS_URL = "https://sms-service/api/sender";
private static final String SMS_API_TOKEN ="";
//test sending
@Test
public void sendKenyaEEmrSms() throws Exception {
_sendSms("2547...", "Hello, this is a test message");
}
public static String _sendSms(String recipient, String message) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
String responseMsg = null;
try {
HttpPost postRequest = new HttpPost(SMS_URL);
postRequest.setHeader("api-token", SMS_API_TOKEN);
JSONObject json = new JSONObject();
json.put("destination", recipient);
json.put("msg", message);
json.put("sender_id", SMS_SENDER_ID);
json.put("gateway", SMS_GATEWAY );

StringEntity entity = new StringEntity(json.toString());
postRequest.setEntity(entity);
postRequest.setHeader("Content-Type", "application/json");

try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity());

if (statusCode == 200) {
responseMsg = "SMS sent successfully" + responseBody;
System.out.println("SMS sent successfully \n" + responseBody);
} else {
responseMsg = "Failed to send SMS " + responseBody;
System.err.println("Failed to send SMS \n" + responseBody);
}
}
} finally {
httpClient.close();
}
return responseMsg;
}
}
Loading

0 comments on commit 7b63676

Please sign in to comment.