Simple message builder for Alexa response.
Alexa Message Builder is available as a node module on NPM.
Install it by running:
npm install alexa-message-builder --saveAfter installing the package, require it in your code:
const AlexaMessageBuilder = or with import* syntax:
* import syntax is not supported in Node.js, you need to use additional library like Babel to make it work.
After requiring it, you simply need to initialize the class, use any of available methods from the documentation below and call .get() in the end. For Example:
const AlexaMessageBuilder = const message = will return:
Building JSON responses manually is not fun and hard to read for a big JSON files. The main motivation for this message builder is to replace them with a simple and readable syntax. For example, instead of this JSON:
You can write following JavaScript code:
Package can work with any Node.js project for building Alexa app. For example, it works perfectly with Claudia Bot Builder:
const BotBuilder = AlexaMessageBuilder = moduleexports = Alexa Message Builder is still not covering 100% of Alexa JSON response, but it covers the big part of it. Here's how it works:
Require the package you previously installed from NPM:
const AlexaMessageBuilder = or with import* syntax:
* import syntax is not supported in Node.js, you need to use additional library like Babel to make it work.
After requiring it, you simply need to initialize the class, use any of available methods from the documentation below and call .get() in the end. For Example:
const AlexaMessageBuilder = const message = will return:
This generates the speech that Alexa will say as a reply to your question or command. It can be used as a response to a LaunchRequest or IntentRequest.
You can send either plain text or Speech Synthesis Markup Language (SSML).
addText method can receive a plain text and it returns a reference to this for chaining.
Example:
This method will throw an error if text is not provided.
addSSML method can receive a SSML message as a string and it returns a reference to this for chaining.
Example:
This method will throw an error if ssmlMessage is not provided.
Similar to the output speech, reprompt supports both text and SSML, and it can be used as a response to a LaunchRequest or IntentRequest.
addRepromptText method can receive a plain text and it returns a reference to this for chaining.
Example:
This method will throw an error if text is not provided.
addRepromptSSML method can receive a SSML message as a string and it returns a reference to this for chaining.
Example:
This method will throw an error if ssmlMessage is not provided.
Alexa supports 3 different types of the cards: Simple, Standard and LinkAccount. First two types are supported by this library.
Cards can only be included when sending a response to a LaunchRequest or IntentRequest.
Simple card is a card that contains a title and plain text content.
addSimpleCard method can receive title and text and it returns a reference to this for chaining.
Example:
This method will throw an error if both title and text are not provided.
Standard card is a card that contains a title, text content, and an image to display.
addStandardCard method can receive title, text and image object, and it returns a reference to this for chaining.
Example:
This method will throw an error if title, text and imageObject are not provided.
Alexa session will be closed by default, if you want to keep it opened use .keepSession() method.
keepSession method will keep the session opened. It doesn't require any params.
Example:
Alexa also allows you to store some session attributes while the session is opened. To do so with a message builder use .addSessionAttribute(key, value) method.
addSessionAttribute method can receive key and value and it returns a reference to this for chaining. Key needs to be a string and value can be in other types too.
Example:
The main body of code is in the lib directory.
The tests are in the spec directory, and should follow the structure of the corresponding source files. All executable test file names should end with -spec, so they will be automatically picked up by npm test. Any additional project files, helper classes etc that must not be directly executed by the test runner should not end with -spec. You can use the spec/helpers directory to store Jasmine helpers, that will be loaded before any test is executed.
We use Jasmine for unit and integration tests. Unless there is a very compelling reason to use something different, please continue using Jasmine for tests. The existing tests are in the spec folder. Here are some useful command shortcuts:
Run all the tests:
npm testRun only some tests:
npm test -- filter=prefixGet detailed hierarchical test name reporting:
npm test -- fullWe use ESLint for syntax consistency, and the linting rules are included in this repository. Running npm test will check the linting rules as well. Please make sure your code has no linting errors before submitting a pull request.
MIT - See LICENSE