| 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;
|
| 32405 |
jai.hind |
17 |
import com.spice.profitmandi.dao.repository.dtr.*;
|
|
|
18 |
import com.spice.profitmandi.dao.repository.whatsapp.WhatsappMessageRepository;
|
| 29198 |
manish |
19 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 32405 |
jai.hind |
20 |
import com.spice.profitmandi.service.whatsapp.WhatsappMessageService;
|
| 32854 |
amit.gupta |
21 |
import com.spice.profitmandi.service.whatsapp.WhatsappMessageType;
|
| 32405 |
jai.hind |
22 |
import org.apache.logging.log4j.LogManager;
|
|
|
23 |
import org.apache.logging.log4j.Logger;
|
| 34545 |
vikas.jang |
24 |
import org.json.JSONArray;
|
| 32405 |
jai.hind |
25 |
import org.json.JSONObject;
|
|
|
26 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 34522 |
ranu |
27 |
import org.springframework.beans.factory.annotation.Value;
|
| 32405 |
jai.hind |
28 |
import org.springframework.stereotype.Component;
|
| 25822 |
amit.gupta |
29 |
|
| 34545 |
vikas.jang |
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;
|
| 32508 |
amit.gupta |
36 |
import java.time.LocalDate;
|
| 32405 |
jai.hind |
37 |
import java.time.LocalDateTime;
|
|
|
38 |
import java.util.*;
|
|
|
39 |
import java.util.stream.Collectors;
|
|
|
40 |
|
| 25835 |
amit.gupta |
41 |
@Component
|
| 25822 |
amit.gupta |
42 |
public class NotificationServiceImpl implements NotificationService {
|
|
|
43 |
|
| 32253 |
amit.gupta |
44 |
private static final Logger LOGGER = LogManager.getLogger(NotificationService.class);
|
|
|
45 |
@Autowired
|
|
|
46 |
UserCampaignRepository userCampaignRepository;
|
|
|
47 |
@Autowired
|
|
|
48 |
UserRepository dtrUserRepository;
|
|
|
49 |
@Autowired
|
|
|
50 |
UserAccountRepository userAccountRepository;
|
|
|
51 |
@Autowired
|
|
|
52 |
NotificationCampaignRepository notificationCampaignRepository;
|
|
|
53 |
@Autowired
|
|
|
54 |
DeviceRepository deviceRepository;
|
|
|
55 |
@Autowired
|
|
|
56 |
FofoStoreRepository fofoStoreRepository;
|
|
|
57 |
@Autowired
|
|
|
58 |
CsService csService;
|
|
|
59 |
@Autowired
|
|
|
60 |
OptinRepository optinRepository;
|
|
|
61 |
@Autowired
|
|
|
62 |
RetailerService retailerService;
|
|
|
63 |
@Autowired
|
|
|
64 |
PushNotificationRepository pushNotificationRepository;
|
|
|
65 |
@Autowired
|
|
|
66 |
private Gson gson;
|
|
|
67 |
@Autowired
|
|
|
68 |
private RestClient restClient;
|
| 34522 |
ranu |
69 |
@Value("${prod}")
|
|
|
70 |
private boolean isProd;
|
| 25822 |
amit.gupta |
71 |
|
| 32405 |
jai.hind |
72 |
@Autowired
|
| 32417 |
amit.gupta |
73 |
private WhatsappMessageRepository whatsappMessageRepository;
|
| 32405 |
jai.hind |
74 |
|
|
|
75 |
@Autowired
|
|
|
76 |
private WhatsappMessageService whatsappMessageService;
|
|
|
77 |
|
| 32253 |
amit.gupta |
78 |
@Override
|
|
|
79 |
public void sendNotification(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
|
| 29927 |
amit.gupta |
80 |
|
| 32253 |
amit.gupta |
81 |
SimpleCampaignParams scp = new SimpleCampaignParams();
|
|
|
82 |
scp.setMessage(sendNotificationModel.getMessage());
|
|
|
83 |
scp.setTitle(sendNotificationModel.getTitle());
|
|
|
84 |
scp.setImageUrl(sendNotificationModel.getImageUrl());
|
|
|
85 |
scp.setType(sendNotificationModel.getType());
|
|
|
86 |
scp.setUrl(sendNotificationModel.getUrl());
|
|
|
87 |
scp.setShowImage(sendNotificationModel.getShowImage());
|
|
|
88 |
scp.setExpireTimestamp(sendNotificationModel.getExpiresat());
|
|
|
89 |
SimpleCampaign sc = new SimpleCampaign(scp);
|
|
|
90 |
sc.setSimpleCampaignParams(scp);
|
| 30025 |
amit.gupta |
91 |
|
| 32253 |
amit.gupta |
92 |
NotificationCampaign nc = new NotificationCampaign();
|
|
|
93 |
nc.setName(sendNotificationModel.getCampaignName());
|
|
|
94 |
nc.setImplementationType("SimpleCampaign");
|
|
|
95 |
nc.setImplementationParams(gson.toJson(scp));
|
|
|
96 |
nc.setMessageType(sendNotificationModel.getMessageType());
|
|
|
97 |
nc.setDocumentId(sendNotificationModel.getDocumentId());
|
|
|
98 |
nc.setCreatedTimestamp(LocalDateTime.now());
|
|
|
99 |
notificationCampaignRepository.persist(nc);
|
| 25822 |
amit.gupta |
100 |
|
| 32253 |
amit.gupta |
101 |
Set<Integer> userIds = new HashSet<>();
|
|
|
102 |
if (sendNotificationModel.getUserIds() != null && sendNotificationModel.getUserIds().size() > 0) {
|
|
|
103 |
userIds.addAll(sendNotificationModel.getUserIds());
|
|
|
104 |
}
|
| 29198 |
manish |
105 |
|
| 32253 |
amit.gupta |
106 |
if (sendNotificationModel.getStateIds() != null && sendNotificationModel.getStateIds().size() > 0) {
|
| 33025 |
amit.gupta |
107 |
List<Integer> fofoIds = fofoStoreRepository.selectByWarehouseIds(sendNotificationModel.getStateIds()).stream()
|
|
|
108 |
.map(x -> x.getId()).collect(Collectors.toList());
|
| 34204 |
tejus.loha |
109 |
if (fofoIds.size() > 0) {
|
| 33025 |
amit.gupta |
110 |
userIds.addAll(userAccountRepository.selectUserIdsByRetailerIds(fofoIds));
|
|
|
111 |
}
|
|
|
112 |
|
| 32253 |
amit.gupta |
113 |
}
|
| 29927 |
amit.gupta |
114 |
|
| 34204 |
tejus.loha |
115 |
if (userIds.size() > 0) {
|
| 33025 |
amit.gupta |
116 |
for (Integer userId : userIds) {
|
|
|
117 |
UserCampaign uc = new UserCampaign();
|
|
|
118 |
uc.setCampaignId(nc.getId());
|
|
|
119 |
uc.setUserId(userId);
|
|
|
120 |
uc.setPushTimestamp(LocalDateTime.now());
|
|
|
121 |
userCampaignRepository.persist(uc);
|
|
|
122 |
}
|
|
|
123 |
List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(new ArrayList<>(userIds),
|
|
|
124 |
LocalDateTime.now().minusMonths(1), LocalDateTime.now());
|
|
|
125 |
pushNotification(nc.getId(), devices);
|
|
|
126 |
} else {
|
|
|
127 |
LOGGER.info("Failed to send notification to any retailer with this model - {}", sendNotificationModel);
|
| 32253 |
amit.gupta |
128 |
}
|
| 25822 |
amit.gupta |
129 |
|
| 32253 |
amit.gupta |
130 |
}
|
| 25822 |
amit.gupta |
131 |
|
| 32253 |
amit.gupta |
132 |
@Override
|
|
|
133 |
public void sendNotificationToAll(SendNotificationModel sendNotificationModel) throws ProfitMandiBusinessException {
|
|
|
134 |
sendNotificationModel.setUserIds(fofoStoreRepository.selectAllDtrUserIds());
|
|
|
135 |
Set<AuthUser> authUsers = new HashSet<>(
|
|
|
136 |
csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
|
|
|
137 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_CATEGORY));
|
|
|
138 |
authUsers.addAll(csService.getAuthUserByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES));
|
|
|
139 |
List<String> emailIds = authUsers.stream().map(x -> x.getEmailId()).collect(Collectors.toList());
|
|
|
140 |
emailIds.add("devkinandan.lal@smartdukaan.com");
|
|
|
141 |
List<User> systemUsers = dtrUserRepository.selectAllByEmailIds(emailIds);
|
| 32405 |
jai.hind |
142 |
sendNotificationModel.getUserIds()
|
|
|
143 |
.addAll(systemUsers.stream().map(x -> x.getId()).collect(Collectors.toList()));
|
| 32253 |
amit.gupta |
144 |
this.sendNotification(sendNotificationModel);
|
|
|
145 |
}
|
| 25822 |
amit.gupta |
146 |
|
| 32253 |
amit.gupta |
147 |
@Override
|
| 32405 |
jai.hind |
148 |
public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message)
|
|
|
149 |
throws ProfitMandiBusinessException {
|
| 32253 |
amit.gupta |
150 |
SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
|
|
|
151 |
sendNotificationModel.setCampaignName(campaignName);
|
|
|
152 |
sendNotificationModel.setMessageType(messageType);
|
|
|
153 |
sendNotificationModel.setTitle(title);
|
|
|
154 |
sendNotificationModel.setMessage(message);
|
|
|
155 |
int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
|
|
|
156 |
sendNotificationModel.setUserIds(Arrays.asList(userId));
|
|
|
157 |
sendNotificationModel.setMessageType(MessageType.wallet);
|
|
|
158 |
this.sendNotification(sendNotificationModel);
|
|
|
159 |
}
|
| 25822 |
amit.gupta |
160 |
|
| 32253 |
amit.gupta |
161 |
public SendNotificationModel getDefaultNotificationModel() {
|
|
|
162 |
SendNotificationModel sendNotificationModel = new SendNotificationModel();
|
|
|
163 |
sendNotificationModel.setType("url");
|
|
|
164 |
sendNotificationModel.setUrl("https://app.smartdukaan.com/pages/home/notifications");
|
|
|
165 |
sendNotificationModel.setExpiresat(LocalDateTime.now().plusDays(1));
|
|
|
166 |
sendNotificationModel.setMessageType(MessageType.notification);
|
|
|
167 |
return sendNotificationModel;
|
|
|
168 |
}
|
| 25822 |
amit.gupta |
169 |
|
| 32253 |
amit.gupta |
170 |
public void pushNotification(int cid, List<Device> devices) {
|
| 29198 |
manish |
171 |
|
| 32253 |
amit.gupta |
172 |
for (Device device : devices) {
|
|
|
173 |
PushNotifications pn = new PushNotifications();
|
|
|
174 |
pn.setNotificationCampaignid(cid);
|
|
|
175 |
pn.setDeviceId(device.getId());
|
|
|
176 |
pn.setUserId(device.getUser_id());
|
|
|
177 |
pushNotificationRepository.persist(pn);
|
|
|
178 |
}
|
| 29198 |
manish |
179 |
|
| 32253 |
amit.gupta |
180 |
}
|
| 29198 |
manish |
181 |
|
| 32253 |
amit.gupta |
182 |
@Override
|
| 34545 |
vikas.jang |
183 |
public void sendNotification(int fofoId, String campaignName, MessageType messageType, String title, String message, String url) throws ProfitMandiBusinessException {
|
| 32253 |
amit.gupta |
184 |
SendNotificationModel sendNotificationModel = this.getDefaultNotificationModel();
|
|
|
185 |
sendNotificationModel.setCampaignName(campaignName);
|
|
|
186 |
sendNotificationModel.setMessageType(messageType);
|
|
|
187 |
sendNotificationModel.setTitle(title);
|
|
|
188 |
sendNotificationModel.setMessage(message);
|
|
|
189 |
int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
|
|
|
190 |
sendNotificationModel.setUserIds(Arrays.asList(userId));
|
|
|
191 |
sendNotificationModel.setMessageType(messageType);
|
|
|
192 |
sendNotificationModel.setUrl(url);
|
|
|
193 |
this.sendNotification(sendNotificationModel);
|
|
|
194 |
}
|
| 29198 |
manish |
195 |
|
| 32253 |
amit.gupta |
196 |
@Override
|
| 34545 |
vikas.jang |
197 |
public boolean sendWhatsappMessage(String message, String title, String mobile) throws Exception {
|
| 34204 |
tejus.loha |
198 |
boolean isSend=false;
|
| 34520 |
amit.gupta |
199 |
LOGGER.info("Is Prod - {}", isProd);
|
| 32874 |
amit.gupta |
200 |
if (isProd) {
|
| 34204 |
tejus.loha |
201 |
isSend=this.sendWhatsappMessage(WhatsappMessageType.TEXT, message, title, mobile, null, null);
|
| 32874 |
amit.gupta |
202 |
}
|
| 34204 |
tejus.loha |
203 |
return isSend;
|
| 32253 |
amit.gupta |
204 |
}
|
| 25822 |
amit.gupta |
205 |
|
| 32253 |
amit.gupta |
206 |
@Override
|
| 34545 |
vikas.jang |
207 |
public boolean sendWhatsappMediaMessage(String message, String mobile, String mediaUrl, String fileName, WhatsappMessageType whatsappMessageType) throws Exception {
|
| 34204 |
tejus.loha |
208 |
boolean isSend=false;
|
| 33025 |
amit.gupta |
209 |
if (isProd) {
|
| 34204 |
tejus.loha |
210 |
isSend=this.sendWhatsappMessage(whatsappMessageType, message, null, mobile, mediaUrl, fileName);
|
| 32874 |
amit.gupta |
211 |
}
|
| 34204 |
tejus.loha |
212 |
return isSend;
|
| 32253 |
amit.gupta |
213 |
}
|
| 25822 |
amit.gupta |
214 |
|
| 32253 |
amit.gupta |
215 |
@Override
|
|
|
216 |
public void optIn(String phoneNumber) throws Exception {
|
|
|
217 |
Map<String, String> requestheaders = new HashMap<>();
|
|
|
218 |
requestheaders.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
219 |
Map<String, String> requestParams = new HashMap<>();
|
|
|
220 |
requestParams.put("userid", String.valueOf(2000215976));
|
|
|
221 |
requestParams.put("password", "MFRd!BBL");
|
|
|
222 |
requestParams.put("phone_number", phoneNumber);
|
|
|
223 |
requestParams.put("auth_scheme", "plain");
|
|
|
224 |
requestParams.put("v", "1.1");
|
|
|
225 |
requestParams.put("format", "json");
|
| 28400 |
tejbeer |
226 |
|
| 32253 |
amit.gupta |
227 |
requestParams.put("method", "OPT_IN");
|
| 25822 |
amit.gupta |
228 |
|
| 32253 |
amit.gupta |
229 |
requestParams.put("channel", "WHATSAPP");
|
|
|
230 |
String response = restClient.get("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
|
|
|
231 |
LOGGER.info("response" + response);
|
|
|
232 |
}
|
| 29927 |
amit.gupta |
233 |
|
| 34204 |
tejus.loha |
234 |
private boolean sendWhatsappMessage(WhatsappMessageType whatsappMessageType, String message, String title, String mobile, String mediaUrl, String fileName)
|
| 32405 |
jai.hind |
235 |
throws Exception {
|
| 32260 |
amit.gupta |
236 |
String sendTo = null;
|
| 34204 |
tejus.loha |
237 |
boolean isSend=false;
|
| 32259 |
amit.gupta |
238 |
if (mobile.length() != 10) {
|
| 34204 |
tejus.loha |
239 |
return isSend;
|
| 32259 |
amit.gupta |
240 |
} else {
|
| 32260 |
amit.gupta |
241 |
sendTo = 91 + mobile;
|
| 32259 |
amit.gupta |
242 |
}
|
| 32253 |
amit.gupta |
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");
|
| 32285 |
amit.gupta |
248 |
requestParams.put("send_to", sendTo);
|
| 32253 |
amit.gupta |
249 |
requestParams.put("v", "1.1");
|
|
|
250 |
requestParams.put("format", "json");
|
|
|
251 |
requestParams.put("auth_scheme", "plain");
|
| 32777 |
amit.gupta |
252 |
Optin optin = optinRepository.selectByMobile(mobile);
|
|
|
253 |
if (optin == null) {
|
|
|
254 |
this.optIn(sendTo);
|
|
|
255 |
optin = new Optin();
|
|
|
256 |
optin.setCreated(LocalDateTime.now());
|
|
|
257 |
optin.setMobile(mobile);
|
|
|
258 |
optinRepository.persist(optin);
|
|
|
259 |
}
|
| 32253 |
amit.gupta |
260 |
if (mediaUrl == null) {
|
| 32415 |
amit.gupta |
261 |
requestParams.put("method", "SENDMESSAGE");
|
| 32854 |
amit.gupta |
262 |
requestParams.put("msg_type", whatsappMessageType.name());
|
| 32253 |
amit.gupta |
263 |
requestParams.put("msg", message);
|
| 34522 |
ranu |
264 |
requestParams.put("isTemplate", "true");
|
|
|
265 |
requestParams.put("header", title);
|
| 32253 |
amit.gupta |
266 |
} else if (mediaUrl != null) {
|
| 32268 |
amit.gupta |
267 |
requestParams.put("method", "SENDMEDIAMESSAGE");
|
| 32854 |
amit.gupta |
268 |
requestParams.put("msg_type", whatsappMessageType.name());
|
| 32253 |
amit.gupta |
269 |
requestParams.put("caption", message);
|
|
|
270 |
requestParams.put("media_url", mediaUrl);
|
|
|
271 |
requestParams.put("filename", fileName);
|
| 32777 |
amit.gupta |
272 |
}
|
| 34545 |
vikas.jang |
273 |
String response = restClient.post("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestheaders);
|
| 32777 |
amit.gupta |
274 |
LOGGER.info("response - {}", response);
|
| 32403 |
tejbeer |
275 |
|
| 32777 |
amit.gupta |
276 |
JSONObject jsonObject = new JSONObject(response);
|
| 32401 |
tejbeer |
277 |
|
| 32777 |
amit.gupta |
278 |
JSONObject whatsappResponse = (JSONObject) jsonObject.get("response");
|
| 32874 |
amit.gupta |
279 |
if (whatsappResponse.getString("status").equals("error")) {
|
|
|
280 |
LOGGER.error("Invalid Whatsapp message, Reason - {}", whatsappResponse.getString("details"));
|
| 34204 |
tejus.loha |
281 |
return isSend;
|
|
|
282 |
}else{
|
|
|
283 |
isSend=true;
|
| 32850 |
amit.gupta |
284 |
}
|
| 32777 |
amit.gupta |
285 |
String externalId = whatsappResponse.getString("id");
|
|
|
286 |
String phone = whatsappResponse.getString("phone");
|
| 32403 |
tejbeer |
287 |
|
| 32777 |
amit.gupta |
288 |
WhatsappMessage whatsappMessage = new WhatsappMessage();
|
|
|
289 |
whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
|
|
|
290 |
whatsappMessage.setExternalId(externalId);
|
|
|
291 |
whatsappMessage.setDestAddr(phone);
|
|
|
292 |
whatsappMessageRepository.persist(whatsappMessage);
|
| 34204 |
tejus.loha |
293 |
return isSend;
|
| 32253 |
amit.gupta |
294 |
}
|
| 30025 |
amit.gupta |
295 |
|
| 32405 |
jai.hind |
296 |
@Override
|
| 33415 |
amit.gupta |
297 |
public boolean shouldSendWhatsappMessage(String mobile) {
|
| 32405 |
jai.hind |
298 |
String destAddr = "91" + mobile;
|
| 33415 |
amit.gupta |
299 |
boolean shouldSend = true;
|
| 32508 |
amit.gupta |
300 |
List<WhatsappMessage> whatsappMessages = whatsappMessageRepository.selectByDestAddr(destAddr, LocalDate.now());
|
| 32405 |
jai.hind |
301 |
if (!whatsappMessages.isEmpty()) {
|
| 32508 |
amit.gupta |
302 |
long failedCount = whatsappMessages.stream().filter(x -> x.getFailed() != null && x.getFailed().equals("FAILED")).collect(Collectors.counting());
|
| 32405 |
jai.hind |
303 |
if (failedCount >= 2) {
|
| 33415 |
amit.gupta |
304 |
shouldSend = false;
|
| 32405 |
jai.hind |
305 |
}
|
|
|
306 |
}
|
| 33415 |
amit.gupta |
307 |
return shouldSend;
|
| 32405 |
jai.hind |
308 |
}
|
| 33715 |
ranu |
309 |
|
|
|
310 |
|
|
|
311 |
@Override
|
|
|
312 |
public void sendPaymentWhatsappMessage(String mobile, String message) throws Exception {
|
| 33968 |
ranu |
313 |
String sendTo = "91" + mobile;
|
|
|
314 |
// String sendTo = "917082253510";
|
| 33715 |
ranu |
315 |
|
|
|
316 |
Map<String, String> requestHeaders = new HashMap<>();
|
|
|
317 |
requestHeaders.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
318 |
Map<String, String> requestParams = new HashMap<>();
|
|
|
319 |
requestParams.put("userid", String.valueOf(2000215976));
|
|
|
320 |
requestParams.put("password", "MFRd!BBL");
|
|
|
321 |
requestParams.put("send_to", sendTo);
|
|
|
322 |
requestParams.put("v", "1.1");
|
|
|
323 |
requestParams.put("format", "json");
|
|
|
324 |
requestParams.put("auth_scheme", "plain");
|
|
|
325 |
requestParams.put("method", "SENDMESSAGE");
|
|
|
326 |
requestParams.put("msg_type", "TEXT");
|
|
|
327 |
requestParams.put("msg", message);
|
|
|
328 |
requestParams.put("isTemplate", "true");
|
|
|
329 |
requestParams.put("header", "Payment Link!");
|
|
|
330 |
|
|
|
331 |
String response = restClient.post("https://media.smsgupshup.com/GatewayAPI/rest", requestParams, requestHeaders);
|
|
|
332 |
LOGGER.info("response - {}", response);
|
|
|
333 |
|
|
|
334 |
JSONObject jsonObject = new JSONObject(response);
|
|
|
335 |
JSONObject whatsappResponse = jsonObject.getJSONObject("response");
|
|
|
336 |
if (whatsappResponse.getString("status").equals("error")) {
|
|
|
337 |
LOGGER.error("Invalid Whatsapp message, Reason - {}", whatsappResponse.getString("details"));
|
|
|
338 |
return;
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
String externalId = whatsappResponse.getString("id");
|
|
|
342 |
String phone = whatsappResponse.getString("phone");
|
|
|
343 |
|
|
|
344 |
WhatsappMessage whatsappMessage = new WhatsappMessage();
|
|
|
345 |
whatsappMessage.setCreatedTimestamp(LocalDateTime.now());
|
|
|
346 |
whatsappMessage.setExternalId(externalId);
|
|
|
347 |
whatsappMessage.setDestAddr(phone);
|
|
|
348 |
whatsappMessageRepository.persist(whatsappMessage);
|
|
|
349 |
}
|
| 34545 |
vikas.jang |
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 |
}
|
| 25822 |
amit.gupta |
448 |
}
|