Built-in JavaScript libraries

We have built-in support for some useful JavaScript libraries which are accessible via api object in global scope. Here is a list of built-in libraries and short examples on how to use them.

API calls

axios

axios is a promise based HTTP client that let you to make http requests to your web services or execute arbitrary HTTP/HTTPS requests.

Follow official documentation for the details.

Dialog script
const SERVICE_URL = "http://api.openweathermap.org/data/2.5/weather";
const appid = "4acdb6432d18884ebc890c13a9c3cc85";

intent('What is the weather in $(CITY: London, Paris, Tokio...)', p => {
    const request_url = `${SERVICE_URL}?appid=${appid}&q=${p.CITY.value}&units=imperial`;
    api.axios.get(request_url)
        .then((response) => {
            console.log(response.data);
            let data = response.data;
            p.play(`The temperature in ${p.CITY.value} is ${Math.floor(data.main.temp)} degrees in Fahrenheit`);
        })
        .catch((error) => {
            console.log(error);
            p.play('Could not get weather information');
        });
});

Time

moment-timezone

Moment Timezone library is a tool to parse and display dates in any timezone. Follow the documentation page of the library to discover all available features. For example, you can use it for dates calculation and date formatting:

Dialog script
intent('What date was a $(PERIOD: week, month, year...) ago', p => {
    let date = api.moment().tz(p.timeZone);
    switch (p.PERIOD.value) {
        case "week" :  date = date.subtract(7, 'days'); break;
        case "month" : date = date.subtract(1, 'months'); break;
        case "year" :  date = date.subtract(1,  'years'); break;
    }
    p.play(`The date a ${p.PERIOD} ago was ${date.format("dddd, MMMM Do YYYY")}`);
});

Note

You always know the client’s timezone from timeZone object in intent handler.

luxon

Luxon is another modern library for working with dates and times in JS. Most valuable features of this library:

  • DateTime, Duration, and Interval types

  • Immutable, chain-able, unambiguous API

  • Parsing and formatting for common and custom formats

  • Native time zone and Intl support (no locale or tz files)

For details, see the Luxon documentation page.

Utility

lodash

Lodash is a modern JavaScript utility library delivering modularity, performance & extras. This is most usable js library which provides utility functions for common programming tasks using the functional programming paradigm. Lodash draws most of its ideas from Underscore.js and now receives maintenance from the original contributors to Underscore.js.

See the lodash website for more details and functions list.

Lodash is available to you in global scope under the _ object name. For example:

Dialog script
const colors = {
    1: 'red',
    2: 'blue',
    3: 'green',
    4: 'yellow',
    5: 'purple'
};

const colorValues = (colors) => _.join(_.values(colors), ', ');

intent(`Select $(COLOR: ${colorValues})`, p => {
    p.play(`You've chosen the ${p.COLOR.value} color`);
});