Subversion Repositories SmartDukaan

Rev

Rev 36194 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
25822 amit.gupta 1
package com.spice.profitmandi.service;
2
 
3
import com.google.gson.Gson;
30025 amit.gupta 4
import com.spice.profitmandi.common.enumuration.MessageType;
29198 manish 5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
29927 amit.gupta 6
import com.spice.profitmandi.common.model.ProfitMandiConstants;
25822 amit.gupta 7
import com.spice.profitmandi.common.model.SendNotificationModel;
30989 tejbeer 8
import com.spice.profitmandi.common.web.client.RestClient;
29927 amit.gupta 9
import com.spice.profitmandi.dao.entity.auth.AuthUser;
32405 jai.hind 10
import com.spice.profitmandi.dao.entity.dtr.*;
36064 aman 11
import com.spice.profitmandi.dao.entity.fofo.Customer;
25822 amit.gupta 12
import com.spice.profitmandi.dao.entity.user.Device;
32405 jai.hind 13
import com.spice.profitmandi.dao.entity.whatsapp.WhatsappMessage;
25822 amit.gupta 14
import com.spice.profitmandi.dao.model.SimpleCampaign;
15
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
16
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
29927 amit.gupta 17
import com.spice.profitmandi.dao.repository.cs.CsService;
35207 amit 18
import com.spice.profitmandi.dao.repository.cs.PartnerRegionRepository;
32405 jai.hind 19
import com.spice.profitmandi.dao.repository.dtr.*;
36064 aman 20
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
32405 jai.hind 21
import com.spice.profitmandi.dao.repository.whatsapp.WhatsappMessageRepository;
29198 manish 22
import com.spice.profitmandi.service.user.RetailerService;
36962 vikas 23
import com.spice.profitmandi.service.whatsapp.CpassWhatsappService;
32405 jai.hind 24
import com.spice.profitmandi.service.whatsapp.WhatsappMessageService;
32854 amit.gupta 25
import com.spice.profitmandi.service.whatsapp.WhatsappMessageType;
32405 jai.hind 26
import org.apache.logging.log4j.LogManager;
27
import org.apache.logging.log4j.Logger;
28
import org.json.JSONObject;
29
import org.springframework.beans.factory.annotation.Autowired;
34522 ranu 30
import org.springframework.beans.factory.annotation.Value;
32405 jai.hind 31
import org.springframework.stereotype.Component;
25822 amit.gupta 32
 
34545 vikas.jang 33
import java.nio.charset.StandardCharsets;
32508 amit.gupta 34
import java.time.LocalDate;
32405 jai.hind 35
import java.time.LocalDateTime;
36
import java.util.*;
37
import java.util.stream.Collectors;
38
 
25835 amit.gupta 39
@Component
25822 amit.gupta 40
public class NotificationServiceImpl implements NotificationService {
41
 
35236 amit 42
    private static final Logger LOGGER = LogManager.getLogger(NotificationServiceImpl.class);
32253 amit.gupta 43
    @Autowired
44
    UserCampaignRepository userCampaignRepository;
45
    @Autowired
46
    UserRepository dtrUserRepository;
47
    @Autowired
48
    UserAccountRepository userAccountRepository;
49
    @Autowired
50
    NotificationCampaignRepository notificationCampaignRepository;
51
    @Autowired
52
    DeviceRepository deviceRepository;
35207 amit 53
 
32253 amit.gupta 54
    @Autowired
35207 amit 55
    PartnerRegionRepository partnerRegionRepository;
56
 
57
 
58
    @Autowired
32253 amit.gupta 59
    FofoStoreRepository fofoStoreRepository;
60
    @Autowired
61
    CsService csService;
62
    @Autowired
63
    OptinRepository optinRepository;
64
    @Autowired
65
    RetailerService retailerService;
66
    @Autowired
36064 aman 67
    CustomerRepository customerRepository;
68
    @Autowired
32253 amit.gupta 69
    PushNotificationRepository pushNotificationRepository;
70
    @Autowired
71
    private Gson gson;
72
    @Autowired
73
    private RestClient restClient;
34522 ranu 74
    @Value("${prod}")
75
    private boolean isProd;
25822 amit.gupta 76
 
32405 jai.hind 77
    @Autowired
32417 amit.gupta 78
    private WhatsappMessageRepository whatsappMessageRepository;
32405 jai.hind 79
 
80
    @Autowired
81
    private WhatsappMessageService whatsappMessageService;
82
 
36962 vikas 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
 
32253 amit.gupta 93
    @Override
94
    public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
29927 amit.gupta 95
 
32253 amit.gupta 96
        SimpleCampaignParams scp = new SimpleCampaignParams();
97
        scp.setMessage(sendNotificationModel.getMessage());
98
        scp.setTitle(sendNotificationModel.getTitle());
99
        scp.setImageUrl(sendNotificationModel.getImageUrl());
100
        scp.setType(sendNotificationModel.getType());
101
        scp.setUrl(sendNotificationModel.getUrl());
102
        scp.setShowImage(sendNotificationModel.getShowImage());
103
        scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
104
        SimpleCampaign sc = new SimpleCampaign(scp);
105
        sc.setSimpleCampaignParams(scp);
30025 amit.gupta 106
 
32253 amit.gupta 107
        NotificationCampaign nc = new NotificationCampaign();
108
        nc.setName(sendNotificationModel.getCampaignName());
109
        nc.setImplementationType("SimpleCampaign");
110
        nc.setImplementationParams(gson.toJson(scp));
111
        nc.setMessageType(sendNotificationModel.getMessageType());
112
        nc.setDocumentId(sendNotificationModel.getDocumentId());
113
        nc.setCreatedTimestamp(LocalDateTime.now());
114
        notificationCampaignRepository.persist(nc);
25822 amit.gupta 115
 
32253 amit.gupta 116
        Set<Integer> userIds = new HashSet<>();
117
        if (sendNotificationModel.getUserIds() != null && sendNotificationModel.getUserIds().size() > 0) {
118
            userIds.addAll(sendNotificationModel.getUserIds());
119
        }
29198 manish 120
 
32253 amit.gupta 121
        if (sendNotificationModel.getStateIds() != null && sendNotificationModel.getStateIds().size() > 0) {
33025 amit.gupta 122
            List<Integer> fofoIds = fofoStoreRepository.selectByWarehouseIds(sendNotificationModel.getStateIds()).stream()
123
                    .map(x -> x.getId()).collect(Collectors.toList());
34204 tejus.loha 124
            if (fofoIds.size() > 0) {
33025 amit.gupta 125
                userIds.addAll(userAccountRepository.selectUserIdsByRetailerIds(fofoIds));
126
            }
127
 
32253 amit.gupta 128
        }
35207 amit 129
        if(sendNotificationModel.getRegionIds() != null && sendNotificationModel.getRegionIds().size() > 0) {
130
            List<Integer> fofoIds = partnerRegionRepository.selectAllByRegionIds(sendNotificationModel.getRegionIds()).stream().map(x->x.getFofoId()).collect(Collectors.toList());
131
            if(fofoIds.size()>0) {
132
                userIds.addAll(userAccountRepository.selectUserIdsByRetailerIds(fofoIds));
133
            }
134
        }
29927 amit.gupta 135
 
34204 tejus.loha 136
        if (userIds.size() > 0) {
33025 amit.gupta 137
            for (Integer userId : userIds) {
138
                UserCampaign uc = new UserCampaign();
139
                uc.setCampaignId(nc.getId());
140
                uc.setUserId(userId);
141
                uc.setPushTimestamp(LocalDateTime.now());
142
                userCampaignRepository.persist(uc);
143
            }
144
            List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(new ArrayList<>(userIds),
145
                    LocalDateTime.now().minusMonths(1), LocalDateTime.now());
146
            pushNotification(nc.getId(), devices);
147
        } else {
148
            LOGGER.info("Failed to send notification to any retailer with this model - {}", sendNotificationModel);
32253 amit.gupta 149
        }
25822 amit.gupta 150
 
32253 amit.gupta 151
    }
25822 amit.gupta 152
 
32253 amit.gupta 153
    @Override
154
    public void sendNotificationToAll(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
155
        sendNotificationModel.setUserIds(fofoStoreRepository.selectAllDtrUserIds());
156
        Set<AuthUser> authUsers = new HashSet<>(
157
                csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
158
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));
159
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));
34903 ranu 160
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_ABM));
32253 amit.gupta 161
        List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
162
        emailIds.add("devkinandan.lal@smartdukaan.com");
163
        List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);
32405 jai.hind 164
        sendNotificationModel.getUserIds()
165
                .addAll(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));
32253 amit.gupta 166
        this.sendNotification(sendNotificationModel);
167
    }
25822 amit.gupta 168
 
32253 amit.gupta 169
    @Override
34550 vikas.jang 170
    public void sendNotificationToSystemUsers(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
171
        Set<AuthUser> authUsers = new HashSet<>(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
172
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));
173
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));
34903 ranu 174
        authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_ABM));
34550 vikas.jang 175
        List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
176
        emailIds.add("devkinandan.lal@smartdukaan.com");
177
        List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);
178
        sendNotificationModel.setUserIds(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));
179
        this.sendNotification(sendNotificationModel);
180
    }
181
 
182
    @Override
32405 jai.hind 183
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message)
184
            throws ProfitMandiBusinessException {
32253 amit.gupta 185
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
186
        sendNotificationModel.setCampaignName(campaignName);
187
        sendNotificationModel.setMessageType(messageType);
188
        sendNotificationModel.setTitle(title);
189
        sendNotificationModel.setMessage(message);
190
        int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
191
        sendNotificationModel.setUserIds(Arrays.asList(userId));
192
        sendNotificationModel.setMessageType(MessageType.wallet);
193
        this.sendNotification(sendNotificationModel);
194
    }
25822 amit.gupta 195
 
32253 amit.gupta 196
    public SendNotificationModel getDefaultNotificationModel() {
197
        SendNotificationModel sendNotificationModel = new SendNotificationModel();
198
        sendNotificationModel.setType("url");
35840 vikas 199
        sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
32253 amit.gupta 200
        sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
201
        sendNotificationModel.setMessageType(MessageType.notification);
202
        return sendNotificationModel;
203
    }
25822 amit.gupta 204
 
32253 amit.gupta 205
    public void pushNotification(int cid, List<Device> devices) {
29198 manish 206
 
32253 amit.gupta 207
        for (Device device : devices) {
208
            PushNotifications pn = new PushNotifications();
209
            pn.setNotificationCampaignid(cid);
210
            pn.setDeviceId(device.getId());
211
            pn.setUserId(device.getUser_id());
212
            pushNotificationRepository.persist(pn);
213
        }
29198 manish 214
 
32253 amit.gupta 215
    }
29198 manish 216
 
32253 amit.gupta 217
    @Override
34545 vikas.jang 218
    public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message, String url) throws ProfitMandiBusinessException {
32253 amit.gupta 219
        SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
220
        sendNotificationModel.setCampaignName(campaignName);
221
        sendNotificationModel.setMessageType(messageType);
222
        sendNotificationModel.setTitle(title);
223
        sendNotificationModel.setMessage(message);
224
        int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
225
        sendNotificationModel.setUserIds(Arrays.asList(userId));
226
        sendNotificationModel.setMessageType(messageType);
227
        sendNotificationModel.setUrl(url);
228
        this.sendNotification(sendNotificationModel);
229
    }
29198 manish 230
 
32253 amit.gupta 231
    @Override
34545 vikas.jang 232
    public boolean sendWhatsappMessage(String message, String title, String mobile) throws Exception {
34204 tejus.loha 233
        boolean isSend=false;
34520 amit.gupta 234
        LOGGER.info("Is Prod - {}", isProd);
32874 amit.gupta 235
        if (isProd) {
34204 tejus.loha 236
            isSend=this.sendWhatsappMessage(WhatsappMessageType.TEXT, message, title, mobile, null, null);
32874 amit.gupta 237
        }
34204 tejus.loha 238
        return isSend;
32253 amit.gupta 239
    }
25822 amit.gupta 240
 
32253 amit.gupta 241
    @Override
35504 vikas 242
    public void sendADLDWhatsappMessage(String mobile) throws Exception {
243
        LOGGER.info("Is Prod - {}", isProd);
244
        if (isProd) {
245
            String message = "क्या आपने SmartDukaan Protect Plus Plan लिया है?\n\n" +
246
                    "सिर्फ ₹199 से शुरू, पाएं अपने मोबाइल के लिए 1 साल की स्मार्ट सुरक्षा —\n" +
247
                    "Liquid Damage Protection\n" +
248
                    "Accidental Damage Protection\n" +
249
                    "अपने फोन को महंगे रिपेयर खर्च से बचाएं।\n" +
250
                    "अभी जानकारी लें और अपने फोन को सुरक्षित बनाएं!";
251
            String title = "SmartDukaan से खरीदारी करने के लिए धन्यवाद!";
252
 
253
            this.sendWhatsappMessage(WhatsappMessageType.TEXT, message, title, mobile, null, null);
254
        }
255
    }
256
 
257
    @Override
36064 aman 258
    public boolean sendWhatsappInvoice(int customerId, int fofoId, String invoiceNumber, String whatsAppNo) throws Exception {
259
        boolean shouldSend = shouldSendWhatsappMessage(whatsAppNo);
260
        if (!shouldSend) return false;
261
 
262
        Customer customer = customerRepository.selectById(customerId);
263
        String mobileNumber = (whatsAppNo != null && !whatsAppNo.isEmpty()) ? whatsAppNo : customer.getMobileNumber();
264
 
265
        String message = "*SmartDukaan's One-Time Offer Unlocked!*\n" +
36194 vikas 266
                "Thank you for your purchase.\n" +
36064 aman 267
                "\n" +
36194 vikas 268
                "Abhi-abhi naya phone liya hai...\n" +
269
                "Par kya aapne uski poori suraksha li hai?\n" +
270
                "SmartDukaan par sabse kam daam mein plans available hain:\n" +
36064 aman 271
                "\n" +
36194 vikas 272
                "*Complete Protection Plan* (Accidental & Liquid Damage) - Starting ₹199\n" +
36064 aman 273
                "\n" +
36194 vikas 274
                "*Extended 1 Year Warranty* - ₹199 se shuru\n" +
36064 aman 275
                "\n" +
36194 vikas 276
                "Abhi store par iski jaankari lein.\n" +
36064 aman 277
                "\n" +
36194 vikas 278
                "*Ye offer phone ki kharidari ke sirf 24 hours tak hi valid hai!*";
36064 aman 279
 
280
        String mediaUrl = "https://partners.smartdukaan.com/wa-invoice-send/"
281
                + Base64.getMimeEncoder().encodeToString(invoiceNumber.getBytes(StandardCharsets.UTF_8)) + ".pdf";
282
        String fileName = "INV-" + invoiceNumber.replace("/", "-") + ".pdf";
283
 
284
        LOGGER.info("sendWhatsappInvoice: mobile={} mediaUrl={}", mobileNumber, mediaUrl);
285
        return sendWhatsappMediaMessage(message, mobileNumber, mediaUrl, fileName, WhatsappMessageType.DOCUMENT);
286
    }
287
 
288
    @Override
34545 vikas.jang 289
    public boolean sendWhatsappMediaMessage(String message, String mobile, String mediaUrl, String fileName, WhatsappMessageType whatsappMessageType) throws Exception {
34204 tejus.loha 290
        boolean isSend=false;
36064 aman 291
//        if (isProd) {
292
//            isSend=this.sendWhatsappMessage(whatsappMessageType, message, null, mobile, mediaUrl, fileName);
293
//        }
294
        isSend = this.sendWhatsappMessage(whatsappMessageType, message, null, mobile, mediaUrl, fileName);
34204 tejus.loha 295
        return isSend;
32253 amit.gupta 296
    }
25822 amit.gupta 297
 
32253 amit.gupta 298
    @Override
299
    public void optIn(String phoneNumber) throws Exception {
300
        Map<String, String> requestheaders = new HashMap<>();
301
        requestheaders.put("Content-Type", "application/x-www-form-urlencoded");
302
        Map<String, String> requestParams = new HashMap<>();
303
        requestParams.put("userid", String.valueOf(2000215976));
304
        requestParams.put("password", "MFRd!BBL");
305
        requestParams.put("phone_number", phoneNumber);
306
        requestParams.put("auth_scheme", "plain");
307
        requestParams.put("v", "1.1");
308
        requestParams.put("format", "json");
28400 tejbeer 309
 
32253 amit.gupta 310
        requestParams.put("method", "OPT_IN");
25822 amit.gupta 311
 
32253 amit.gupta 312
        requestParams.put("channel", "WHATSAPP");
313
        String response = restClient.get("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
314
        LOGGER.info("response" + response);
315
    }
29927 amit.gupta 316
 
34204 tejus.loha 317
    private boolean sendWhatsappMessage(WhatsappMessageType whatsappMessageType, String message, String title, String mobile, String mediaUrl, String fileName)
32405 jai.hind 318
            throws Exception {
36962 vikas 319
        if (isCpass()) {
320
            return cpassWhatsappService.sendBasic(whatsappMessageType, message, title, mobile, mediaUrl, fileName);
321
        }
32260 amit.gupta 322
        String sendTo = null;
34204 tejus.loha 323
        boolean isSend=false;
32259 amit.gupta 324
        if (mobile.length() != 10) {
34204 tejus.loha 325
            return isSend;
32259 amit.gupta 326
        } else {
32260 amit.gupta 327
            sendTo = 91 + mobile;
32259 amit.gupta 328
        }
32253 amit.gupta 329
        Map<String, String> requestheaders = new HashMap<>();
330
        requestheaders.put("Content-Type", "application/x-www-form-urlencoded");
331
        Map<String, String> requestParams = new HashMap<>();
332
        requestParams.put("userid", String.valueOf(2000215976));
333
        requestParams.put("password", "MFRd!BBL");
32285 amit.gupta 334
        requestParams.put("send_to", sendTo);
32253 amit.gupta 335
        requestParams.put("v", "1.1");
336
        requestParams.put("format", "json");
337
        requestParams.put("auth_scheme", "plain");
32777 amit.gupta 338
        Optin optin = optinRepository.selectByMobile(mobile);
339
        if (optin == null) {
340
            this.optIn(sendTo);
341
            optin = new Optin();
342
            optin.setCreated(LocalDateTime.now());
343
            optin.setMobile(mobile);
344
            optinRepository.persist(optin);
345
        }
32253 amit.gupta 346
        if (mediaUrl == null) {
32415 amit.gupta 347
            requestParams.put("method", "SENDMESSAGE");
32854 amit.gupta 348
            requestParams.put("msg_type", whatsappMessageType.name());
32253 amit.gupta 349
            requestParams.put("msg", message);
34522 ranu 350
            requestParams.put("isTemplate", "true");
351
            requestParams.put("header", title);
32253 amit.gupta 352
        } else if (mediaUrl != null) {
32268 amit.gupta 353
            requestParams.put("method", "SENDMEDIAMESSAGE");
32854 amit.gupta 354
            requestParams.put("msg_type", whatsappMessageType.name());
32253 amit.gupta 355
            requestParams.put("caption", message);
356
            requestParams.put("media_url", mediaUrl);
357
            requestParams.put("filename", fileName);
32777 amit.gupta 358
        }
34545 vikas.jang 359
        String response = restClient.post("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
32777 amit.gupta 360
        LOGGER.info("response  - {}", response);
32403 tejbeer 361
 
32777 amit.gupta 362
        JSONObject jsonObject = new JSONObject(response);
32401 tejbeer 363
 
32777 amit.gupta 364
        JSONObject whatsappResponse = (JSONObject) jsonObject.get("response");
32874 amit.gupta 365
        if (whatsappResponse.getString("status").equals("error")) {
366
            LOGGER.error("Invalid Whatsapp message, Reason - {}", whatsappResponse.getString("details"));
34204 tejus.loha 367
            return isSend;
368
        }else{
369
            isSend=true;
32850 amit.gupta 370
        }
32777 amit.gupta 371
        String externalId = whatsappResponse.getString("id");
372
        String phone = whatsappResponse.getString("phone");
32403 tejbeer 373
 
32777 amit.gupta 374
        WhatsappMessage whatsappMessage = new WhatsappMessage();
375
        whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
376
        whatsappMessage.setExternalId(externalId);
377
        whatsappMessage.setDestAddr(phone);
378
        whatsappMessageRepository.persist(whatsappMessage);
34204 tejus.loha 379
        return isSend;
32253 amit.gupta 380
    }
30025 amit.gupta 381
 
32405 jai.hind 382
    @Override
33415 amit.gupta 383
    public boolean shouldSendWhatsappMessage(String mobile) {
32405 jai.hind 384
        String destAddr = "91" + mobile;
33415 amit.gupta 385
        boolean shouldSend = true;
32508 amit.gupta 386
        List<WhatsappMessage> whatsappMessages = whatsappMessageRepository.selectByDestAddr(destAddr, LocalDate.now());
32405 jai.hind 387
        if (!whatsappMessages.isEmpty()) {
32508 amit.gupta 388
            long failedCount = whatsappMessages.stream().filter(x -> x.getFailed() != null && x.getFailed().equals("FAILED")).collect(Collectors.counting());
32405 jai.hind 389
            if (failedCount >= 2) {
33415 amit.gupta 390
                shouldSend = false;
32405 jai.hind 391
            }
392
        }
33415 amit.gupta 393
        return shouldSend;
32405 jai.hind 394
    }
33715 ranu 395
 
396
 
397
    @Override
398
    public void sendPaymentWhatsappMessage(String mobile, String message) throws Exception {
36962 vikas 399
        if (isCpass()) {
400
            cpassWhatsappService.sendText(mobile, message);
401
            return;
402
        }
33968 ranu 403
        String sendTo = "91" + mobile;
404
//        String sendTo = "917082253510";
33715 ranu 405
 
406
        Map<String, String> requestHeaders = new HashMap<>();
407
        requestHeaders.put("Content-Type", "application/x-www-form-urlencoded");
408
        Map<String, String> requestParams = new HashMap<>();
409
        requestParams.put("userid", String.valueOf(2000215976));
410
        requestParams.put("password", "MFRd!BBL");
411
        requestParams.put("send_to", sendTo);
412
        requestParams.put("v", "1.1");
413
        requestParams.put("format", "json");
414
        requestParams.put("auth_scheme", "plain");
415
        requestParams.put("method", "SENDMESSAGE");
416
        requestParams.put("msg_type", "TEXT");
417
        requestParams.put("msg", message);
418
        requestParams.put("isTemplate", "true");
419
        requestParams.put("header", "Payment Link!");
420
 
421
        String response = restClient.post("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestHeaders);
422
        LOGGER.info("response  - {}", response);
423
 
424
        JSONObject jsonObject = new JSONObject(response);
425
        JSONObject whatsappResponse = jsonObject.getJSONObject("response");
426
        if (whatsappResponse.getString("status").equals("error")) {
427
            LOGGER.error("Invalid Whatsapp message, Reason - {}", whatsappResponse.getString("details"));
428
            return;
429
        }
430
 
431
        String externalId = whatsappResponse.getString("id");
432
        String phone = whatsappResponse.getString("phone");
433
 
434
        WhatsappMessage whatsappMessage = new WhatsappMessage();
435
        whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
436
        whatsappMessage.setExternalId(externalId);
437
        whatsappMessage.setDestAddr(phone);
438
        whatsappMessageRepository.persist(whatsappMessage);
439
    }
34545 vikas.jang 440
 
25822 amit.gupta 441
}