Commit 5d7b3f8d authored by Ajit Deshmukh's avatar Ajit Deshmukh

Made changes in the WhatsAppComm component to repalce the function name with...

Made changes in the WhatsAppComm component to repalce the function name with the actual result in the message body
parent 0d1eb2a3
--------------loyalty_points_generated------------
Insert into OBJ_FOLLOWUP_ACT (OBJ_NAME,LINE_NO,ACTION_ID,ACTION_TYPE,ACTION_INFO,CONDITIONAL_EXPRESSION,CONDITIONAL_INPUT,CHG_DATE,CHG_USER,CHG_TERM,MAX_RETRY_COUNT) values
('invoice_po',2,'save','E','loyalty_points_generated',null,null,null,null,null,5);
Insert into MAIL_FORMAT (FORMAT_CODE,FORMAT_TYPE,SEND_TO,COPY_TO,BLIND_COPY,SUBJECT,BODY_COMP,PRIORITY,DELIVERY_REPORT,RETURN_RECEIPT,MAIL_APPLICATION,MAIL_SERVER,MAIL_BOX,MAIL_ID,ATTACH_TYPE,ATTACH_TEXT,WINNAME,WIN_NAME,MAIL_GENERATION,MAIL_DESCR,FN_NAME,COND_METHOD,EMAIL_EXPR,ATTACH_OBJECT,TEMPLATE_PURPOSE,STATUS,USER_ID__OWN,BODY_TEXT,MSG_TYPE,TEMPLATE_NAME) values ('loyalty_points_generated','T','df_getCustNumber<Detail1.cust_code>',null,null,'MailSend',0,null,null,null,null,null,null,null,null,null,null,'w_despatch',null,null,null,null,null,null,null,null,null,'[
{
"name":"1",
"value": df_getCustomerName<Detail1.cust_code>
},
{
"name":"2",
"value":"P N RAO"
},
{
"name":"3",
"value": df_getInvoiceId<Detail1.invoice_id>
},
{
"name":"4",
"value": df_getCreditedLoyaltyPoints<Detail1.sale_order>
},
{
"name":"5",
"value": df_getBalanceLoyaltyPoints<Detail1.cust_code>
}
]','W','{ "whatsapp": "loyalty_points_generated", "sms": "67cec93ad6fc05406d37fd92" }');
create or replace NONEDITIONABLE FUNCTION df_getCustNumber (
P_CUST_CODE IN customer.cust_code%TYPE
)
RETURN customer.tele1%TYPE
IS
v_mobile customer.tele1%TYPE;
BEGIN
SELECT c.tele1
INTO v_mobile
FROM customer c
WHERE c.cust_code = P_CUST_CODE;
RETURN v_mobile;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
create or replace NONEDITIONABLE FUNCTION df_getCustomerName(
p_cust_code IN VARCHAR2
)
RETURN VARCHAR2
IS
v_cust_name customer.cust_name%TYPE;
BEGIN
SELECT c.cust_name
INTO v_cust_name
FROM customer c
WHERE TRIM(c.cust_code) = TRIM(p_cust_code);
RETURN v_cust_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 'NO DATA FOUND';
WHEN TOO_MANY_ROWS THEN
RETURN 'MULTIPLE RECORDS FOUND';
WHEN OTHERS THEN
RETURN SQLERRM;
END;
create or replace NONEDITIONABLE FUNCTION df_getInvoiceId (
p_invoice_id IN VARCHAR2
)
RETURN VARCHAR2
IS
BEGIN
RETURN p_invoice_id;
END;
create or replace NONEDITIONABLE FUNCTION df_getCreditedLoyaltyPoints (
p_sale_order IN VARCHAR2
)
RETURN NUMBER
IS
v_lp_credit NUMBER;
BEGIN
SELECT before_amt
INTO v_lp_credit
FROM gift_vouch_trace
WHERE TRIM(ref_no__adj) = TRIM(p_sale_order)
AND ref_type = 'A';
RETURN v_lp_credit;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 0;
WHEN TOO_MANY_ROWS THEN
RETURN NULL;
WHEN OTHERS THEN
RETURN NULL;
END;
create or replace NONEDITIONABLE FUNCTION df_getBalanceLoyaltyPoints (
p_cust_code IN VARCHAR2
)
RETURN NUMBER
IS
v_bal_amt NUMBER;
BEGIN
SELECT NVL(amount, 0) - NVL(adj_amt, 0)
INTO v_bal_amt
FROM gift_voucher
WHERE TRIM(cust_code) = TRIM(p_cust_code);
RETURN v_bal_amt;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 0;
WHEN TOO_MANY_ROWS THEN
RETURN NULL;
WHEN OTHERS THEN
RETURN NULL;
END;
------------order_status_update--------------
Insert into OBJ_FOLLOWUP_ACT (OBJ_NAME,LINE_NO,ACTION_ID,ACTION_TYPE,ACTION_INFO,CONDITIONAL_EXPRESSION,CONDITIONAL_INPUT,CHG_DATE,CHG_USER,CHG_TERM,MAX_RETRY_COUNT) values
('workorder_feedback',5,'Confirm','E','order_status_update',null,null,null,null,null,5);
Insert into MAIL_FORMAT (FORMAT_CODE,FORMAT_TYPE,SEND_TO,COPY_TO,BLIND_COPY,SUBJECT,BODY_COMP,PRIORITY,DELIVERY_REPORT,RETURN_RECEIPT,MAIL_APPLICATION,MAIL_SERVER,MAIL_BOX,MAIL_ID,ATTACH_TYPE,ATTACH_TEXT,WINNAME,WIN_NAME,MAIL_GENERATION,MAIL_DESCR,FN_NAME,COND_METHOD,EMAIL_EXPR,ATTACH_OBJECT,TEMPLATE_PURPOSE,STATUS,USER_ID__OWN,BODY_TEXT,MSG_TYPE,TEMPLATE_NAME) values
('order_status_update','T','df_getCustomerNumerForOrderStatus<Detail1.work_order>',null,null,'MailSend',0,null,null,null,null,null,null,null,null,null,null,'w_workorder_feedback',null,null,null,null,null,null,null,null,null,'[
{
"name":"1",
"value": df_getCustomerNameForOrderStatus<Detail1.work_order>
},
{
"name":"2",
"value": df_getOrderNumber<Detail1.work_order>
},
{
"name":"3",
"value": df_getOrderStatus<Detail1.proc_code>
}
]','W','{ "whatsapp": "order_status_update", "sms": "67cec93ad6fc05406d37fd92" }');
create or replace NONEDITIONABLE FUNCTION df_getCustomerNumerForOrderStatus (
p_work_order IN VARCHAR2
)
RETURN VARCHAR2
IS
v_tele1 customer.tele1%TYPE;
BEGIN
SELECT c.tele1
INTO v_tele1
FROM workorder w
JOIN sorder s ON s.sale_order = w.sale_order
JOIN customer c ON c.cust_code = s.cust_code
WHERE TRIM(w.work_order) = TRIM(p_work_order);
RETURN v_tele1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
WHEN TOO_MANY_ROWS THEN
RETURN NULL;
WHEN OTHERS THEN
RETURN NULL;
END;
create or replace NONEDITIONABLE FUNCTION df_getCustomerNameForOrderStatus (
p_work_order IN VARCHAR2
)
RETURN VARCHAR2
IS
v_custName customer.cust_name%TYPE;
BEGIN
SELECT c.cust_name
INTO v_custName
FROM workorder w
JOIN sorder s ON s.sale_order = w.sale_order
JOIN customer c ON c.cust_code = s.cust_code
WHERE TRIM(w.work_order) = TRIM(p_work_order);
RETURN v_custName;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
WHEN TOO_MANY_ROWS THEN
RETURN NULL;
WHEN OTHERS THEN
RETURN NULL;
END;
create or replace NONEDITIONABLE FUNCTION df_getOrderNumber (
p_work_order IN VARCHAR2
)
RETURN VARCHAR2
IS
BEGIN
RETURN p_work_order;
END;
create or replace NONEDITIONABLE FUNCTION df_getOrderStatus (
p_proc_code IN VARCHAR2
)
RETURN VARCHAR2
IS
v_descr process.descr%TYPE;
BEGIN
SELECT descr
INTO v_descr
FROM process
WHERE TRIM(proc_code) = TRIM(p_proc_code);
RETURN v_descr;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN 'NO DATA FOUND';
WHEN TOO_MANY_ROWS THEN
RETURN 'MULTIPLE RECORDS FOUND';
WHEN OTHERS THEN
RETURN NULL;
END;
package ibase.communication;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.json.JSONArray;
import org.json.JSONObject;
import ibase.communication.utility.CommUtility;
import ibase.utility.BaseLogger;
import ibase.utility.UserInfoBean;
import ibase.webitm.ejb.DBAccessEJB;
public class WhatsAppComm {
private DBAccessEJB dbAccess = null;
public String sendWhatsApp(String actionInfo, String whatsAppConfString, String receiptMobNumber, UserInfoBean userInfo, String mailXmlString, String tranXmlString) {
BaseLogger.log("3", null, null, " SendWhatsApppppp Test 19-May-25 Ams @@####### ");
BaseLogger.log("3", null, null, "whatsappConfString ==> " + whatsAppConfString);
BaseLogger.log("3", null, null, " ActionInfo====> " +actionInfo);
BaseLogger.log("3", null, null, " receiptMobNumber==> " +receiptMobNumber);
BaseLogger.log("3", null, null, " tranXmlString==> " +tranXmlString);
String sendWhatsAppResult = "";
try {
String smsTemplateId = null;
CommUtility commUtility = new CommUtility();
// Added by Gagan B. on 29-JAN-26 to only evaluate tranXmlString if it is not empty
if (tranXmlString != null && !tranXmlString.isEmpty()) {
String result = commUtility.getValue(receiptMobNumber, tranXmlString, userInfo);
BaseLogger.log("3", null, null, "result::: [" + result + "]");
receiptMobNumber = result;
}
JSONObject whatsAppJsonObj = null;
if (whatsAppConfString != null && !whatsAppConfString.isEmpty()) {
whatsAppJsonObj = new JSONObject(whatsAppConfString);
JSONArray parameters = whatsAppJsonObj.getJSONArray("parameters");
for (int i = 0; i < parameters.length(); i++) {
JSONObject param = parameters.getJSONObject(i);
String value = param.getString("value");
if (value.startsWith("df_") || value.contains("<")){
String replacedValue = commUtility.getValue(value, tranXmlString, userInfo);
param.put("value", replacedValue);
}
}
}
BaseLogger.log("3", null, null, " whatsappConfString==> " +whatsAppConfString);
BaseLogger.log("3", null, null, " receiptMobNumber==> " +receiptMobNumber);
String templateName = getDBAccess().getDBColumnValue("MAIL_FORMAT", "TEMPLATE_NAME",
(" FORMAT_CODE = '" + actionInfo + "' "), userInfo.getTransDB());
BaseLogger.log("3", null, null, "templateName::: [" + templateName + "]");
// Added by Amol S to get the template name for SMS and WhatsApp message, which are stored in JSON format -- Start
if (templateName != null && templateName.trim().startsWith("{")) {
BaseLogger.log("3", null, null, "Inside the if for whtsapp New ==>" + templateName + "]");
// Parse as JSON
JSONObject jsonObj = new JSONObject(templateName);
smsTemplateId = jsonObj.optString("whatsapp", templateName);
BaseLogger.log("3", null, null, "smsTemplateId ==>" + smsTemplateId + "]");
} else {
smsTemplateId = templateName;
}
// Added by Amol S to get the template name for SMS and WhatsApp message, which are stored in JSON format -- End
if (whatsAppJsonObj == null) {
throw new Exception("WhatsApp configuration is empty.");
}
String endpoint = whatsAppJsonObj.getString("endpoint");
String accessToken = whatsAppJsonObj.getString("token_id");
String broadcast_name = whatsAppJsonObj.getString("broadcast_name");
JSONArray parameters = whatsAppJsonObj.getJSONArray("parameters");
/*
JSONArray parameterArray = new JSONArray();
JSONObject parameterObj = new JSONObject();
parameterObj.put("name", "1");
parameterObj.put("value", "123456");
parameterArray.put(parameterObj);
*/
JSONObject jsonBodyObj = new JSONObject();
jsonBodyObj.put("template_name", smsTemplateId);
jsonBodyObj.put("broadcast_name", broadcast_name);
jsonBodyObj.put("parameters", parameters);
String jsonBody = jsonBodyObj.toString();
BaseLogger.log("3", null, null, "jsonBody:: [" + jsonBody + "]");
BaseLogger.log("3", null, null, "accessToken::: [" + accessToken + "]");
endpoint=endpoint+receiptMobNumber;
BaseLogger.log("3", null, null, "New ENDPOINT==>::: [" + endpoint + "]");
URL url = new URL(endpoint);
// HttpURLConnection conn = (HttpURLConnection) url.openConnection();
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
// conn.setRequestProperty("Content-Type", "application/json-patch+json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
try (OutputStream os = conn.getOutputStream()) {
byte[] input = jsonBody.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = conn.getResponseCode();
BaseLogger.log("3", null, null, "WhatsApp API Response Code: " + responseCode);
InputStream is;
is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
br.close();
if (responseCode == 200 || responseCode == 201) {
sendWhatsAppResult = "success";
BaseLogger.log("3", null, null, "WA Message sent successfully response code: [" + responseCode +"]"
+ "Response Body ["+ response.toString() +"]");
} else {
sendWhatsAppResult = "error";
BaseLogger.log("3", null, null, "Failed to send WhatsApp message. HTTP code: " + responseCode
+ "Response Body ["+ response.toString() +"]");
}
conn.disconnect();
} catch (Exception e) {
BaseLogger.log("3", null, null, "Error sending WhatsApp message: " + e.getMessage());
e.printStackTrace();
}
// Added by Gagan B. on 30-JAN-26 to return message as "error" and "success" from sendWhatsApp method.
return sendWhatsAppResult;
}
private DBAccessEJB getDBAccess() {
try {
if (dbAccess == null) {
dbAccess = new DBAccessEJB();
}
}
catch (Exception e) {
e.printStackTrace();
}
return dbAccess;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment