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.easyrulesbot.service.bot;
35
36 import fr.paris.lutece.plugins.chatbot.business.BotPost;
37 import fr.paris.lutece.plugins.easyrulesbot.service.bot.rules.BotRule;
38 import fr.paris.lutece.plugins.easyrulesbot.service.response.filters.ResponseFilter;
39 import fr.paris.lutece.plugins.easyrulesbot.service.response.exceptions.ResponseProcessingException;
40 import fr.paris.lutece.portal.service.security.LuteceUser;
41 import fr.paris.lutece.portal.service.security.SecurityService;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43
44 import org.easyrules.api.Rule;
45 import org.easyrules.api.RulesEngine;
46
47 import java.io.Serializable;
48
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.Locale;
52 import java.util.Map;
53 import java.util.concurrent.ConcurrentHashMap;
54
55 import javax.servlet.http.HttpServletRequest;
56
57
58
59
60 public class BotExecutor implements Serializable
61 {
62 public static final String DATA_USER_FIRSTNAME = "user_firstname";
63 public static final String DATA_USER_LASTNAME = "user_lastname";
64 public static final String DATA_USER_EMAIL = "user_email";
65 private static final long serialVersionUID = 1L;
66 private EasyRulesBot _bot;
67 private Locale _locale;
68 private BotRule _currentRule;
69 private Map<String, String> _mapData = new ConcurrentHashMap<String, String>( );
70
71
72
73
74
75
76
77
78
79 public BotExecutor( EasyRulesBot bot, Locale locale )
80 {
81 _bot = bot;
82 _locale = locale;
83 }
84
85
86
87
88
89
90 public Locale getLocale( )
91 {
92 return _locale;
93 }
94
95
96
97
98
99
100 public String getQuestion( )
101 {
102 String strQuestion = null;
103
104 if ( _currentRule != null )
105 {
106 strQuestion = _currentRule.getMessage( _mapData, _locale );
107 }
108
109 return strQuestion;
110 }
111
112
113
114
115
116
117
118
119
120
121 public List<BotPost> processResponse( String strUserMessage ) throws ResponseProcessingException
122 {
123 List<BotPost> listBotPost = new ArrayList<>( );
124
125 String strResponseValue = processFilters( strUserMessage );
126
127 if ( _currentRule != null )
128 {
129 strResponseValue = _currentRule.getResponseProcessor( ).processResponse( strResponseValue, _locale, _mapData );
130 _mapData.put( _currentRule.getDataKey( ), strResponseValue );
131
132 String strResponseComment = _currentRule.getResponseComment( _mapData, _locale );
133
134 if ( ( strResponseComment != null ) && !strResponseComment.trim( ).equals( "" ) )
135 {
136 BotPost post = new BotPost( strResponseComment, BotPost.CONTENT_TYPE_TEXT );
137 listBotPost.add( post );
138 }
139 }
140 return listBotPost;
141 }
142
143
144
145
146
147
148
149
150
151
152 private String processFilters( String strResponse ) throws ResponseProcessingException
153 {
154 String strResponseValue = strResponse;
155
156 for ( ResponseFilter filter : _bot.getResponseFilters( ) )
157 {
158 strResponseValue = filter.filterResponse( strResponseValue, _locale, _mapData );
159 }
160
161 return strResponseValue;
162 }
163
164
165
166
167
168
169
170 public void setCurrentRule( BotRule rule )
171 {
172 _currentRule = rule;
173 }
174
175
176
177
178 public synchronized void fireRules( )
179 {
180 _currentRule = null;
181
182 RulesEngine engine = _bot.getRulesEngine( );
183
184 for ( Rule rule : engine.getRules( ) )
185 {
186 if ( rule instanceof BotRule )
187 {
188 ( (BotRule) rule ).setExecutor( this );
189 }
190 }
191
192 engine.fireRules( );
193 }
194
195
196
197
198 public void traceData( )
199 {
200 for ( String strKey : _mapData.keySet( ) )
201 {
202 AppLogService.info( strKey + " : " + _mapData.get( strKey ) );
203 }
204 }
205
206
207
208
209
210
211 public Map<String, String> getDataMap( )
212 {
213 return _mapData;
214 }
215
216
217
218
219
220
221 public String getBotName( )
222 {
223 return _bot.getName( _locale );
224 }
225
226
227
228
229
230
231 public void setLuteceUser( HttpServletRequest request )
232 {
233 if ( SecurityService.isAuthenticationEnable( ) )
234 {
235 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
236
237 if ( user != null )
238 {
239 _mapData.put( DATA_USER_FIRSTNAME, user.getUserInfo( LuteceUser.NAME_GIVEN ) );
240 _mapData.put( DATA_USER_LASTNAME, user.getUserInfo( LuteceUser.NAME_FAMILY ) );
241 _mapData.put( DATA_USER_EMAIL, user.getEmail( ) );
242 }
243 }
244 }
245
246
247
248
249
250
251 public Object getBotAvatarUrl( )
252 {
253 return _bot.getAvatarUrl( );
254 }
255
256
257 }