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