Subversion Repositories SmartDukaan

Rev

Rev 36194 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36194 Rev 36962
Line 18... Line 18...
18
import com.spice.profitmandi.dao.repository.cs.PartnerRegionRepository;
18
import com.spice.profitmandi.dao.repository.cs.PartnerRegionRepository;
19
import com.spice.profitmandi.dao.repository.dtr.*;
19
import com.spice.profitmandi.dao.repository.dtr.*;
20
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
20
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
21
import com.spice.profitmandi.dao.repository.whatsapp.WhatsappMessageRepository;
21
import com.spice.profitmandi.dao.repository.whatsapp.WhatsappMessageRepository;
22
import com.spice.profitmandi.service.user.RetailerService;
22
import com.spice.profitmandi.service.user.RetailerService;
-
 
23
import com.spice.profitmandi.service.whatsapp.CpassWhatsappService;
23
import com.spice.profitmandi.service.whatsapp.WhatsappMessageService;
24
import com.spice.profitmandi.service.whatsapp.WhatsappMessageService;
24
import com.spice.profitmandi.service.whatsapp.WhatsappMessageType;
25
import com.spice.profitmandi.service.whatsapp.WhatsappMessageType;
25
import org.apache.logging.log4j.LogManager;
26
import org.apache.logging.log4j.LogManager;
26
import org.apache.logging.log4j.Logger;
27
import org.apache.logging.log4j.Logger;
27
import org.json.JSONArray;
-
 
28
import org.json.JSONObject;
28
import org.json.JSONObject;
29
import org.springframework.beans.factory.annotation.Autowired;
29
import org.springframework.beans.factory.annotation.Autowired;
30
import org.springframework.beans.factory.annotation.Value;
30
import org.springframework.beans.factory.annotation.Value;
31
import org.springframework.stereotype.Component;
31
import org.springframework.stereotype.Component;
32
 
32
 
33
import java.io.BufferedReader;
-
 
34
import java.io.InputStreamReader;
-
 
35
import java.io.OutputStream;
-
 
36
import java.net.HttpURLConnection;
-
 
37
import java.net.URL;
-
 
38
import java.nio.charset.StandardCharsets;
33
import java.nio.charset.StandardCharsets;
39
import java.time.LocalDate;
34
import java.time.LocalDate;
40
import java.time.LocalDateTime;
35
import java.time.LocalDateTime;
41
import java.util.*;
36
import java.util.*;
42
import java.util.stream.Collectors;
37
import java.util.stream.Collectors;
Line 83... Line 78...
83
    private WhatsappMessageRepository whatsappMessageRepository;
78
    private WhatsappMessageRepository whatsappMessageRepository;
84
 
79
 
85
    @Autowired
80
    @Autowired
86
    private WhatsappMessageService whatsappMessageService;
81
    private WhatsappMessageService whatsappMessageService;
87
 
82
 
-
 
83
    @Autowired
-
 
84
    private CpassWhatsappService cpassWhatsappService;
-
 
85
 
-
 
86
    @Value("${whatsapp.provider:cpass}")
-
 
87
    private String whatsappProvider;
-
 
88
 
-
 
89
    private boolean isCpass() {
-
 
90
        return "cpass".equalsIgnoreCase(whatsappProvider);
-
 
91
    }
-
 
92
 
88
    @Override
93
    @Override
89
    public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
94
    public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
90
 
95
 
91
        SimpleCampaignParams scp = new SimpleCampaignParams();
96
        SimpleCampaignParams scp = new SimpleCampaignParams();
92
        scp.setMessage(sendNotificationModel.getMessage());
97
        scp.setMessage(sendNotificationModel.getMessage());
Line 309... Line 314...
309
        LOGGER.info("response" + response);
314
        LOGGER.info("response" + response);
310
    }
315
    }
311
 
316
 
312
    private boolean sendWhatsappMessage(WhatsappMessageType whatsappMessageType, String message, String title, String mobile, String mediaUrl, String fileName)
317
    private boolean sendWhatsappMessage(WhatsappMessageType whatsappMessageType, String message, String title, String mobile, String mediaUrl, String fileName)
313
            throws Exception {
318
            throws Exception {
-
 
319
        if (isCpass()) {
-
 
320
            return cpassWhatsappService.sendBasic(whatsappMessageType, message, title, mobile, mediaUrl, fileName);
-
 
321
        }
314
        String sendTo = null;
322
        String sendTo = null;
315
        boolean isSend=false;
323
        boolean isSend=false;
316
        if (mobile.length() != 10) {
324
        if (mobile.length() != 10) {
317
            return isSend;
325
            return isSend;
318
        } else {
326
        } else {
Line 386... Line 394...
386
    }
394
    }
387
 
395
 
388
 
396
 
389
    @Override
397
    @Override
390
    public void sendPaymentWhatsappMessage(String mobile, String message) throws Exception {
398
    public void sendPaymentWhatsappMessage(String mobile, String message) throws Exception {
-
 
399
        if (isCpass()) {
-
 
400
            cpassWhatsappService.sendText(mobile, message);
-
 
401
            return;
-
 
402
        }
391
        String sendTo = "91" + mobile;
403
        String sendTo = "91" + mobile;
392
//        String sendTo = "917082253510";
404
//        String sendTo = "917082253510";
393
 
405
 
394
        Map<String, String> requestHeaders = new HashMap<>();
406
        Map<String, String> requestHeaders = new HashMap<>();
395
        requestHeaders.put("Content-Type", "application/x-www-form-urlencoded");
407
        requestHeaders.put("Content-Type", "application/x-www-form-urlencoded");
Line 424... Line 436...
424
        whatsappMessage.setExternalId(externalId);
436
        whatsappMessage.setExternalId(externalId);
425
        whatsappMessage.setDestAddr(phone);
437
        whatsappMessage.setDestAddr(phone);
426
        whatsappMessageRepository.persist(whatsappMessage);
438
        whatsappMessageRepository.persist(whatsappMessage);
427
    }
439
    }
428
 
440
 
429
    private boolean sendWhatsAppMessageByMeta(WhatsappMessageType whatsappMessageType, String message, String title, String mobile, String mediaUrl, String fileName) throws Exception {
-
 
430
 
-
 
431
        mobile = this.optInUsersByMobiles(mobile);
-
 
432
 
-
 
433
        String token = "###############";
-
 
434
        String phoneNumberId = "000000000000000000";
-
 
435
        String url = "https://graph.facebook.com/v22.0/" + phoneNumberId + "/messages";
-
 
436
 
-
 
437
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
-
 
438
        conn.setRequestMethod("POST");
-
 
439
        conn.setRequestProperty("Authorization", "Bearer " + token);
-
 
440
        conn.setRequestProperty("Content-Type", "application/json");
-
 
441
        conn.setDoOutput(true);
-
 
442
 
-
 
443
        // Construct the payload
-
 
444
        JSONObject payload = new JSONObject();
-
 
445
        payload.put("messaging_product", "whatsapp");
-
 
446
        payload.put("to", "918529003611");
-
 
447
 
-
 
448
        if (mediaUrl != null) {
-
 
449
            JSONObject image = new JSONObject();
-
 
450
            image.put("link", mediaUrl);
-
 
451
            if (fileName != null) image.put("caption", message);
-
 
452
            payload.put("type", "image");
-
 
453
            payload.put("image", image);
-
 
454
        } else {
-
 
455
            payload.put("type", "template");
-
 
456
            JSONObject template = new JSONObject();
-
 
457
            template.put("name", "bid_live");
-
 
458
            JSONObject language = new JSONObject();
-
 
459
            language.put("code", "en_US");
-
 
460
            template.put("language", language);
-
 
461
            JSONArray components = new JSONArray();
-
 
462
            JSONObject body = new JSONObject();
-
 
463
            body.put("type", "body");
-
 
464
 
-
 
465
            JSONArray parameters = new JSONArray();
-
 
466
            parameters.put(new JSONObject().put("type", "text").put("text", "*Oppo Reno 10 Pro 5G (12GB 256GB) Silvery Grey* at 54.55% Off"));
-
 
467
            parameters.put(new JSONObject().put("type", "text").put("text", "44000.0"));
-
 
468
            parameters.put(new JSONObject().put("type", "text").put("text", "20000.0"));
-
 
469
            parameters.put(new JSONObject().put("type", "text").put("text", "05/05/2025 06:00PM"));
-
 
470
            body.put("parameters", parameters);
-
 
471
            components.put(body);
-
 
472
 
-
 
473
            template.put("components", components);
-
 
474
            payload.put("template", template);
-
 
475
        }
-
 
476
        LOGGER.info("Final WhatsApp payload: \n{}", payload.toString(2));
-
 
477
        try (OutputStream os = conn.getOutputStream()) {
-
 
478
            os.write(payload.toString().getBytes(StandardCharsets.UTF_8));
-
 
479
        }
-
 
480
 
-
 
481
        int status = conn.getResponseCode();
-
 
482
        String response = new BufferedReader(new InputStreamReader(status >= 400 ? conn.getErrorStream() : conn.getInputStream())).lines().collect(Collectors.joining("\n"));
-
 
483
 
-
 
484
        LOGGER.info("WhatsApp API response: {}", response);
-
 
485
 
-
 
486
        if (status != 200 && status != 201) {
-
 
487
            LOGGER.error("Failed to send WhatsApp message. Status: {}, Response: {}", status, response);
-
 
488
            return false;
-
 
489
        }
-
 
490
 
-
 
491
        JSONObject jsonResponse = new JSONObject(response);
-
 
492
        JSONArray messages = jsonResponse.optJSONArray("messages");
-
 
493
        String externalId = null;
-
 
494
 
-
 
495
        if (messages != null && messages.length() > 0) {
-
 
496
            externalId = messages.getJSONObject(0).optString("id");
-
 
497
            WhatsappMessage whatsappMessage = new WhatsappMessage();
-
 
498
            whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
-
 
499
            whatsappMessage.setExternalId(externalId);
-
 
500
            whatsappMessage.setDestAddr(mobile);
-
 
501
            whatsappMessageRepository.persist(whatsappMessage);
-
 
502
        }
-
 
503
 
-
 
504
        return externalId != null;
-
 
505
    }
-
 
506
 
-
 
507
    private String optInUsersByMobiles(String mobiles) throws Exception {
-
 
508
        List<String> savedMobiles = new ArrayList<>();
-
 
509
        if (mobiles == null || mobiles.isEmpty()) return String.join(",",savedMobiles);
-
 
510
        String[] mobileNumbers = mobiles.split(",");
-
 
511
        for (String mobile : mobileNumbers) {
-
 
512
            mobile = mobile.trim();
-
 
513
            if (mobile.isEmpty()) continue;
-
 
514
            Optin optin = optinRepository.selectByMobile(mobile);
-
 
515
            if (optin == null) {
-
 
516
                this.optIn(mobile);
-
 
517
                optin = new Optin();
-
 
518
                optin.setCreated(LocalDateTime.now());
-
 
519
                optin.setMobile(mobile);
-
 
520
                optinRepository.persist(optin);
-
 
521
            }
-
 
522
            savedMobiles.add("91"+mobile);
-
 
523
        }
-
 
524
        return String.join(",",savedMobiles);
-
 
525
    }
-
 
526
}
441
}