| 3339 |
mandeep.dh |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.util;
|
|
|
5 |
|
|
|
6 |
import in.shop2020.crm.Activity;
|
|
|
7 |
import in.shop2020.crm.ActivityType;
|
|
|
8 |
import in.shop2020.crm.CRMService.Client;
|
| 3390 |
mandeep.dh |
9 |
import in.shop2020.crm.SearchFilter;
|
| 3339 |
mandeep.dh |
10 |
import in.shop2020.crm.Ticket;
|
|
|
11 |
import in.shop2020.crm.TicketCategory;
|
|
|
12 |
import in.shop2020.crm.TicketPriority;
|
|
|
13 |
import in.shop2020.crm.TicketStatus;
|
|
|
14 |
import in.shop2020.model.v1.user.User;
|
|
|
15 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
16 |
import in.shop2020.thrift.clients.CRMClient;
|
|
|
17 |
import in.shop2020.thrift.clients.UserClient;
|
|
|
18 |
|
|
|
19 |
import java.io.IOException;
|
|
|
20 |
import java.text.ParseException;
|
|
|
21 |
import java.util.Date;
|
| 3408 |
mandeep.dh |
22 |
import java.util.List;
|
| 3339 |
mandeep.dh |
23 |
import java.util.regex.Matcher;
|
|
|
24 |
import java.util.regex.Pattern;
|
|
|
25 |
|
|
|
26 |
import javax.mail.Message;
|
|
|
27 |
import javax.mail.MessagingException;
|
| 3368 |
mandeep.dh |
28 |
import javax.mail.Multipart;
|
|
|
29 |
import javax.mail.Part;
|
| 3339 |
mandeep.dh |
30 |
|
| 3546 |
mandeep.dh |
31 |
import net.htmlparser.jericho.Source;
|
|
|
32 |
|
| 3339 |
mandeep.dh |
33 |
import org.apache.commons.logging.Log;
|
|
|
34 |
import org.apache.commons.logging.LogFactory;
|
|
|
35 |
import org.apache.thrift.TException;
|
|
|
36 |
import org.apache.thrift.transport.TTransportException;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* @author mandeep
|
|
|
40 |
*
|
|
|
41 |
*/
|
|
|
42 |
public class CRMEmailProcessor {
|
| 3368 |
mandeep.dh |
43 |
private static final int DESCRIPTION_MAX_WIDTH = 1900;
|
| 3369 |
mandeep.dh |
44 |
private static final String MAILOR_DAEMON_EMAIL_ID = "mailer-daemon@googlemail.com";
|
| 3339 |
mandeep.dh |
45 |
private static final Log log = LogFactory
|
|
|
46 |
.getLog(CRMEmailProcessor.class);
|
|
|
47 |
private Client client;
|
|
|
48 |
|
|
|
49 |
public void processEmail(Message message) throws MessagingException,
|
|
|
50 |
IOException, TException, UserContextException {
|
| 3369 |
mandeep.dh |
51 |
// Ignoring mails from Mailor daemon
|
|
|
52 |
if (MAILOR_DAEMON_EMAIL_ID.equals(parseEmailId(message.getFrom()[0].toString()))) {
|
|
|
53 |
return;
|
|
|
54 |
}
|
|
|
55 |
|
| 4142 |
mandeep.dh |
56 |
// Try parsing the ticket Id from email with subject like Saholic#123 ...
|
| 3339 |
mandeep.dh |
57 |
Long ticketId = parseTicketId(message);
|
|
|
58 |
|
|
|
59 |
if (ticketId != null) {
|
|
|
60 |
log.info("Response for Ticket: " + ticketId + ": " + message);
|
|
|
61 |
updateTicket(ticketId, message);
|
|
|
62 |
} else {
|
|
|
63 |
log.info("Mail not recognized as CRM ticket response with subject: "
|
|
|
64 |
+ message.getSubject());
|
|
|
65 |
log.info("Creating ticket for the same");
|
|
|
66 |
createTicket(message);
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
| 4142 |
mandeep.dh |
70 |
// Parses regex like ^.*Saholic#(\d+).*
|
| 3339 |
mandeep.dh |
71 |
private Long parseTicketId(Message message) throws MessagingException {
|
|
|
72 |
Long ticketId = null;
|
|
|
73 |
String subject = message.getSubject();
|
|
|
74 |
if (subject != null) {
|
|
|
75 |
Pattern p = Pattern.compile("^.*"
|
|
|
76 |
+ CRMConstants.CRM_SUBJECT_PREFIX_FOR_TICKET_ID
|
|
|
77 |
+ "(\\d+).*$");
|
|
|
78 |
Matcher m = p.matcher(subject);
|
|
|
79 |
if (m.matches()) {
|
|
|
80 |
ticketId = Long.parseLong(m.group(1));
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
return ticketId;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public Date getLastProcessedTimestamp() throws ParseException, TException {
|
| 3373 |
mandeep.dh |
88 |
client = new CRMClient().getClient();
|
| 3339 |
mandeep.dh |
89 |
return new Date(client.getLastEmailProcessedTimestamp());
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public void updateLastProcessedTimestamp(Date date) throws TException {
|
| 3340 |
mandeep.dh |
93 |
client = new CRMClient().getClient();
|
| 3339 |
mandeep.dh |
94 |
client.updateLastEmailProcessedTimestamp(date.getTime());
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
private void updateTicket(Long ticketId, Message message) {
|
|
|
98 |
try {
|
| 3390 |
mandeep.dh |
99 |
SearchFilter searchFilter = new SearchFilter();
|
|
|
100 |
searchFilter.setTicketId(ticketId);
|
| 3373 |
mandeep.dh |
101 |
client = new CRMClient().getClient();
|
| 3408 |
mandeep.dh |
102 |
List<Ticket> tickets = client.getTickets(searchFilter);
|
|
|
103 |
if (tickets == null || tickets.isEmpty()) {
|
|
|
104 |
log.error("Invalid TicketId: " + ticketId);
|
|
|
105 |
log.error("Not processing message with subject: " + message.getSubject());
|
|
|
106 |
return;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
Ticket ticket = tickets.get(0);
|
| 3339 |
mandeep.dh |
110 |
Activity activity = new Activity();
|
|
|
111 |
activity.setTicketId(ticketId);
|
|
|
112 |
activity.setTicketAssigneeId(ticket.getAssigneeId());
|
|
|
113 |
activity.setTicketCategory(ticket.getCategory());
|
|
|
114 |
activity.setTicketDescription(ticket.getDescription());
|
|
|
115 |
activity.setTicketPriority(ticket.getPriority());
|
|
|
116 |
activity.setTicketStatus(ticket.getStatus());
|
|
|
117 |
activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
|
|
|
118 |
activity.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
|
| 3398 |
mandeep.dh |
119 |
activity.setIsRead(false);
|
| 3339 |
mandeep.dh |
120 |
|
|
|
121 |
if (ticket.isSetCustomerId()) {
|
|
|
122 |
activity.setCustomerId(ticket.getCustomerId());
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
activity.setDescription(convertMessageToText(message));
|
| 3368 |
mandeep.dh |
126 |
|
|
|
127 |
if (TicketStatus.CLOSED.equals(ticket.getStatus())) {
|
|
|
128 |
ticket.setStatus(TicketStatus.REOPEN);
|
|
|
129 |
}
|
|
|
130 |
|
| 3373 |
mandeep.dh |
131 |
client = new CRMClient().getClient();
|
| 3339 |
mandeep.dh |
132 |
client.updateTicket(ticket, activity);
|
|
|
133 |
} catch (TTransportException e) {
|
|
|
134 |
log.error("Could not update ticket " + ticketId + " with mail "
|
|
|
135 |
+ message, e);
|
|
|
136 |
} catch (TException e) {
|
|
|
137 |
log.error("Could not update ticket " + ticketId + " with mail "
|
|
|
138 |
+ message, e);
|
|
|
139 |
} catch (MessagingException e) {
|
|
|
140 |
log.error("Could not update ticket " + ticketId + " with mail "
|
|
|
141 |
+ message, e);
|
|
|
142 |
} catch (IOException e) {
|
|
|
143 |
log.error("Could not update ticket " + ticketId + " with mail "
|
|
|
144 |
+ message, e);
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
private void createTicket(Message message) throws MessagingException,
|
|
|
149 |
IOException, TException, UserContextException {
|
|
|
150 |
String description = convertMessageToText(message);
|
|
|
151 |
Ticket ticket = new Ticket();
|
|
|
152 |
ticket.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
|
|
|
153 |
ticket.setCategory(TicketCategory.OTHER);
|
|
|
154 |
String customerEmailId = parseEmailId(message.getFrom()[0].toString());
|
|
|
155 |
|
|
|
156 |
in.shop2020.model.v1.user.UserContextService.Client userClient = new UserClient()
|
|
|
157 |
.getClient();
|
|
|
158 |
User user = userClient.getUserByEmail(customerEmailId);
|
|
|
159 |
|
|
|
160 |
if (user == null || user.getUserId() == -1) {
|
|
|
161 |
ticket.setCustomerEmailId(customerEmailId);
|
|
|
162 |
} else {
|
|
|
163 |
ticket.setCustomerId(user.getUserId());
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
ticket.setDescription(description);
|
|
|
167 |
ticket.setPriority(TicketPriority.MEDIUM);
|
|
|
168 |
ticket.setStatus(TicketStatus.OPEN);
|
|
|
169 |
|
|
|
170 |
log.info("Creating activity!");
|
|
|
171 |
Activity activity = new Activity();
|
|
|
172 |
activity.setDescription(description);
|
|
|
173 |
activity.setTicketCategory(ticket.getCategory());
|
|
|
174 |
activity.setTicketDescription(ticket.getDescription());
|
|
|
175 |
activity.setTicketPriority(ticket.getPriority());
|
|
|
176 |
activity.setTicketStatus(ticket.getStatus());
|
|
|
177 |
activity.setType(ActivityType.RECEIVED_EMAIL_FROM_CUSTOMER);
|
|
|
178 |
activity.setCreatorId(CRMConstants.ADMIN_AGENT_ID);
|
| 3398 |
mandeep.dh |
179 |
activity.setIsRead(false);
|
| 3339 |
mandeep.dh |
180 |
|
| 3373 |
mandeep.dh |
181 |
client = new CRMClient().getClient();
|
| 3339 |
mandeep.dh |
182 |
client.insertTicket(ticket, activity);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
// parsing email Id from strings like
|
| 3368 |
mandeep.dh |
186 |
// "Pankaj Jain <ponkoj@hotmail.com>"
|
| 3339 |
mandeep.dh |
187 |
private String parseEmailId(String address) {
|
|
|
188 |
Pattern p = Pattern.compile("^.*<(.+)>.*$");
|
|
|
189 |
Matcher m = p.matcher(address);
|
|
|
190 |
if (m.matches()) {
|
|
|
191 |
address = m.group(1);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
return address;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
private String convertMessageToText(Message message)
|
|
|
198 |
throws MessagingException, IOException {
|
| 3368 |
mandeep.dh |
199 |
String messageContent = getText(message);
|
| 3339 |
mandeep.dh |
200 |
|
| 3368 |
mandeep.dh |
201 |
String content = "From: " + message.getFrom()[0].toString()
|
|
|
202 |
+ "\n\nSubject: " + message.getSubject() + "\n\nBody: "
|
|
|
203 |
+ messageContent;
|
| 3339 |
mandeep.dh |
204 |
|
|
|
205 |
if (content.length() > DESCRIPTION_MAX_WIDTH) {
|
|
|
206 |
content = content.substring(0, DESCRIPTION_MAX_WIDTH);
|
| 3368 |
mandeep.dh |
207 |
content += "\n\nTHIS TEXT IS TRUNCATED. PLEASE VISIT INBOX TO SEE COMPLETE DETAILS.";
|
| 3339 |
mandeep.dh |
208 |
}
|
|
|
209 |
|
|
|
210 |
return content;
|
|
|
211 |
}
|
| 3368 |
mandeep.dh |
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Return the primary text content of the message.
|
|
|
215 |
*/
|
|
|
216 |
private String getText(Part p) throws MessagingException, IOException {
|
|
|
217 |
if (p.isMimeType("text/*")) {
|
|
|
218 |
String s = (String) p.getContent();
|
| 3546 |
mandeep.dh |
219 |
if (p.isMimeType("text/html")) {
|
|
|
220 |
return new Source(s).getTextExtractor().toString();
|
|
|
221 |
}
|
|
|
222 |
else {
|
|
|
223 |
return s;
|
|
|
224 |
}
|
| 3368 |
mandeep.dh |
225 |
}
|
|
|
226 |
|
|
|
227 |
if (p.isMimeType("multipart/alternative")) {
|
|
|
228 |
// prefer plain text over html text
|
|
|
229 |
Multipart mp = (Multipart) p.getContent();
|
|
|
230 |
String text = null;
|
|
|
231 |
for (int i = 0; i < mp.getCount(); i++) {
|
|
|
232 |
Part bp = mp.getBodyPart(i);
|
|
|
233 |
if (bp.isMimeType("text/html")) {
|
|
|
234 |
if (text == null)
|
|
|
235 |
text = getText(bp);
|
|
|
236 |
continue;
|
|
|
237 |
} else if (bp.isMimeType("text/plain")) {
|
|
|
238 |
String s = getText(bp);
|
|
|
239 |
if (s != null)
|
|
|
240 |
return s;
|
|
|
241 |
} else {
|
|
|
242 |
return getText(bp);
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
return text;
|
|
|
246 |
} else if (p.isMimeType("multipart/*")) {
|
|
|
247 |
Multipart mp = (Multipart) p.getContent();
|
|
|
248 |
for (int i = 0; i < mp.getCount(); i++) {
|
|
|
249 |
String s = getText(mp.getBodyPart(i));
|
|
|
250 |
if (s != null)
|
|
|
251 |
return s;
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
return null;
|
|
|
256 |
}
|
| 3339 |
mandeep.dh |
257 |
}
|