| 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.*;
|
| 25822 |
amit.gupta |
11 |
import com.spice.profitmandi.dao.entity.user.Device;
|
| 32405 |
jai.hind |
12 |
import com.spice.profitmandi.dao.entity.whatsapp.WhatsappMessage;
|
| 25822 |
amit.gupta |
13 |
import com.spice.profitmandi.dao.model.SimpleCampaign;
|
|
|
14 |
import com.spice.profitmandi.dao.model.SimpleCampaignParams;
|
|
|
15 |
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
|
| 29927 |
amit.gupta |
16 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
| 35207 |
amit |
17 |
import com.spice.profitmandi.dao.repository.cs.PartnerRegionRepository;
|
| 32405 |
jai.hind |
18 |
import com.spice.profitmandi.dao.repository.dtr.*;
|
|
|
19 |
import com.spice.profitmandi.dao.repository.whatsapp.WhatsappMessageRepository;
|
| 29198 |
manish |
20 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 32405 |
jai.hind |
21 |
import com.spice.profitmandi.service.whatsapp.WhatsappMessageService;
|
| 32854 |
amit.gupta |
22 |
import com.spice.profitmandi.service.whatsapp.WhatsappMessageType;
|
| 32405 |
jai.hind |
23 |
import org.apache.logging.log4j.LogManager;
|
|
|
24 |
import org.apache.logging.log4j.Logger;
|
| 34545 |
vikas.jang |
25 |
import org.json.JSONArray;
|
| 32405 |
jai.hind |
26 |
import org.json.JSONObject;
|
|
|
27 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 34522 |
ranu |
28 |
import org.springframework.beans.factory.annotation.Value;
|
| 32405 |
jai.hind |
29 |
import org.springframework.stereotype.Component;
|
| 25822 |
amit.gupta |
30 |
|
| 34545 |
vikas.jang |
31 |
import java.io.BufferedReader;
|
|
|
32 |
import java.io.InputStreamReader;
|
|
|
33 |
import java.io.OutputStream;
|
|
|
34 |
import java.net.HttpURLConnection;
|
|
|
35 |
import java.net.URL;
|
|
|
36 |
import java.nio.charset.StandardCharsets;
|
| 32508 |
amit.gupta |
37 |
import java.time.LocalDate;
|
| 32405 |
jai.hind |
38 |
import java.time.LocalDateTime;
|
|
|
39 |
import java.util.*;
|
|
|
40 |
import java.util.stream.Collectors;
|
|
|
41 |
|
| 25835 |
amit.gupta |
42 |
@Component
|
| 25822 |
amit.gupta |
43 |
public class NotificationServiceImpl implements NotificationService {
|
|
|
44 |
|
| 35236 |
amit |
45 |
private static final Logger LOGGER = LogManager.getLogger(NotificationServiceImpl.class);
|
| 32253 |
amit.gupta |
46 |
@Autowired
|
|
|
47 |
UserCampaignRepository userCampaignRepository;
|
|
|
48 |
@Autowired
|
|
|
49 |
UserRepository dtrUserRepository;
|
|
|
50 |
@Autowired
|
|
|
51 |
UserAccountRepository userAccountRepository;
|
|
|
52 |
@Autowired
|
|
|
53 |
NotificationCampaignRepository notificationCampaignRepository;
|
|
|
54 |
@Autowired
|
|
|
55 |
DeviceRepository deviceRepository;
|
| 35207 |
amit |
56 |
|
| 32253 |
amit.gupta |
57 |
@Autowired
|
| 35207 |
amit |
58 |
PartnerRegionRepository partnerRegionRepository;
|
|
|
59 |
|
|
|
60 |
|
|
|
61 |
@Autowired
|
| 32253 |
amit.gupta |
62 |
FofoStoreRepository fofoStoreRepository;
|
|
|
63 |
@Autowired
|
|
|
64 |
CsService csService;
|
|
|
65 |
@Autowired
|
|
|
66 |
OptinRepository optinRepository;
|
|
|
67 |
@Autowired
|
|
|
68 |
RetailerService retailerService;
|
|
|
69 |
@Autowired
|
|
|
70 |
PushNotificationRepository pushNotificationRepository;
|
|
|
71 |
@Autowired
|
|
|
72 |
private Gson gson;
|
|
|
73 |
@Autowired
|
|
|
74 |
private RestClient restClient;
|
| 34522 |
ranu |
75 |
@Value("${prod}")
|
|
|
76 |
private boolean isProd;
|
| 25822 |
amit.gupta |
77 |
|
| 32405 |
jai.hind |
78 |
@Autowired
|
| 32417 |
amit.gupta |
79 |
private WhatsappMessageRepository whatsappMessageRepository;
|
| 32405 |
jai.hind |
80 |
|
|
|
81 |
@Autowired
|
|
|
82 |
private WhatsappMessageService whatsappMessageService;
|
|
|
83 |
|
| 32253 |
amit.gupta |
84 |
@Override
|
|
|
85 |
public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
|
| 29927 |
amit.gupta |
86 |
|
| 32253 |
amit.gupta |
87 |
SimpleCampaignParams scp = new SimpleCampaignParams();
|
|
|
88 |
scp.setMessage(sendNotificationModel.getMessage());
|
|
|
89 |
scp.setTitle(sendNotificationModel.getTitle());
|
|
|
90 |
scp.setImageUrl(sendNotificationModel.getImageUrl());
|
|
|
91 |
scp.setType(sendNotificationModel.getType());
|
|
|
92 |
scp.setUrl(sendNotificationModel.getUrl());
|
|
|
93 |
scp.setShowImage(sendNotificationModel.getShowImage());
|
|
|
94 |
scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
|
|
|
95 |
SimpleCampaign sc = new SimpleCampaign(scp);
|
|
|
96 |
sc.setSimpleCampaignParams(scp);
|
| 30025 |
amit.gupta |
97 |
|
| 32253 |
amit.gupta |
98 |
NotificationCampaign nc = new NotificationCampaign();
|
|
|
99 |
nc.setName(sendNotificationModel.getCampaignName());
|
|
|
100 |
nc.setImplementationType("SimpleCampaign");
|
|
|
101 |
nc.setImplementationParams(gson.toJson(scp));
|
|
|
102 |
nc.setMessageType(sendNotificationModel.getMessageType());
|
|
|
103 |
nc.setDocumentId(sendNotificationModel.getDocumentId());
|
|
|
104 |
nc.setCreatedTimestamp(LocalDateTime.now());
|
|
|
105 |
notificationCampaignRepository.persist(nc);
|
| 25822 |
amit.gupta |
106 |
|
| 32253 |
amit.gupta |
107 |
Set<Integer> userIds = new HashSet<>();
|
|
|
108 |
if (sendNotificationModel.getUserIds() != null && sendNotificationModel.getUserIds().size() > 0) {
|
|
|
109 |
userIds.addAll(sendNotificationModel.getUserIds());
|
|
|
110 |
}
|
| 29198 |
manish |
111 |
|
| 32253 |
amit.gupta |
112 |
if (sendNotificationModel.getStateIds() != null && sendNotificationModel.getStateIds().size() > 0) {
|
| 33025 |
amit.gupta |
113 |
List<Integer> fofoIds = fofoStoreRepository.selectByWarehouseIds(sendNotificationModel.getStateIds()).stream()
|
|
|
114 |
.map(x -> x.getId()).collect(Collectors.toList());
|
| 34204 |
tejus.loha |
115 |
if (fofoIds.size() > 0) {
|
| 33025 |
amit.gupta |
116 |
userIds.addAll(userAccountRepository.selectUserIdsByRetailerIds(fofoIds));
|
|
|
117 |
}
|
|
|
118 |
|
| 32253 |
amit.gupta |
119 |
}
|
| 35207 |
amit |
120 |
if(sendNotificationModel.getRegionIds() != null && sendNotificationModel.getRegionIds().size() > 0) {
|
|
|
121 |
List<Integer> fofoIds = partnerRegionRepository.selectAllByRegionIds(sendNotificationModel.getRegionIds()).stream().map(x->x.getFofoId()).collect(Collectors.toList());
|
|
|
122 |
if(fofoIds.size()>0) {
|
|
|
123 |
userIds.addAll(userAccountRepository.selectUserIdsByRetailerIds(fofoIds));
|
|
|
124 |
}
|
|
|
125 |
}
|
| 29927 |
amit.gupta |
126 |
|
| 34204 |
tejus.loha |
127 |
if (userIds.size() > 0) {
|
| 33025 |
amit.gupta |
128 |
for (Integer userId : userIds) {
|
|
|
129 |
UserCampaign uc = new UserCampaign();
|
|
|
130 |
uc.setCampaignId(nc.getId());
|
|
|
131 |
uc.setUserId(userId);
|
|
|
132 |
uc.setPushTimestamp(LocalDateTime.now());
|
|
|
133 |
userCampaignRepository.persist(uc);
|
|
|
134 |
}
|
|
|
135 |
List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(new ArrayList<>(userIds),
|
|
|
136 |
LocalDateTime.now().minusMonths(1), LocalDateTime.now());
|
|
|
137 |
pushNotification(nc.getId(), devices);
|
|
|
138 |
} else {
|
|
|
139 |
LOGGER.info("Failed to send notification to any retailer with this model - {}", sendNotificationModel);
|
| 32253 |
amit.gupta |
140 |
}
|
| 25822 |
amit.gupta |
141 |
|
| 32253 |
amit.gupta |
142 |
}
|
| 25822 |
amit.gupta |
143 |
|
| 32253 |
amit.gupta |
144 |
@Override
|
|
|
145 |
public void sendNotificationToAll(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
|
|
|
146 |
sendNotificationModel.setUserIds(fofoStoreRepository.selectAllDtrUserIds());
|
|
|
147 |
Set<AuthUser> authUsers = new HashSet<>(
|
|
|
148 |
csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
|
|
|
149 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));
|
|
|
150 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));
|
| 34903 |
ranu |
151 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_ABM));
|
| 32253 |
amit.gupta |
152 |
List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
|
|
|
153 |
emailIds.add("devkinandan.lal@smartdukaan.com");
|
|
|
154 |
List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);
|
| 32405 |
jai.hind |
155 |
sendNotificationModel.getUserIds()
|
|
|
156 |
.addAll(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));
|
| 32253 |
amit.gupta |
157 |
this.sendNotification(sendNotificationModel);
|
|
|
158 |
}
|
| 25822 |
amit.gupta |
159 |
|
| 32253 |
amit.gupta |
160 |
@Override
|
| 34550 |
vikas.jang |
161 |
public void sendNotificationToSystemUsers(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
|
|
|
162 |
Set<AuthUser> authUsers = new HashSet<>(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
|
|
|
163 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));
|
|
|
164 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));
|
| 34903 |
ranu |
165 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_ABM));
|
| 34550 |
vikas.jang |
166 |
List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
|
|
|
167 |
emailIds.add("devkinandan.lal@smartdukaan.com");
|
|
|
168 |
List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);
|
|
|
169 |
sendNotificationModel.setUserIds(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));
|
|
|
170 |
this.sendNotification(sendNotificationModel);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
@Override
|
| 32405 |
jai.hind |
174 |
public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message)
|
|
|
175 |
throws ProfitMandiBusinessException {
|
| 32253 |
amit.gupta |
176 |
SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
|
|
|
177 |
sendNotificationModel.setCampaignName(campaignName);
|
|
|
178 |
sendNotificationModel.setMessageType(messageType);
|
|
|
179 |
sendNotificationModel.setTitle(title);
|
|
|
180 |
sendNotificationModel.setMessage(message);
|
|
|
181 |
int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
|
|
|
182 |
sendNotificationModel.setUserIds(Arrays.asList(userId));
|
|
|
183 |
sendNotificationModel.setMessageType(MessageType.wallet);
|
|
|
184 |
this.sendNotification(sendNotificationModel);
|
|
|
185 |
}
|
| 25822 |
amit.gupta |
186 |
|
| 32253 |
amit.gupta |
187 |
public SendNotificationModel getDefaultNotificationModel() {
|
|
|
188 |
SendNotificationModel sendNotificationModel = new SendNotificationModel();
|
|
|
189 |
sendNotificationModel.setType("url");
|
| 34902 |
vikas |
190 |
sendNotificationModel.setUrl("https://smartdukaan.com/pages/home/notifications");
|
| 32253 |
amit.gupta |
191 |
sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
|
|
|
192 |
sendNotificationModel.setMessageType(MessageType.notification);
|
|
|
193 |
return sendNotificationModel;
|
|
|
194 |
}
|
| 25822 |
amit.gupta |
195 |
|
| 32253 |
amit.gupta |
196 |
public void pushNotification(int cid, List<Device> devices) {
|
| 29198 |
manish |
197 |
|
| 32253 |
amit.gupta |
198 |
for (Device device : devices) {
|
|
|
199 |
PushNotifications pn = new PushNotifications();
|
|
|
200 |
pn.setNotificationCampaignid(cid);
|
|
|
201 |
pn.setDeviceId(device.getId());
|
|
|
202 |
pn.setUserId(device.getUser_id());
|
|
|
203 |
pushNotificationRepository.persist(pn);
|
|
|
204 |
}
|
| 29198 |
manish |
205 |
|
| 32253 |
amit.gupta |
206 |
}
|
| 29198 |
manish |
207 |
|
| 32253 |
amit.gupta |
208 |
@Override
|
| 34545 |
vikas.jang |
209 |
public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message, String url) throws ProfitMandiBusinessException {
|
| 32253 |
amit.gupta |
210 |
SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
|
|
|
211 |
sendNotificationModel.setCampaignName(campaignName);
|
|
|
212 |
sendNotificationModel.setMessageType(messageType);
|
|
|
213 |
sendNotificationModel.setTitle(title);
|
|
|
214 |
sendNotificationModel.setMessage(message);
|
|
|
215 |
int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
|
|
|
216 |
sendNotificationModel.setUserIds(Arrays.asList(userId));
|
|
|
217 |
sendNotificationModel.setMessageType(messageType);
|
|
|
218 |
sendNotificationModel.setUrl(url);
|
|
|
219 |
this.sendNotification(sendNotificationModel);
|
|
|
220 |
}
|
| 29198 |
manish |
221 |
|
| 32253 |
amit.gupta |
222 |
@Override
|
| 34545 |
vikas.jang |
223 |
public boolean sendWhatsappMessage(String message, String title, String mobile) throws Exception {
|
| 34204 |
tejus.loha |
224 |
boolean isSend=false;
|
| 34520 |
amit.gupta |
225 |
LOGGER.info("Is Prod - {}", isProd);
|
| 32874 |
amit.gupta |
226 |
if (isProd) {
|
| 34204 |
tejus.loha |
227 |
isSend=this.sendWhatsappMessage(WhatsappMessageType.TEXT, message, title, mobile, null, null);
|
| 32874 |
amit.gupta |
228 |
}
|
| 34204 |
tejus.loha |
229 |
return isSend;
|
| 32253 |
amit.gupta |
230 |
}
|
| 25822 |
amit.gupta |
231 |
|
| 32253 |
amit.gupta |
232 |
@Override
|
| 34545 |
vikas.jang |
233 |
public boolean sendWhatsappMediaMessage(String message, String mobile, String mediaUrl, String fileName, WhatsappMessageType whatsappMessageType) throws Exception {
|
| 34204 |
tejus.loha |
234 |
boolean isSend=false;
|
| 33025 |
amit.gupta |
235 |
if (isProd) {
|
| 34204 |
tejus.loha |
236 |
isSend=this.sendWhatsappMessage(whatsappMessageType, message, null, mobile, mediaUrl, fileName);
|
| 32874 |
amit.gupta |
237 |
}
|
| 34204 |
tejus.loha |
238 |
return isSend;
|
| 32253 |
amit.gupta |
239 |
}
|
| 25822 |
amit.gupta |
240 |
|
| 32253 |
amit.gupta |
241 |
@Override
|
|
|
242 |
public void optIn(String phoneNumber) throws Exception {
|
|
|
243 |
Map<String, String> requestheaders = new HashMap<>();
|
|
|
244 |
requestheaders.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
245 |
Map<String, String> requestParams = new HashMap<>();
|
|
|
246 |
requestParams.put("userid", String.valueOf(2000215976));
|
|
|
247 |
requestParams.put("password", "MFRd!BBL");
|
|
|
248 |
requestParams.put("phone_number", phoneNumber);
|
|
|
249 |
requestParams.put("auth_scheme", "plain");
|
|
|
250 |
requestParams.put("v", "1.1");
|
|
|
251 |
requestParams.put("format", "json");
|
| 28400 |
tejbeer |
252 |
|
| 32253 |
amit.gupta |
253 |
requestParams.put("method", "OPT_IN");
|
| 25822 |
amit.gupta |
254 |
|
| 32253 |
amit.gupta |
255 |
requestParams.put("channel", "WHATSAPP");
|
|
|
256 |
String response = restClient.get("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
|
|
|
257 |
LOGGER.info("response" + response);
|
|
|
258 |
}
|
| 29927 |
amit.gupta |
259 |
|
| 34204 |
tejus.loha |
260 |
private boolean sendWhatsappMessage(WhatsappMessageType whatsappMessageType, String message, String title, String mobile, String mediaUrl, String fileName)
|
| 32405 |
jai.hind |
261 |
throws Exception {
|
| 32260 |
amit.gupta |
262 |
String sendTo = null;
|
| 34204 |
tejus.loha |
263 |
boolean isSend=false;
|
| 32259 |
amit.gupta |
264 |
if (mobile.length() != 10) {
|
| 34204 |
tejus.loha |
265 |
return isSend;
|
| 32259 |
amit.gupta |
266 |
} else {
|
| 32260 |
amit.gupta |
267 |
sendTo = 91 + mobile;
|
| 32259 |
amit.gupta |
268 |
}
|
| 32253 |
amit.gupta |
269 |
Map<String, String> requestheaders = new HashMap<>();
|
|
|
270 |
requestheaders.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
271 |
Map<String, String> requestParams = new HashMap<>();
|
|
|
272 |
requestParams.put("userid", String.valueOf(2000215976));
|
|
|
273 |
requestParams.put("password", "MFRd!BBL");
|
| 32285 |
amit.gupta |
274 |
requestParams.put("send_to", sendTo);
|
| 32253 |
amit.gupta |
275 |
requestParams.put("v", "1.1");
|
|
|
276 |
requestParams.put("format", "json");
|
|
|
277 |
requestParams.put("auth_scheme", "plain");
|
| 32777 |
amit.gupta |
278 |
Optin optin = optinRepository.selectByMobile(mobile);
|
|
|
279 |
if (optin == null) {
|
|
|
280 |
this.optIn(sendTo);
|
|
|
281 |
optin = new Optin();
|
|
|
282 |
optin.setCreated(LocalDateTime.now());
|
|
|
283 |
optin.setMobile(mobile);
|
|
|
284 |
optinRepository.persist(optin);
|
|
|
285 |
}
|
| 32253 |
amit.gupta |
286 |
if (mediaUrl == null) {
|
| 32415 |
amit.gupta |
287 |
requestParams.put("method", "SENDMESSAGE");
|
| 32854 |
amit.gupta |
288 |
requestParams.put("msg_type", whatsappMessageType.name());
|
| 32253 |
amit.gupta |
289 |
requestParams.put("msg", message);
|
| 34522 |
ranu |
290 |
requestParams.put("isTemplate", "true");
|
|
|
291 |
requestParams.put("header", title);
|
| 32253 |
amit.gupta |
292 |
} else if (mediaUrl != null) {
|
| 32268 |
amit.gupta |
293 |
requestParams.put("method", "SENDMEDIAMESSAGE");
|
| 32854 |
amit.gupta |
294 |
requestParams.put("msg_type", whatsappMessageType.name());
|
| 32253 |
amit.gupta |
295 |
requestParams.put("caption", message);
|
|
|
296 |
requestParams.put("media_url", mediaUrl);
|
|
|
297 |
requestParams.put("filename", fileName);
|
| 32777 |
amit.gupta |
298 |
}
|
| 34545 |
vikas.jang |
299 |
String response = restClient.post("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
|
| 32777 |
amit.gupta |
300 |
LOGGER.info("response - {}", response);
|
| 32403 |
tejbeer |
301 |
|
| 32777 |
amit.gupta |
302 |
JSONObject jsonObject = new JSONObject(response);
|
| 32401 |
tejbeer |
303 |
|
| 32777 |
amit.gupta |
304 |
JSONObject whatsappResponse = (JSONObject) jsonObject.get("response");
|
| 32874 |
amit.gupta |
305 |
if (whatsappResponse.getString("status").equals("error")) {
|
|
|
306 |
LOGGER.error("Invalid Whatsapp message, Reason - {}", whatsappResponse.getString("details"));
|
| 34204 |
tejus.loha |
307 |
return isSend;
|
|
|
308 |
}else{
|
|
|
309 |
isSend=true;
|
| 32850 |
amit.gupta |
310 |
}
|
| 32777 |
amit.gupta |
311 |
String externalId = whatsappResponse.getString("id");
|
|
|
312 |
String phone = whatsappResponse.getString("phone");
|
| 32403 |
tejbeer |
313 |
|
| 32777 |
amit.gupta |
314 |
WhatsappMessage whatsappMessage = new WhatsappMessage();
|
|
|
315 |
whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
|
|
|
316 |
whatsappMessage.setExternalId(externalId);
|
|
|
317 |
whatsappMessage.setDestAddr(phone);
|
|
|
318 |
whatsappMessageRepository.persist(whatsappMessage);
|
| 34204 |
tejus.loha |
319 |
return isSend;
|
| 32253 |
amit.gupta |
320 |
}
|
| 30025 |
amit.gupta |
321 |
|
| 32405 |
jai.hind |
322 |
@Override
|
| 33415 |
amit.gupta |
323 |
public boolean shouldSendWhatsappMessage(String mobile) {
|
| 32405 |
jai.hind |
324 |
String destAddr = "91" + mobile;
|
| 33415 |
amit.gupta |
325 |
boolean shouldSend = true;
|
| 32508 |
amit.gupta |
326 |
List<WhatsappMessage> whatsappMessages = whatsappMessageRepository.selectByDestAddr(destAddr, LocalDate.now());
|
| 32405 |
jai.hind |
327 |
if (!whatsappMessages.isEmpty()) {
|
| 32508 |
amit.gupta |
328 |
long failedCount = whatsappMessages.stream().filter(x -> x.getFailed() != null && x.getFailed().equals("FAILED")).collect(Collectors.counting());
|
| 32405 |
jai.hind |
329 |
if (failedCount >= 2) {
|
| 33415 |
amit.gupta |
330 |
shouldSend = false;
|
| 32405 |
jai.hind |
331 |
}
|
|
|
332 |
}
|
| 33415 |
amit.gupta |
333 |
return shouldSend;
|
| 32405 |
jai.hind |
334 |
}
|
| 33715 |
ranu |
335 |
|
|
|
336 |
|
|
|
337 |
@Override
|
|
|
338 |
public void sendPaymentWhatsappMessage(String mobile, String message) throws Exception {
|
| 33968 |
ranu |
339 |
String sendTo = "91" + mobile;
|
|
|
340 |
// String sendTo = "917082253510";
|
| 33715 |
ranu |
341 |
|
|
|
342 |
Map<String, String> requestHeaders = new HashMap<>();
|
|
|
343 |
requestHeaders.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
344 |
Map<String, String> requestParams = new HashMap<>();
|
|
|
345 |
requestParams.put("userid", String.valueOf(2000215976));
|
|
|
346 |
requestParams.put("password", "MFRd!BBL");
|
|
|
347 |
requestParams.put("send_to", sendTo);
|
|
|
348 |
requestParams.put("v", "1.1");
|
|
|
349 |
requestParams.put("format", "json");
|
|
|
350 |
requestParams.put("auth_scheme", "plain");
|
|
|
351 |
requestParams.put("method", "SENDMESSAGE");
|
|
|
352 |
requestParams.put("msg_type", "TEXT");
|
|
|
353 |
requestParams.put("msg", message);
|
|
|
354 |
requestParams.put("isTemplate", "true");
|
|
|
355 |
requestParams.put("header", "Payment Link!");
|
|
|
356 |
|
|
|
357 |
String response = restClient.post("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestHeaders);
|
|
|
358 |
LOGGER.info("response - {}", response);
|
|
|
359 |
|
|
|
360 |
JSONObject jsonObject = new JSONObject(response);
|
|
|
361 |
JSONObject whatsappResponse = jsonObject.getJSONObject("response");
|
|
|
362 |
if (whatsappResponse.getString("status").equals("error")) {
|
|
|
363 |
LOGGER.error("Invalid Whatsapp message, Reason - {}", whatsappResponse.getString("details"));
|
|
|
364 |
return;
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
String externalId = whatsappResponse.getString("id");
|
|
|
368 |
String phone = whatsappResponse.getString("phone");
|
|
|
369 |
|
|
|
370 |
WhatsappMessage whatsappMessage = new WhatsappMessage();
|
|
|
371 |
whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
|
|
|
372 |
whatsappMessage.setExternalId(externalId);
|
|
|
373 |
whatsappMessage.setDestAddr(phone);
|
|
|
374 |
whatsappMessageRepository.persist(whatsappMessage);
|
|
|
375 |
}
|
| 34545 |
vikas.jang |
376 |
|
|
|
377 |
private boolean sendWhatsAppMessageByMeta(WhatsappMessageType whatsappMessageType, String message, String title, String mobile, String mediaUrl, String fileName) throws Exception {
|
|
|
378 |
|
|
|
379 |
mobile = this.optInUsersByMobiles(mobile);
|
|
|
380 |
|
|
|
381 |
String token = "###############";
|
|
|
382 |
String phoneNumberId = "000000000000000000";
|
|
|
383 |
String url = "https://graph.facebook.com/v22.0/" + phoneNumberId + "/messages";
|
|
|
384 |
|
|
|
385 |
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
|
|
|
386 |
conn.setRequestMethod("POST");
|
|
|
387 |
conn.setRequestProperty("Authorization", "Bearer " + token);
|
|
|
388 |
conn.setRequestProperty("Content-Type", "application/json");
|
|
|
389 |
conn.setDoOutput(true);
|
|
|
390 |
|
|
|
391 |
// Construct the payload
|
|
|
392 |
JSONObject payload = new JSONObject();
|
|
|
393 |
payload.put("messaging_product", "whatsapp");
|
|
|
394 |
payload.put("to", "918529003611");
|
|
|
395 |
|
|
|
396 |
if (mediaUrl != null) {
|
|
|
397 |
JSONObject image = new JSONObject();
|
|
|
398 |
image.put("link", mediaUrl);
|
|
|
399 |
if (fileName != null) image.put("caption", message);
|
|
|
400 |
payload.put("type", "image");
|
|
|
401 |
payload.put("image", image);
|
|
|
402 |
} else {
|
|
|
403 |
payload.put("type", "template");
|
|
|
404 |
JSONObject template = new JSONObject();
|
|
|
405 |
template.put("name", "bid_live");
|
|
|
406 |
JSONObject language = new JSONObject();
|
|
|
407 |
language.put("code", "en_US");
|
|
|
408 |
template.put("language", language);
|
|
|
409 |
JSONArray components = new JSONArray();
|
|
|
410 |
JSONObject body = new JSONObject();
|
|
|
411 |
body.put("type", "body");
|
|
|
412 |
|
|
|
413 |
JSONArray parameters = new JSONArray();
|
|
|
414 |
parameters.put(new JSONObject().put("type", "text").put("text", "*Oppo Reno 10 Pro 5G (12GB 256GB) Silvery Grey* at 54.55% Off"));
|
|
|
415 |
parameters.put(new JSONObject().put("type", "text").put("text", "44000.0"));
|
|
|
416 |
parameters.put(new JSONObject().put("type", "text").put("text", "20000.0"));
|
|
|
417 |
parameters.put(new JSONObject().put("type", "text").put("text", "05/05/2025 06:00PM"));
|
|
|
418 |
body.put("parameters", parameters);
|
|
|
419 |
components.put(body);
|
|
|
420 |
|
|
|
421 |
template.put("components", components);
|
|
|
422 |
payload.put("template", template);
|
|
|
423 |
}
|
|
|
424 |
LOGGER.info("Final WhatsApp payload: \n{}", payload.toString(2));
|
|
|
425 |
try (OutputStream os = conn.getOutputStream()) {
|
|
|
426 |
os.write(payload.toString().getBytes(StandardCharsets.UTF_8));
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
int status = conn.getResponseCode();
|
|
|
430 |
String response = new BufferedReader(new InputStreamReader(status >= 400 ? conn.getErrorStream() : conn.getInputStream())).lines().collect(Collectors.joining("\n"));
|
|
|
431 |
|
|
|
432 |
LOGGER.info("WhatsApp API response: {}", response);
|
|
|
433 |
|
|
|
434 |
if (status != 200 && status != 201) {
|
|
|
435 |
LOGGER.error("Failed to send WhatsApp message. Status: {}, Response: {}", status, response);
|
|
|
436 |
return false;
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
JSONObject jsonResponse = new JSONObject(response);
|
|
|
440 |
JSONArray messages = jsonResponse.optJSONArray("messages");
|
|
|
441 |
String externalId = null;
|
|
|
442 |
|
|
|
443 |
if (messages != null && messages.length() > 0) {
|
|
|
444 |
externalId = messages.getJSONObject(0).optString("id");
|
|
|
445 |
WhatsappMessage whatsappMessage = new WhatsappMessage();
|
|
|
446 |
whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
|
|
|
447 |
whatsappMessage.setExternalId(externalId);
|
|
|
448 |
whatsappMessage.setDestAddr(mobile);
|
|
|
449 |
whatsappMessageRepository.persist(whatsappMessage);
|
|
|
450 |
}
|
|
|
451 |
|
|
|
452 |
return externalId != null;
|
|
|
453 |
}
|
|
|
454 |
|
|
|
455 |
private String optInUsersByMobiles(String mobiles) throws Exception {
|
|
|
456 |
List<String> savedMobiles = new ArrayList<>();
|
|
|
457 |
if (mobiles == null || mobiles.isEmpty()) return String.join(",",savedMobiles);
|
|
|
458 |
String[] mobileNumbers = mobiles.split(",");
|
|
|
459 |
for (String mobile : mobileNumbers) {
|
|
|
460 |
mobile = mobile.trim();
|
|
|
461 |
if (mobile.isEmpty()) continue;
|
|
|
462 |
Optin optin = optinRepository.selectByMobile(mobile);
|
|
|
463 |
if (optin == null) {
|
|
|
464 |
this.optIn(mobile);
|
|
|
465 |
optin = new Optin();
|
|
|
466 |
optin.setCreated(LocalDateTime.now());
|
|
|
467 |
optin.setMobile(mobile);
|
|
|
468 |
optinRepository.persist(optin);
|
|
|
469 |
}
|
|
|
470 |
savedMobiles.add("91"+mobile);
|
|
|
471 |
}
|
|
|
472 |
return String.join(",",savedMobiles);
|
|
|
473 |
}
|
| 25822 |
amit.gupta |
474 |
}
|