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
35 package fr.paris.lutece.plugins.chatbot.service;
36
37 import fr.paris.lutece.plugins.chatbot.business.BotPost;
38 import fr.paris.lutece.plugins.chatbot.service.bot.ChatBot;
39 import fr.paris.lutece.plugins.chatbot.business.ChatData;
40 import fr.paris.lutece.plugins.chatbot.business.Post;
41 import fr.paris.lutece.portal.service.spring.SpringContextService;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Locale;
45 import java.util.Map;
46 import javax.servlet.http.HttpServletRequest;
47 import org.apache.commons.lang3.StringUtils;
48
49
50
51
52 public final class ChatService
53 {
54 private static final String RESET = "reset";
55
56 private static Map<String, ChatData> _mapConversations = new HashMap<>( );
57 private static PostRenderer _renderer = SpringContextService.getBean( "chatbot.postRenderer" );
58
59
60
61
62 private ChatService( )
63 {
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public static void processMessage( HttpServletRequest request, String strConversationId, String strMessage, String strBotKey, Locale locale )
81 {
82 ChatBot bot = BotService.getBot( strBotKey );
83 String strUserMessage = extractUserMessage( strMessage );
84 String strUserCodifiedMessage= extractUserCodifiedMessage( strMessage);
85
86 boolean bResetConversation = RESET.equalsIgnoreCase( strUserCodifiedMessage );
87 if ( bResetConversation )
88 {
89 _mapConversations.remove( strConversationId );
90 bot.reset( strConversationId );
91 }
92 ChatData data = _mapConversations.get( strConversationId );
93 if ( data == null )
94 {
95 data = new ChatData( bot.getWelcomeMessage() );
96 _mapConversations.put( strConversationId, data );
97 }
98 if ( !bResetConversation )
99 {
100 data.addUserPost( strUserMessage );
101 }
102
103 if ( !bResetConversation || StringUtils.isEmpty( bot.getWelcomeMessage( ) ) )
104 {
105 List<BotPost> listMessages = bot.processUserMessage( strUserCodifiedMessage, strConversationId, locale );
106 for ( BotPost post : listMessages )
107 {
108 data.addBotPost( post );
109 }
110 }
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124 public static List<Post> getConversation( String strConversationId, ChatBot bot, Locale locale )
125 {
126 ChatData data = _mapConversations.get( strConversationId );
127 if ( data == null )
128 {
129 if ( StringUtils.isEmpty( bot.getWelcomeMessage() ) )
130 {
131 processMessage( null, strConversationId, "", bot.getKey( ), locale );
132 data = _mapConversations.get( strConversationId );
133 }
134 else
135 {
136 data = new ChatData( bot.getWelcomeMessage() );
137 }
138 }
139 return _renderer.render( data.getPosts( ) );
140 }
141
142
143
144
145
146
147
148
149 private static String extractUserMessage( String strMessage )
150 {
151 int nPos = strMessage.indexOf( '|' );
152 if ( nPos > 0 )
153 {
154 return strMessage.substring( 0, nPos );
155 }
156 return strMessage;
157 }
158
159
160
161
162
163
164
165
166
167 private static String extractUserCodifiedMessage( String strMessage )
168 {
169 int nPos = strMessage.indexOf( '|' );
170 if ( ( nPos > 0 ) && ( nPos < strMessage.length() ))
171 {
172 return strMessage.substring( nPos + 1 );
173 }
174 return strMessage;
175 }
176
177 }