Skip to content

Commit

Permalink
Merge pull request #8 from ask-utils/feature/progressive-response
Browse files Browse the repository at this point in the history
create progressive response helper
  • Loading branch information
hideokamoto committed Aug 22, 2018
2 parents 12e33d0 + 47362ef commit bdefc20
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -129,3 +129,15 @@ or
$ npm run lint -- --fix
```

### History
v0.9.0 - progressive response support
v0.8.0 - testing mock function
v0.6.0 - get all function directly
v0.5.1 - bug fix
v0.5.0 - unit test helper
v0.4.1 - bug fix
v0.4.0 - some function can get directly
v0.3.0 - add new helper functions and init jsdoc
v0.2.0 - add new helper functions
v0.1.1 - initial
30 changes: 30 additions & 0 deletions libs/progressiveResponse.js
@@ -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
2 changes: 1 addition & 1 deletion libs/system/device.js
Expand Up @@ -47,7 +47,7 @@ module.exports.getSupportedInterfaces = getSupportedInterfaces

/**
* Get device permissions
*
*E982ITVV7EK5U
* @return {string[]} - lists of permissions
* @param {string} [type='all'] - permission type
* @since 0.7.0
Expand Down
18 changes: 9 additions & 9 deletions libs/testUtils.js
@@ -1,7 +1,5 @@
const { ResponseFactory, AttributesManagerFactory, PersistenceAdapter } = require('ask-sdk-core')
const { services } = require('ask-sdk-model')

const { ServiceClientFactory, apiClient } = services
/**
* get handlerInput object to test your handler function
*
Expand All @@ -19,13 +17,13 @@ const getHandlerInput = (requestEnvelope, context = {}) => {
persistenceAdapter: PersistenceAdapter
}),
responseBuilder: ResponseFactory.init(),
serviceClientFactory: apiClient
? new ServiceClientFactory({
apiClient: apiClient,
apiEndpoint: requestEnvelope.context.System.apiEndpoint,
authorizationValue: requestEnvelope.context.System.apiAccessToken
})
: undefined
serviceClientFactory: {
getDirectiveServiceClient: () => {
return {
enqueue: (directive, endpoint, token) => ({directive, endpoint, token})
}
}
}
}
return handlerInput
}
Expand Down Expand Up @@ -62,6 +60,8 @@ const getRequestEnvelopeMock = () => {
playerActivity: 'IDLE'
},
System: {
apiEndpoint: 'https://api.amazonalexa.com',
apiAccessToken: 'exampleAccessToken',
device: {
supportedInterfaces: {
AudioPlayer: {}
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/libs/progressiveResponse.js
@@ -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'
})
})
})
})

0 comments on commit bdefc20

Please sign in to comment.