1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.chatbot.modules.stationnement.web;
35
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.stream.Collectors;
42
43 import javax.servlet.ServletException;
44 import javax.servlet.http.HttpServlet;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.apache.commons.io.IOUtils;
49
50 import com.fasterxml.jackson.databind.JsonNode;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52
53 import fr.paris.lutece.plugins.chatbot.modules.stationnement.business.NoFeesDay;
54 import fr.paris.lutece.plugins.chatbot.modules.stationnement.business.NoFeesDayHome;
55 import fr.paris.lutece.portal.service.util.AppLogService;
56 import fr.paris.lutece.portal.service.util.AppPropertiesService;
57 import fr.paris.lutece.util.httpaccess.HttpAccess;
58 import fr.paris.lutece.util.httpaccess.HttpAccessException;
59
60 public class NoFeesDayActionServlet extends HttpServlet
61 {
62
63 private static final String PROPERTY_TOKEN = "chatbot-stationnement.recast.token";
64
65
66
67
68 private static final long serialVersionUID = -2387659805321292879L;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 private static final ObjectMapper mapper = new ObjectMapper();
134
135 @Override
136 protected void service( HttpServletRequest request, HttpServletResponse response )
137 throws ServletException, IOException
138 {
139 String strBody = IOUtils.toString(request.getReader());
140 AppLogService.info ("Got request:" + strBody);
141 JsonNode actualObj = mapper.readTree(strBody);
142 String strConversionId = actualObj.path("conversation").get("conversation_id").asText();
143 String strDate = actualObj.path("conversation").path("memory").path("date").get("iso").asText();
144 String strDateSql = strDate.substring(0, 10);
145 HttpAccess httpAccess = new HttpAccess( );
146 Map<String, Object> responseJson = new HashMap<>();
147 List<Object> messages = new ArrayList<>();
148 responseJson.put("messages", messages);
149
150 Map<String, Object> message = new HashMap<>();
151 messages.add(message);
152
153 List<NoFeesDay> listNoFeesDays = NoFeesDayHome.getNoFeesDaysList();
154 String strNoFeeDays = listNoFeesDays.stream()
155 .map(it -> it.getDate().toString() )
156 .collect(Collectors.joining(", "));
157 boolean isEqual = listNoFeesDays.stream().anyMatch(it -> {
158 return it.getDate().toString().equals(strDateSql);
159 });
160
161 message.put("type", "text");
162 message.put("content", "List des jours fériés : " + strNoFeeDays);
163
164 message = new HashMap<>();
165 messages.add(message);
166 message.put("type", "text");
167 message.put("content", isEqual ? "Donc oui, c'est gratuit !" : "Donc non, c'est payant...");
168
169 String strResponseJson = mapper.writeValueAsString( responseJson );
170
171 Map<String, String> headers = new HashMap<>();
172 String strToken = AppPropertiesService.getProperty( PROPERTY_TOKEN );
173 headers.put( "Authorization", "Token " + strToken);
174
175
176 Map<String, String> headersResponse = new HashMap<>();
177 try {
178
179
180 String strResponse = httpAccess.doPostJSON(
181 "https://api.recast.ai/connect/v1/conversations/" + strConversionId + "/messages",
182 strResponseJson, headers, headersResponse);
183 AppLogService.info ("Got reponse " + strResponse) ;
184 } catch (HttpAccessException e) {
185 AppLogService.error ("Error posting message to conversion " + strBody, e);
186
187 }
188
189 Map<String, Object> hookResponse = new HashMap<>();
190 Map<String, Object> newMemory = mapper.convertValue(actualObj.path("conversation").get("memory"), Map.class);
191 newMemory.put("isfreeday", isEqual);
192 hookResponse.put("memory", newMemory);
193 hookResponse.put("memory", newMemory);
194 hookResponse.put("messages", messages);
195
196 String strFullResponse = mapper.writeValueAsString(hookResponse);
197 AppLogService.info( "Full hook response : " + strFullResponse );
198 response.setHeader( "Content-Type", "application/json" );
199 response.getWriter().print(strFullResponse);
200 }
201 }