Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ask-utils/feature/progressive-response
create progressive response helper
- Loading branch information
Showing
5 changed files
with
77 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Create progressive response directive | ||
* | ||
* @param {object} handlerInput - from ask-sdk | ||
* @param {string} speechText - text content to speach prrogressive | ||
*/ | ||
const enqueueProgressiveResponseDirective = (handlerInput, speechText) => { | ||
// Call Alexa Directive Service. | ||
const requestEnvelope = handlerInput.requestEnvelope | ||
const directiveServiceClient = handlerInput.serviceClientFactory.getDirectiveServiceClient() | ||
|
||
const requestId = requestEnvelope.request.requestId | ||
const endpoint = requestEnvelope.context.System.apiEndpoint | ||
const token = requestEnvelope.context.System.apiAccessToken | ||
|
||
// build the progressive response directive | ||
const directive = { | ||
header: { | ||
requestId | ||
}, | ||
directive: { | ||
type: 'VoicePlayer.Speak', | ||
speech: speechText | ||
} | ||
} | ||
|
||
// send directive | ||
return directiveServiceClient.enqueue(directive, endpoint, token) | ||
} | ||
module.exports = enqueueProgressiveResponseDirective |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const assert = require('power-assert') | ||
const enqueueProgressiveResponseDirective = require('../../../libs/progressiveResponse') | ||
const { getHandlerInput, getRequestEnvelopeMock } = require('../../../libs/testUtils') | ||
const handlerInput = getHandlerInput(getRequestEnvelopeMock()) | ||
|
||
describe('libs/progressiveResponse.js', () => { | ||
describe('#enqueueProgressiveResponseDirective()', () => { | ||
it('should return true when given matched intent name', () => { | ||
const result = enqueueProgressiveResponseDirective(handlerInput, 'hello') | ||
assert.deepEqual(result, { | ||
directive: { | ||
directive: { | ||
speech: 'hello', | ||
type: 'VoicePlayer.Speak' | ||
}, | ||
header: { | ||
requestId: handlerInput.requestEnvelope.request.requestId | ||
} | ||
}, | ||
endpoint: 'https://api.amazonalexa.com', | ||
token: 'exampleAccessToken' | ||
}) | ||
}) | ||
}) | ||
}) |