Subversion Repositories SmartDukaan

Rev

Rev 34522 | Rev 34550 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 34522 Rev 34545
Line 19... Line 19...
19
import com.spice.profitmandi.service.user.RetailerService;
19
import com.spice.profitmandi.service.user.RetailerService;
20
import com.spice.profitmandi.service.whatsapp.WhatsappMessageService;
20
import com.spice.profitmandi.service.whatsapp.WhatsappMessageService;
21
import com.spice.profitmandi.service.whatsapp.WhatsappMessageType;
21
import com.spice.profitmandi.service.whatsapp.WhatsappMessageType;
22
import org.apache.logging.log4j.LogManager;
22
import org.apache.logging.log4j.LogManager;
23
import org.apache.logging.log4j.Logger;
23
import org.apache.logging.log4j.Logger;
-
 
24
import org.json.JSONArray;
24
import org.json.JSONObject;
25
import org.json.JSONObject;
25
import org.springframework.beans.factory.annotation.Autowired;
26
import org.springframework.beans.factory.annotation.Autowired;
26
import org.springframework.beans.factory.annotation.Value;
27
import org.springframework.beans.factory.annotation.Value;
27
import org.springframework.stereotype.Component;
28
import org.springframework.stereotype.Component;
28
 
29
 
-
 
30
import java.io.BufferedReader;
-
 
31
import java.io.InputStreamReader;
-
 
32
import java.io.OutputStream;
-
 
33
import java.net.HttpURLConnection;
-
 
34
import java.net.URL;
-
 
35
import java.nio.charset.StandardCharsets;
29
import java.time.LocalDate;
36
import java.time.LocalDate;
30
import java.time.LocalDateTime;
37
import java.time.LocalDateTime;
31
import java.util.*;
38
import java.util.*;
32
import java.util.stream.Collectors;
39
import java.util.stream.Collectors;
33
 
40
 
Line 171... Line 178...
171
        }
178
        }
172
 
179
 
173
    }
180
    }
174
 
181
 
175
    @Override
182
    @Override
176
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message,
183
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message, String url) throws ProfitMandiBusinessException {
177
                                 String url) throws ProfitMandiBusinessException {
-
 
178
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
184
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
179
        sendNotificationModel.setCampaignName(campaignName);
185
        sendNotificationModel.setCampaignName(campaignName);
180
        sendNotificationModel.setMessageType(messageType);
186
        sendNotificationModel.setMessageType(messageType);
181
        sendNotificationModel.setTitle(title);
187
        sendNotificationModel.setTitle(title);
182
        sendNotificationModel.setMessage(message);
188
        sendNotificationModel.setMessage(message);
Line 186... Line 192...
186
        sendNotificationModel.setUrl(url);
192
        sendNotificationModel.setUrl(url);
187
        this.sendNotification(sendNotificationModel);
193
        this.sendNotification(sendNotificationModel);
188
    }
194
    }
189
 
195
 
190
    @Override
196
    @Override
191
    public boolean sendWhatsappMessage(String message, String title, String mobile)
197
    public boolean sendWhatsappMessage(String message, String title, String mobile) throws Exception {
192
            throws Exception {
-
 
193
        boolean isSend=false;
198
        boolean isSend=false;
194
        LOGGER.info("Is Prod - {}", isProd);
199
        LOGGER.info("Is Prod - {}", isProd);
195
        if (isProd) {
200
        if (isProd) {
196
            isSend=this.sendWhatsappMessage(WhatsappMessageType.TEXT, message, title, mobile, null, null);
201
            isSend=this.sendWhatsappMessage(WhatsappMessageType.TEXT, message, title, mobile, null, null);
197
        }
202
        }
198
        return isSend;
203
        return isSend;
199
    }
204
    }
200
 
205
 
201
    @Override
206
    @Override
202
    public boolean sendWhatsappMediaMessage(String message, String mobile, String mediaUrl, String fileName, WhatsappMessageType whatsappMessageType)
207
    public boolean sendWhatsappMediaMessage(String message, String mobile, String mediaUrl, String fileName, WhatsappMessageType whatsappMessageType) throws Exception {
203
            throws Exception {
-
 
204
        boolean isSend=false;
208
        boolean isSend=false;
205
        if (isProd) {
209
        if (isProd) {
206
            isSend=this.sendWhatsappMessage(whatsappMessageType, message, null, mobile, mediaUrl, fileName);
210
            isSend=this.sendWhatsappMessage(whatsappMessageType, message, null, mobile, mediaUrl, fileName);
207
        }
211
        }
208
        return isSend;
212
        return isSend;
Line 261... Line 265...
261
            requestParams.put("header", title);
265
            requestParams.put("header", title);
262
        } else if (mediaUrl != null) {
266
        } else if (mediaUrl != null) {
263
            requestParams.put("method", "SENDMEDIAMESSAGE");
267
            requestParams.put("method", "SENDMEDIAMESSAGE");
264
            requestParams.put("msg_type", whatsappMessageType.name());
268
            requestParams.put("msg_type", whatsappMessageType.name());
265
            requestParams.put("caption", message);
269
            requestParams.put("caption", message);
266
            //requestParams.put("msg", message);
-
 
267
            requestParams.put("media_url", mediaUrl);
270
            requestParams.put("media_url", mediaUrl);
268
            requestParams.put("filename", fileName);
271
            requestParams.put("filename", fileName);
269
        }
272
        }
270
        String response =
-
 
271
                restClient.post("https://media.smsgupshup.com/GatewayAPI/rest",
273
        String response = restClient.post("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
272
                        requestParams,
-
 
273
                        requestheaders);
-
 
274
        LOGGER.info("response  - {}", response);
274
        LOGGER.info("response  - {}", response);
275
 
275
 
276
        JSONObject jsonObject = new JSONObject(response);
276
        JSONObject jsonObject = new JSONObject(response);
277
 
277
 
278
        JSONObject whatsappResponse = (JSONObject) jsonObject.get("response");
278
        JSONObject whatsappResponse = (JSONObject) jsonObject.get("response");
Line 345... Line 345...
345
        whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
345
        whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
346
        whatsappMessage.setExternalId(externalId);
346
        whatsappMessage.setExternalId(externalId);
347
        whatsappMessage.setDestAddr(phone);
347
        whatsappMessage.setDestAddr(phone);
348
        whatsappMessageRepository.persist(whatsappMessage);
348
        whatsappMessageRepository.persist(whatsappMessage);
349
    }
349
    }
-
 
350
 
-
 
351
    private boolean sendWhatsAppMessageByMeta(WhatsappMessageType whatsappMessageType, String message, String title, String mobile, String mediaUrl, String fileName) throws Exception {
-
 
352
 
-
 
353
        mobile = this.optInUsersByMobiles(mobile);
-
 
354
 
-
 
355
        String token = "###############";
-
 
356
        String phoneNumberId = "000000000000000000";
-
 
357
        String url = "https://graph.facebook.com/v22.0/" + phoneNumberId + "/messages";
-
 
358
 
-
 
359
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
-
 
360
        conn.setRequestMethod("POST");
-
 
361
        conn.setRequestProperty("Authorization", "Bearer " + token);
-
 
362
        conn.setRequestProperty("Content-Type", "application/json");
-
 
363
        conn.setDoOutput(true);
-
 
364
 
-
 
365
        // Construct the payload
-
 
366
        JSONObject payload = new JSONObject();
-
 
367
        payload.put("messaging_product", "whatsapp");
-
 
368
        payload.put("to", "918529003611");
-
 
369
 
-
 
370
        if (mediaUrl != null) {
-
 
371
            JSONObject image = new JSONObject();
-
 
372
            image.put("link", mediaUrl);
-
 
373
            if (fileName != null) image.put("caption", message);
-
 
374
            payload.put("type", "image");
-
 
375
            payload.put("image", image);
-
 
376
        } else {
-
 
377
            payload.put("type", "template");
-
 
378
            JSONObject template = new JSONObject();
-
 
379
            template.put("name", "bid_live");
-
 
380
            JSONObject language = new JSONObject();
-
 
381
            language.put("code", "en_US");
-
 
382
            template.put("language", language);
-
 
383
            JSONArray components = new JSONArray();
-
 
384
            JSONObject body = new JSONObject();
-
 
385
            body.put("type", "body");
-
 
386
 
-
 
387
            JSONArray parameters = new JSONArray();
-
 
388
            parameters.put(new JSONObject().put("type", "text").put("text", "*Oppo Reno 10 Pro 5G (12GB 256GB) Silvery Grey* at 54.55% Off"));
-
 
389
            parameters.put(new JSONObject().put("type", "text").put("text", "44000.0"));
-
 
390
            parameters.put(new JSONObject().put("type", "text").put("text", "20000.0"));
-
 
391
            parameters.put(new JSONObject().put("type", "text").put("text", "05/05/2025 06:00PM"));
-
 
392
            body.put("parameters", parameters);
-
 
393
            components.put(body);
-
 
394
 
-
 
395
            template.put("components", components);
-
 
396
            payload.put("template", template);
-
 
397
        }
-
 
398
        LOGGER.info("Final WhatsApp payload: \n{}", payload.toString(2));
-
 
399
        try (OutputStream os = conn.getOutputStream()) {
-
 
400
            os.write(payload.toString().getBytes(StandardCharsets.UTF_8));
-
 
401
        }
-
 
402
 
-
 
403
        int status = conn.getResponseCode();
-
 
404
        String response = new BufferedReader(new InputStreamReader(status >= 400 ? conn.getErrorStream() : conn.getInputStream())).lines().collect(Collectors.joining("\n"));
-
 
405
 
-
 
406
        LOGGER.info("WhatsApp API response: {}", response);
-
 
407
 
-
 
408
        if (status != 200 && status != 201) {
-
 
409
            LOGGER.error("Failed to send WhatsApp message. Status: {}, Response: {}", status, response);
-
 
410
            return false;
-
 
411
        }
-
 
412
 
-
 
413
        JSONObject jsonResponse = new JSONObject(response);
-
 
414
        JSONArray messages = jsonResponse.optJSONArray("messages");
-
 
415
        String externalId = null;
-
 
416
 
-
 
417
        if (messages != null && messages.length() > 0) {
-
 
418
            externalId = messages.getJSONObject(0).optString("id");
-
 
419
            WhatsappMessage whatsappMessage = new WhatsappMessage();
-
 
420
            whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
-
 
421
            whatsappMessage.setExternalId(externalId);
-
 
422
            whatsappMessage.setDestAddr(mobile);
-
 
423
            whatsappMessageRepository.persist(whatsappMessage);
-
 
424
        }
-
 
425
 
-
 
426
        return externalId != null;
-
 
427
    }
-
 
428
 
-
 
429
    private String optInUsersByMobiles(String mobiles) throws Exception {
-
 
430
        List<String> savedMobiles = new ArrayList<>();
-
 
431
        if (mobiles == null || mobiles.isEmpty()) return String.join(",",savedMobiles);
-
 
432
        String[] mobileNumbers = mobiles.split(",");
-
 
433
        for (String mobile : mobileNumbers) {
-
 
434
            mobile = mobile.trim();
-
 
435
            if (mobile.isEmpty()) continue;
-
 
436
            Optin optin = optinRepository.selectByMobile(mobile);
-
 
437
            if (optin == null) {
-
 
438
                this.optIn(mobile);
-
 
439
                optin = new Optin();
-
 
440
                optin.setCreated(LocalDateTime.now());
-
 
441
                optin.setMobile(mobile);
-
 
442
                optinRepository.persist(optin);
-
 
443
            }
-
 
444
            savedMobiles.add("91"+mobile);
-
 
445
        }
-
 
446
        return String.join(",",savedMobiles);
-
 
447
    }
350
}
448
}