Skip to content

Commit 1ad8105

Browse files
LaurenceTewsrabidgremlin
authored andcommitted
Set re-prompts and hints into session during confused bot state (rabidgremlin#15)
* set reprompts and hints into session during confused bot state * Added test for confused knot with reprompts * undid explicit obj creation in logger
1 parent de3f024 commit 1ad8105

File tree

7 files changed

+101
-35
lines changed

7 files changed

+101
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/.gradle/
33
/build/
44

5+
.idea
56
.project
67
.classpath
78
.settings

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version=4.4.1
1+
version=4.4.2
22

33
# These are place holder values. Real values should be set in user's home gradle.properties
44
# and are only needed when signing and uploading to central maven repo

mutters-core/src/main/java/com/rabidgremlin/mutters/core/util/SessionUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static void setNumberSlotIntoSession(Session session, String slotName, Nu
123123
*
124124
* @param session The session.
125125
* @param slotName The name of the slot.
126-
* @param value The balue to store.
126+
* @param value The value to store.
127127
*/
128128
public static void setStringSlotIntoSession(Session session, String slotName, String value)
129129
{

mutters-ink-bot/src/main/java/com/rabidgremlin/mutters/bot/ink/InkBot.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import java.util.Map;
77
import java.util.Random;
8+
import java.util.Collection;
89

910
import org.apache.commons.lang3.StringUtils;
1011
import org.slf4j.Logger;
@@ -67,7 +68,7 @@ public abstract class InkBot<T extends InkBotConfiguration>
6768
protected HashMap<String, InkBotFunction> inkBotFunctions = new HashMap<String, InkBotFunction>();
6869

6970
/** Map of global intents for the bot. */
70-
protected HashMap<String, String> globalIntents = new HashMap<String, String>();
71+
protected HashMap<String, String> globalIntents = new HashMap<>();
7172

7273
/** Random for default reponses. */
7374
private Random rand = new Random();
@@ -141,7 +142,7 @@ public InkBot(T configuration)
141142
List<String> defaultResponses = configuration.getDefaultResponses();
142143
if (defaultResponses != null)
143144
{
144-
setDefaultResponses(defaultResponses.toArray(new String[defaultResponses.size()]));
145+
setDefaultResponses(defaultResponses.toArray(new String[0]));
145146
}
146147
}
147148

@@ -156,7 +157,7 @@ public BotResponse respond(Session session, Context context, String messageText)
156157
throws BotException
157158
{
158159
log.debug("===> \n session: {} context: {} messageText: {}",
159-
new Object[]{ session, context, messageText });
160+
new Object[]{ session, context, messageText });
160161

161162
CurrentResponse currentResponse = new CurrentResponse();
162163

@@ -206,7 +207,7 @@ public BotResponse respond(Session session, Context context, String messageText)
206207
// restore the story state
207208
SessionUtils.loadInkStoryState(session, story.getState());
208209

209-
// call hook so additional things can be applied to story after state has been restored
210+
// call hook so additional things can be applied to story after state has been restored
210211
afterStoryStateLoaded(story);
211212

212213
// get to right place in story, capture any pretext
@@ -237,19 +238,7 @@ public BotResponse respond(Session session, Context context, String messageText)
237238
afterIntentMatch(intentMatch, session, story);
238239

239240
// copy any slot values into ink vars
240-
for (SlotMatch slotMatch : intentMatch.getSlotMatches().values())
241-
{
242-
if (slotMatch.getValue() instanceof Number)
243-
{
244-
story.getVariablesState().set(slotMatch.getSlot().getName().toLowerCase(),
245-
slotMatch.getValue());
246-
}
247-
else
248-
{
249-
story.getVariablesState().set(slotMatch.getSlot().getName().toLowerCase(),
250-
slotMatch.getValue().toString());
251-
}
252-
}
241+
setSlotValuesInInk(intentMatch.getSlotMatches().values(), story);
253242

254243
// did we match something flag. Used so we can set reprompt correctly
255244
boolean foundMatch = false;
@@ -294,17 +283,7 @@ public BotResponse respond(Session session, Context context, String messageText)
294283
// reset failed count
295284
failedToUnderstandCount = 0;
296285

297-
// set reprompt into session
298-
if (currentResponse.getReprompt() != null)
299-
{
300-
SessionUtils.setReprompt(session, currentResponse.getReprompt());
301-
}
302-
else
303-
{
304-
SessionUtils.setReprompt(session, defaultResponse + " " + currentResponse.getResponseText());
305-
}
306-
SessionUtils.setRepromptHint(session, currentResponse.getHint());
307-
SessionUtils.setRepromptQuickReplies(session, currentResponse.getResponseQuickReplies());
286+
setRepromptInSession(currentResponse, session, defaultResponse);
308287
}
309288
else
310289
{
@@ -331,6 +310,8 @@ public BotResponse respond(Session session, Context context, String messageText)
331310
getResponseText(session, currentResponse, story, intentMatch, false, preText);
332311
// reset failed count
333312
failedToUnderstandCount = 0;
313+
314+
setRepromptInSession(currentResponse, session, defaultResponse);
334315
}
335316

336317
// save failed count
@@ -364,6 +345,35 @@ public BotResponse respond(Session session, Context context, String messageText)
364345
}
365346
}
366347

348+
private void setRepromptInSession(CurrentResponse currentResponse, Session session, String defaultResponse)
349+
{
350+
if (currentResponse.getReprompt() != null)
351+
{
352+
SessionUtils.setReprompt(session, currentResponse.getReprompt());
353+
}
354+
else
355+
{
356+
SessionUtils.setReprompt(session, defaultResponse + " " + currentResponse.getResponseText());
357+
}
358+
SessionUtils.setRepromptHint(session, currentResponse.getHint());
359+
SessionUtils.setRepromptQuickReplies(session, currentResponse.getResponseQuickReplies());
360+
}
361+
362+
private void setSlotValuesInInk(Collection<SlotMatch> slotMatches, Story story) throws Exception
363+
{
364+
for (SlotMatch slotMatch : slotMatches)
365+
{
366+
if (slotMatch.getValue() instanceof Number)
367+
{
368+
story.getVariablesState().set(slotMatch.getSlot().getName().toLowerCase(), slotMatch.getValue());
369+
}
370+
else
371+
{
372+
story.getVariablesState().set(slotMatch.getSlot().getName().toLowerCase(), slotMatch.getValue().toString());
373+
}
374+
}
375+
}
376+
367377
private void getResponseText(Session session, CurrentResponse currentResponse, Story story, IntentMatch intentMatch, boolean skipfirst, String preText)
368378
throws StoryException, Exception
369379
{

mutters-ink-bot/src/test/ink/taxi/confused.ink

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,15 @@
22
I'm sorry I'm not understanding you at all :(
33
If you are in a hurry, please call 555-12345 to order your taxi.
44
-> END
5+
6+
== confused_bot_with_handover ==
7+
I'm struggling with that one. Do you want me to call our service line for you?
8+
::SET_REPROMPT Would you like me to call our service line?
9+
::SET_HINT Yes or no
10+
::ADD_QUICK_REPLY Yes
11+
::ADD_QUICK_REPLY No
12+
+ YesIntent
13+
Calling our service operators now. Please hold the line.
14+
+ NoIntent
15+
Okay. I'm here if you need me.
16+
-> END

mutters-ink-bot/src/test/java/com/rabidgremlin/mutters/bot/ink/TestConfusedBot.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33
import static org.hamcrest.CoreMatchers.is;
44
import static org.hamcrest.CoreMatchers.notNullValue;
5-
import static org.hamcrest.CoreMatchers.nullValue;
65
import static org.hamcrest.CoreMatchers.startsWith;
76
import static org.junit.Assert.assertThat;
87

98
import org.junit.Before;
10-
import org.junit.BeforeClass;
119
import org.junit.Test;
1210

13-
import com.rabidgremlin.mutters.bot.ink.InkBotConfiguration.ConfusedKnot;
1411
import com.rabidgremlin.mutters.core.Context;
1512
import com.rabidgremlin.mutters.core.bot.BotException;
1613
import com.rabidgremlin.mutters.core.bot.BotResponse;
1714
import com.rabidgremlin.mutters.core.session.Session;
1815

16+
import java.util.List;
17+
1918
public class TestConfusedBot
2019
{
2120
private TaxiInkBot botWithConfusedKnot;
2221
private TaxiInkBot botWithoutConfusedKnot;
22+
private TaxiInkBot botWithConfusedKnotWithReprompts;
2323

2424

2525
class TaxiBotWithConfusedKnotConfig extends TaxiInkBotConfiguration
@@ -30,6 +30,15 @@ public ConfusedKnot getConfusedKnot()
3030
return new ConfusedKnot(2, "confused_bot");
3131
}
3232
}
33+
34+
class TaxiBotWithConfusedKnotWithRepromptsConfig extends TaxiInkBotConfiguration
35+
{
36+
@Override
37+
public ConfusedKnot getConfusedKnot()
38+
{
39+
return new ConfusedKnot(2, "confused_bot_with_handover");
40+
}
41+
}
3342

3443
class TaxiBotWithoutConfusedKnotConfig extends TaxiInkBotConfiguration
3544
{
@@ -46,7 +55,8 @@ public ConfusedKnot getConfusedKnot()
4655
public void setUp()
4756
{
4857
botWithConfusedKnot = new TaxiInkBot(new TaxiBotWithConfusedKnotConfig());
49-
botWithoutConfusedKnot = new TaxiInkBot(new TaxiBotWithoutConfusedKnotConfig());
58+
botWithoutConfusedKnot = new TaxiInkBot(new TaxiBotWithoutConfusedKnotConfig());
59+
botWithConfusedKnotWithReprompts = new TaxiInkBot(new TaxiBotWithConfusedKnotWithRepromptsConfig());
5060
}
5161

5262

@@ -142,5 +152,38 @@ public void testStopConfusion()
142152
assertThat(response.isAskResponse(), is(false));
143153
}
144154

155+
@Test
156+
public void testConfusedKnotWithReprompts()
157+
throws BotException
158+
{
159+
Session session = new Session();
160+
Context context = new Context();
161+
162+
BotResponse response = botWithConfusedKnotWithReprompts.respond(session, context, "Order me a taxi");
163+
164+
assertThat(response, is(notNullValue()));
165+
assertThat(response.getResponse(), is("What is the pick up address ?"));
166+
assertThat(response.isAskResponse(), is(true));
167+
168+
response = botWithConfusedKnotWithReprompts.respond(session, context, "skibidi whop");
169+
170+
assertThat(response, is(notNullValue()));
171+
assertThat(response.getResponse(), is("Where would you like to be picked up ?"));
172+
assertThat(response.isAskResponse(), is(true));
173+
174+
response = botWithConfusedKnotWithReprompts.respond(session, context, "Where is my cab ?");
175+
176+
assertThat(response, is(notNullValue()));
177+
assertThat(response.getResponse(), startsWith("I'm struggling with that one. Do you want me to call our service line for you?"));
178+
assertThat(response.isAskResponse(), is(true));
145179

180+
assertThat(SessionUtils.getReprompt(session), is("Would you like me to call our service line?"));
181+
182+
assertThat(response.getHint(), is("Yes or no"));
183+
184+
assertThat(response.getQuickReplies().size(), is(2));
185+
List<String> quickReplies = response.getQuickReplies();
186+
assertThat(quickReplies.get(0), is("Yes"));
187+
assertThat(quickReplies.get(1), is("No"));
188+
}
146189
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"inkVersion":17,"root":["\n","\n","\n","\n","\n","\n",{"->":"start"},"done",{"start":[["ev",{"^->":"start.0.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^OrderTaxi",{"->":"$r","var":true},null],"c":["ev",{"^->":"start.0.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n",{"->":"order_taxi"},"\n",{"#f":7}]}],["ev",{"^->":"start.1.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^CancelTaxi",{"->":"$r","var":true},null],"c":["ev",{"^->":"start.1.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n",{"->":"cancel_taxi"},"\n",{"#f":7}]}],["ev",{"^->":"start.2.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^WhereTaxi",{"->":"$r","var":true},null],"c":["ev",{"^->":"start.2.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n",{"->":"where_taxi"},"\n",{"#f":7}]}],{"#f":3}],"order_taxi":[[[["G>",["ev",{"VAR?":"address"},"str","^","/str","==","/ev",{"->":".^.b","c":true},{"b":[{"->":"order_taxi.request_address"},{"->":".^.^.^.3"},null]}],[{"->":".^.b"},{"b":[{"->":"order_taxi.order_the_taxi"},{"->":".^.^.^.3"},null]}],"nop","G<",null],"\n","end",{"#f":7,"#n":"order_taxi_loop"}],null],{"request_address":[["^What is the pick up address ?","\n","^::SET_REPROMPT Where would you like to be picked up ?","\n","^::SET_HINT 123 Someplace Rd","\n",["ev",{"^->":"order_taxi.request_address.0.6.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^GaveAddress",{"->":"$r","var":true},null],"c":["ev",{"^->":"order_taxi.request_address.0.6.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n","\n",{"->":".^.^.^.g-0"},{"#f":7}]}],{"g-0":[{"->":".^.^.^.^.0.order_taxi_loop"},{"#f":7}]}],{"#f":3}],"order_the_taxi":["^::ORDER_TAXI","\n","^Taxi ",["G>","ev",{"VAR?":"taxiNo"},"out","/ev","G<",null],"^ is on its way","\n","^::ADD_ATTACHMENT type::link url::http://trackcab.example.com/t/",["G>","ev",{"VAR?":"taxiNo"},"out","/ev","G<",null],"^ title::Track your taxi here","\n","^::ADD_QUICK_REPLY Where is my taxi?","\n","^::ADD_QUICK_REPLY Cancel my taxi","\n","end",{"#f":3}],"#f":3}],"cancel_taxi":["^Your taxi has been cancelled","\n","end",{"#f":3}],"where_taxi":["^Your taxi is about 7 minutes away","\n","end",{"#f":3}],"stop":["^Ok","\n","end",{"#f":3}],"help":["^I can help you order a taxi or find out the location of your current taxi.","\n","^Try say \"Order a cab\" or \"Where is my cab\"","\n","end",{"#f":3}],"confused_bot":["^I'm sorry I'm not understanding you at all :(","\n","^If you are in a hurry, please call 555-12345 to order your taxi.","\n","end",{"#f":3}],"global decl":["ev","str","^","/str",{"VAR=":"address"},"str","^","/str",{"VAR=":"taxiNo"},"/ev","end",null],"#f":3}],"listDefs":{}}
1+
{"inkVersion":17,"root":["\n","\n","\n","\n","\n","\n",{"->":"start"},"done",{"start":[["ev",{"^->":"start.0.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^OrderTaxi",{"->":"$r","var":true},null],"c":["ev",{"^->":"start.0.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n",{"->":"order_taxi"},"\n",null]}],["ev",{"^->":"start.1.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^CancelTaxi",{"->":"$r","var":true},null],"c":["ev",{"^->":"start.1.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n",{"->":"cancel_taxi"},"\n",null]}],["ev",{"^->":"start.2.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^WhereTaxi",{"->":"$r","var":true},null],"c":["ev",{"^->":"start.2.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n",{"->":"where_taxi"},"\n",null]}],{"#f":3}],"order_taxi":[[[["G>",["ev",{"VAR?":"address"},"str","^","/str","==","/ev",{"->":".^.b","c":true},{"b":[{"->":"order_taxi.request_address"},{"->":".^.^.^.3"},null]}],[{"->":".^.b"},{"b":[{"->":"order_taxi.order_the_taxi"},{"->":".^.^.^.3"},null]}],"nop","G<",null],"\n","end",{"#n":"order_taxi_loop"}],null],{"request_address":[["^What is the pick up address ?","\n","^::SET_REPROMPT Where would you like to be picked up ?","\n","^::SET_HINT 123 Someplace Rd","\n",["ev",{"^->":"order_taxi.request_address.0.6.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^GaveAddress",{"->":"$r","var":true},null],"c":["ev",{"^->":"order_taxi.request_address.0.6.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n","\n",{"->":".^.^.^.g-0"},null]}],{"g-0":[{"->":".^.^.^.^.0.order_taxi_loop"},null]}],{"#f":3}],"order_the_taxi":["^::ORDER_TAXI","\n","^Taxi ",["G>","ev",{"VAR?":"taxiNo"},"out","/ev","G<",null],"^ is on its way","\n","^::ADD_ATTACHMENT type::link url::http://trackcab.example.com/t/",["G>","ev",{"VAR?":"taxiNo"},"out","/ev","G<",null],"^ title::Track your taxi here","\n","^::ADD_QUICK_REPLY Where is my taxi?","\n","^::ADD_QUICK_REPLY Cancel my taxi","\n","end",{"#f":3}],"#f":3}],"cancel_taxi":["^Your taxi has been cancelled","\n","end",{"#f":3}],"where_taxi":["^Your taxi is about 7 minutes away","\n","end",{"#f":3}],"stop":["^Ok","\n","end",{"#f":3}],"help":["^I can help you order a taxi or find out the location of your current taxi.","\n","^Try say \"Order a cab\" or \"Where is my cab\"","\n","end",{"#f":3}],"confused_bot":["^I'm sorry I'm not understanding you at all :(","\n","^If you are in a hurry, please call 555-12345 to order your taxi.","\n","end",{"#f":3}],"confused_bot_with_handover":["^I'm struggling with that one. Do you want me to call our service line for you?","\n","^::SET_REPROMPT Would you like me to call our service line?","\n","^::SET_HINT Yes or no","\n","^::ADD_QUICK_REPLY Yes","\n","^::ADD_QUICK_REPLY No","\n",["ev",{"^->":"confused_bot_with_handover.10.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^YesIntent",{"->":"$r","var":true},null],"c":["ev",{"^->":"confused_bot_with_handover.10.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n","\n","^Calling our service operators now. Please hold the line.","\n",null]}],["ev",{"^->":"confused_bot_with_handover.11.$r1"},{"temp=":"$r"},"str",{"->":".^.s"},[{"#n":"$r1"}],"/str","/ev",{"*":".^.c","flg":2},{"s":["^NoIntent",{"->":"$r","var":true},null],"c":["ev",{"^->":"confused_bot_with_handover.11.c.$r2"},"/ev",{"temp=":"$r"},{"->":".^.^.s"},[{"#n":"$r2"}],"\n","\n","^Okay. I'm here if you need me.","\n","end",null]}],{"#f":3}],"global decl":["ev","str","^","/str",{"VAR=":"address"},"str","^","/str",{"VAR=":"taxiNo"},"/ev","end",null],"#f":3}],"listDefs":{}}

0 commit comments

Comments
 (0)