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.
Working with API calls¶
To work with API calls you can use either standard http
and https
JavaScript libraries or one of the following clients.
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.
const SERVICE_URL = "http://api.openweathermap.org/data/2.5/weather";
const appid = "4acdb6432d18884ebc890c13a9c3cc85";
intent('What is the weather in $(LOC)', p => {
const request_url = `${SERVICE_URL}?appid=${appid}&q=${p.LOC}&units=imperial`;
api.axios.get(request_url)
.then((response) => {
console.log(response.data);
let data = response.data;
p.play(`The temperature in ${p.LOC} is ${Math.floor(data.main.temp)} degrees in Fahrenheit`);
})
.catch((error) => {
console.log(error);
p.play('Could not get weather information');
});
});
request¶
request.js
is a simplified HTTP request client JavaScript library that lets you make HTTP requests to your web services or execute arbitrary HTTP/HTTPS requests.
Follow official documentation for the details.
Here is a simple example how you can access JSON web services inside your script:
const SERVICE_URL = "http://api.openweathermap.org/data/2.5/weather";
const appid = "4acdb6432d18884ebc890c13a9c3cc85";
intent('What is the weather in $(LOC)', p => {
const request_url = `${SERVICE_URL}?appid=${appid}&q=${p.LOC}&units=imperial`;
api.request(request_url, (error, res, body) => {
if (error || res && res.statusCode !== 200) {
p.play('Could not get weather information');
} else {
let data = JSON.parse(body);
console.log({data});
p.play(`The temperature in ${p.LOC} is ${Math.floor(data.main.temp)} degrees in Fahrenheit`);
}
});
});
See also: Making a Web API call from the dialog script.
Working with 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:
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 more information see the 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:
const colors = {'red': 1, 'green': 2, 'blue': 3};
const COLOR_INTENT = _.join(_.map(Object.keys(colors), key => `${key}~${colors[key]}`), '|');
intent(`Select $(COLOR ${COLOR_INTENT})`, p=> {
p.play(`You have selected ${p.COLOR.value} color number ${p.COLOR.label}`);
});