[Alexa] Lightweight package to write SSML as TSX (@talkyjs/ssml)
Before Alexa Live 2020, I’m using ssml-tsx library to write a SSML as JSX(TSX). But, the library is conf […]
広告ここから
広告ここまで
目次
Before Alexa Live 2020, I’m using ssml-tsx
library to write a SSML as JSX(TSX).
But, the library is conflict to ask-sdk-jsx-for-apl
.
So, I’ve made a alt library to me.
@talkyjs/ssml
It’s a lightweight library to do it. Includes just a type definition for Alexa SSML and a helper function to render SSML to string.
Install
We can use it almost the same as ask-sdk-jsx-for-apl
.
$ npm install -S @talkyjs/ssml react react-dom
# (Optional) with ask-sdk-jsx-for-apl
$ npm install -S ask-sdk-jsx-for-apl
# (Optional) with TypeScript
$ npm install -D @types/react @types/react-dom
(Optional) check the tsconfig.json
We need to update these attributes like this.
...
"jsx": "react",
"esModuleInterop": true,
...
Write a JSX for Alexa!
Using @talkyjs/ssml and ask-sdk-jsx@for-apl, we can use JSX to make a response for Alexa request both SSML and APL.
ssml.jsx
import React from 'react'
import '@talkyjs/ssml'
export const LaunchRequestSpeech = () => {
return (
<speak>
hello It's a great thing,
and there is test.
<emphasis level="reduced">hello</emphasis>
<w role="amazon:NN">aaaa</w>
<amazon-domain name="music">Music</amazon-domain>
<amazon-effect name="whispered">aaaa</amazon-effect>
<amazon-emotion name="disappointed" intensity="high">aaaa</amazon-emotion>
</speak>
)
}
apl.jsx
import React from 'react';
import { APL, MainTemplate, Container, Text, AplDocument } from 'ask-sdk-jsx-for-apl';
export const LaunchAplDocumentFC = () => {
const launchMessage = 'Welcome to my first JSX for APL skill!';
return (
<APL theme="dark">
<MainTemplate>
<Container
alignItems="center"
justifyContent="spaceAround">
<Text
text={launchMessage}
fontSize="50px"
color="rgb(251,184,41)" />
</Container>
</MainTemplate>
</APL>
);
}
LaunchRequest.jsx
import React from 'react'
import { renderSSMLToString } from "@talkyjs/ssml";
import { AplDocument } from "ask-sdk-jsx-for-apl";
import { LaunchAplDocumentFC } from "./apl";
import { LaunchRequestSpeech } from "./ssml";
export const LaunchRequestHandler = {
canHandle(input) {
return input.requestEnvelope.request.type === 'LaunchRequest'
},
async handle(input) {
return input.responseBuilder
.speak(
renderSSMLToString(
<LaunchRequestSpeech />
)
)
.addDirective(
new AplDocument(
<LaunchAplDocumentFC />
).getDirective()
)
.getResponse()
}
}
The response
{
"directives": [
{
"document": {
"mainTemplate": {
"items": [
{
"alignItems": "center",
"items": [
{
"color": "rgb(251,184,41)",
"fontSize": "50px",
"text": "Welcome to my first JSX for APL skill!",
"type": "Text"
}
],
"justifyContent": "spaceAround",
"type": "Container"
}
],
"parameters": []
},
"theme": "dark",
"type": "APL",
"version": "1.4"
},
"type": "Alexa.Presentation.APL.RenderDocument"
}
],
"outputSpeech": {
"ssml": "<speak>hello It's a great thing, and there is test.<emphasis level=\"reduced\">hello</emphasis><w role=\"amazon:NN\">aaaa</w><amazon:domain name=\"music\">Music</amazon:domain><amazon:effect name=\"whispered\">aaaa</amazon:effect><amazon:emotion name=\"disappointed\" intensity=\"high\">aaaa</amazon:emotion></speak>",
"type": "SSML"
}
}