Releases: ask-utils/ask-utils
JSX Speech Script library
We're happy to announce the new package @ask-utils/speech-script
.
We can easy to write your Skill's SSML like React.
Install
$ npm i -S @ask-utils/speech-script
Usage
/** @jsx ssml */
import {
StandardSkillFactory
} from 'ask-sdk'
import {
ssml,
SpeechScriptJSX
} from '../dist/index'
import { LaunchRequest } from 'ask-sdk-model'
/**
* SSML component like React
**/
class LaunchRequestScript extends SpeechScriptJSX<LaunchRequest> {
speech() {
const { locale } = this.props.request
if (/^ja/.test(locale)) {
return (
<speak>こんにちは!<break time="0.5s"/>お元気ですか?</speak>
)
}
return (
<speak>
Hello! <break time="0.5s"/>How are you?
</speak>
)
}
reprompt() {
const { locale } = this.props.request
if (/^ja/.test(locale)) {
return (
<speak>
お元気ですか?<break time="0.5s"/>
<amazon-effect name="whispered">今日はいい日になるといいですね。</amazon-effect>
</speak>
)
}
return (
<speak>
How are you?<break time="0.5s"/>
<amazon-effect name="whispered">I hope you to have a good day.</amazon-effect>
</speak>
)
}
}
/**
* Use the SSML component in your RequestHandler
**/
export const handler = StandardSkillFactory.init()
.addRequestHandlers({
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest'
},
handle(handlerInput) {
const Speech = new LaunchRequestScript(handlerInput)
const {
speech,
reprompt,
} = Speech.create()
return handlerInput.responseBuilder
.speak(speech)
.reprompt(reprompt)
.getResponse()
}
}).lambda()
V2.0.0
Breaking change
- Update TypeScript compile options
- Add Rollup to build the code
- Fix some code to allow the new compile options
DBClient & Logger & cloneHandler
DBClient
We can access to DynamoDB easier.
If you are using S3 adapter, we have to write a aws-sdk script.
But to use this, we don't think about it.
import { HandlerInput } from 'ask-sdk-core'
import { DBClient, DBItem, getUserId } from 'ask-utils'
const handler = {
canHandle() {
return true
},
async handle(handlerInput: HandlerInput) {
// configure client
const client = new DBClient( 'MyTable' )
// Get user id
const userId = getUserId(handlerInput)
// Get DB data
const data = await client.get(userId)
// Update DB data
const score = data.count || 1
await client.put( userId, { score } )
// return response
return handlerInput.responseBuilder
.speak(`You launch the skill at ${score} times !`)
.getResponse()
}
}
Logger
We can log the skill request/response.
import * as Alexa from 'ask-sdk'
import {
RequestLogger,
ResponseLogger
} from 'ask-utils'
export const handler = Alexa.SkillBuilders.custom()
.addRequestHandlers({
...
)
.addRequestInterceptors(
RequestLogger
)
.addResponseInterceptors(
ResponseLogger
)
.lambda()
cloneHandler
We can clone your handler object.
import * as Alexa from 'ask-sdk'
import {
mergeHandler
} from 'ask-utils'
// The handler will handle the `MyIntent` request
const baseHandler: Alexa.RequestHandler = {
canHandle(handlerInput) {
if (Alexa.getRequestType(handlerInput.requestEnvelope) !== 'IntentRequest') return false
return Alexa.getIntentName(handlerInput.requestEnvelope) === 'MyIntent'
},
handle(handlerInput) {
return handlerInput.responseBuilder.speak('Hello world!').getResponse()
}
}
// And the handler supports the `AMAZON.YesIntent` and the response is same as `baseHandler`
const clonedHandler: Alexa.RequestHandler = mergeHandler(
baseHandler,
{
canHandle(handlerInput) {
if (Alexa.getRequestType(handlerInput.requestEnvelope) !== 'IntentRequest') return false
return Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.YesIntent'
}
}
)
Happy coding!
Add isLaunchRequest()
Today, we can easy to make canHandle
method by isLaunchRequest
.
before
const LaunchRequestHandler = {
canHandle (handlerInput) {
return canHandle(handlerInput, 'LaunchRequest')
},
after
const LaunchRequestHandler = {
canHandle (handlerInput) {
return isLaunchRequest(handlerInput)
},
Reminder Integrations
Today we can start to using the Alexa Reminder API.
Remind Customers of Important Tasks or Events with the Reminders API
The ASK Utils provides helpder API Client fot the reminder API.
Upgrade
$ npm i -S ask-utils@latest
Usage
PUT new reminder
const client = new RemidnerClient(handlerInput)
const payload = {...}
client.setPayload(payload)
await client.fetch('POST')
Lists reminder
const client = new RemidnerClient(handlerInput)
const lists = await client.fetch()
Delete a reminder
const client = new RemidnerClient(handlerInput)
await client.fetch('DELETE', `/v1/alerts/reminders/${id}`)
More information
Read our API Documentations.
https://ask-utils.github.io/ask-utils/RemidnerClient.html
Translation helper class
Add Translation helper class.
Docs: https://ask-utils.github.io/ask-utils/Translations.html
Usage
const LocalizationInterceptor = {
process (handlerInput) {
const t = new Translations()
t.putLocaleStrings('en-US', {
HELLO: 'Hello'
})
t.putLocaleStrings('ja-JP', {
HELLO: 'こんにちは'
})
const localizationClient = i18n.use(sprintf).init({
lng: handlerInput.requestEnvelope.request.locale,
overloadTranslationOptionHandler: sprintf.overloadTranslationOptionHandler,
resources: t.getLanguageStrings(),
returnObjects: true
})
const attributes = handlerInput.attributesManager.getRequestAttributes()
attributes.t = function (...args) {
return localizationClient.t(...args)
}
}
}
add `enqueueProgressiveResponseDirective()`
enqueueProgressiveResponseDirective()
will helps you to easy add progressive response into your Alexa Skill.
example
const { enqueueProgressiveResponseDirective } = require('ask-utils')
const GetFirstEventHandler = {
canHandle (handlerInput) {
const request = handlerInput.requestEnvelope.request
return request.type === 'IntentRequest' && request.intent.name === 'GetFirstEventIntent'
},
async handle (handlerInput) {
try {
await enqueueProgressiveResponseDirective(handlerInput, 'Please wait for a while')
} catch (err) {
// if it failed we can continue, just the user will wait longer for first response
console.log(err)
}
// call some api
const content = await get()
return responseBuilder
.speak(content)
.getResponse()
}
}