Skip to content

Commit

Permalink
add suggestion chips to welcome message
Browse files Browse the repository at this point in the history
  • Loading branch information
frogermcs committed Nov 26, 2017
1 parent 54d36b2 commit 6575bd6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
37 changes: 30 additions & 7 deletions functions/conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class Conversation {
}

_greetNewUser() {
this.dialogflowApp.ask(Str.GREETING_NEW_USER, Str.GREETING_NEW_USER_NO_INPUT_PROMPT);
if (this._isScreenAvailable()) {
this._askWithSuggestionChips(Str.GREETING_NEW_USER, Str.GREETING_USER_SUGGESTION_CHIPS)
} else {
this.dialogflowApp.ask(Str.GREETING_NEW_USER, Str.GREETING_NEW_USER_NO_INPUT_PROMPT);
}
}

_greetExistingUser() {
Expand All @@ -42,10 +46,18 @@ class Conversation {
if (userGivenName) {
formattedName = ' ' + userGivenName;
}
this.dialogflowApp.ask(
util.format(Str.GREETING_EXISTING_USER, formattedName, loggedWater),
Str.GREETING_EXISTING_USER_NO_INPUT_PROMPT
);

if (this._isScreenAvailable()) {
this._askWithSuggestionChips(
util.format(Str.GREETING_EXISTING_USER, formattedName, loggedWater),
Str.GREETING_USER_SUGGESTION_CHIPS
);
} else {
this.dialogflowApp.ask(
util.format(Str.GREETING_EXISTING_USER, formattedName, loggedWater),
Str.GREETING_EXISTING_USER_NO_INPUT_PROMPT
);
}
});
}

Expand Down Expand Up @@ -135,10 +147,9 @@ class Conversation {

//Intent facts_drinking_water
getFactForDrinkingWater() {
const screenAvailable = this.dialogflowApp.hasSurfaceCapability(this.dialogflowApp.SurfaceCapabilities.SCREEN_OUTPUT);
const waterFact = this.factsRepository.getRandomWaterFact();
let speechResponse;
if (screenAvailable) {
if (this._isScreenAvailable()) {
speechResponse = this.factsRepository.getWaterFactRichResponse(waterFact);
} else {
speechResponse = this.factsRepository.getWaterFactAudioTextResponse(waterFact);
Expand All @@ -149,6 +160,18 @@ class Conversation {
_getCurrentUserId() {
return this.dialogflowApp.getUser().userId;
}

_isScreenAvailable() {
return this.dialogflowApp.hasSurfaceCapability(this.dialogflowApp.SurfaceCapabilities.SCREEN_OUTPUT);
}

_askWithSuggestionChips(speech, suggestions) {
this.dialogflowApp.ask(this.dialogflowApp
.buildRichResponse()
.addSimpleResponse(speech)
.addSuggestions(suggestions)
);
}
}

module.exports = Conversation;
1 change: 1 addition & 0 deletions functions/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
`How much water did you drink since last time?`,
`See you later!`
],
GREETING_USER_SUGGESTION_CHIPS: ['100ml', '200ml', '500ml', '1L'],
WATER_LOGGED_NOW: `Ok, I’ve added %s%s of water to your daily log. In sum you have drunk %sml today. Let me know when you drink more! See you later.`,
WATER_LOGGED_OVERALL: `In sum you have drunk %sml today. Let me know when you drink more! See you later.`,

Expand Down
49 changes: 48 additions & 1 deletion functions/test/conversation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('Conversation', () => {
const userManagerStub = sinon.stub(userManagerInstance, 'isFirstUsage').resolves(true);
const userManagerMock = sinon.mock(userManagerInstance);
const dialogFlowStub = sinon.stub(dialogFlowAppInstance, 'ask').returns(true);
const dialogFlowStub2 = sinon.stub(dialogFlowAppInstance, 'hasSurfaceCapability').returns(false);

userManagerMock
.expects('saveAssistantUser')
Expand All @@ -64,25 +65,61 @@ describe('Conversation', () => {
done();

dialogFlowStub.restore();
dialogFlowStub2.restore();
userManagerStub.restore();
});
});

it('Should greet new user', (done) => {
it('Should greet new user with audio when screen isnt available', (done) => {
const userManagerStub = sinon.stub(userManagerInstance, 'isFirstUsage').resolves(true);
const userManagerStub2 = sinon.stub(userManagerInstance, 'saveAssistantUser');
const dialogFlowAppMock = sinon.mock(dialogFlowAppInstance);
dialogFlowAppMock
.expects('ask')
.once().withArgs(Str.GREETING_NEW_USER, Str.GREETING_NEW_USER_NO_INPUT_PROMPT)
.returns(true);
dialogFlowAppMock
.expects('hasSurfaceCapability')
.once().withArgs(dialogFlowAppInstance.SurfaceCapabilities.SCREEN_OUTPUT)
.returns(false);

conversationInstance.actionWelcomeUser().then(() => {
dialogFlowAppMock.verify();
done();

userManagerStub.restore();
userManagerStub2.restore();
dialogFlowAppMock.restore();
});
});

it('Should greet new user with response and suggestion chips when screen is available', (done) => {
const userManagerStub = sinon.stub(userManagerInstance, 'isFirstUsage').resolves(true);
const userManagerStub2 = sinon.stub(userManagerInstance, 'saveAssistantUser');
const dialogFlowAppMock = sinon.mock(dialogFlowAppInstance);

const expectedResponse = "expected_response";
const addSuggestionsStub = sinon.stub().returns(expectedResponse);
const addSimpleResponseStub = sinon.stub().returns({addSuggestions: addSuggestionsStub});
const buildRichResponseStub = sinon.stub(dialogFlowAppInstance, 'buildRichResponse').returns({addSimpleResponse: addSimpleResponseStub});

dialogFlowAppMock
.expects('ask')
.once().withArgs(expectedResponse)
.returns(true);
dialogFlowAppMock
.expects('hasSurfaceCapability')
.once().withArgs(dialogFlowAppInstance.SurfaceCapabilities.SCREEN_OUTPUT)
.returns(true);

conversationInstance.actionWelcomeUser().then(() => {
dialogFlowAppMock.verify();
done();

userManagerStub.restore();
userManagerStub2.restore();
buildRichResponseStub.restore();
dialogFlowAppMock.restore();
});
});

Expand All @@ -95,6 +132,11 @@ describe('Conversation', () => {
const dialogFlowAppMock = sinon.mock(dialogFlowAppInstance);
const loadAssistantUserStub = sinon.stub(userManagerInstance, 'loadAssistantUser').resolves(exampleUser);

dialogFlowAppMock
.expects('hasSurfaceCapability')
.once().withArgs(dialogFlowAppInstance.SurfaceCapabilities.SCREEN_OUTPUT)
.returns(false);

dialogFlowAppMock
.expects('ask')
.once()
Expand Down Expand Up @@ -123,6 +165,11 @@ describe('Conversation', () => {
const dialogFlowAppMock = sinon.mock(dialogFlowAppInstance);
const loadAssistantUserStub = sinon.stub(userManagerInstance, 'loadAssistantUser').resolves({userId: "123"});

dialogFlowAppMock
.expects('hasSurfaceCapability')
.once().withArgs(dialogFlowAppInstance.SurfaceCapabilities.SCREEN_OUTPUT)
.returns(false);

dialogFlowAppMock
.expects('ask')
.once()
Expand Down

0 comments on commit 6575bd6

Please sign in to comment.