Zenera has built-in support for useful JavaScript libraries accessible via the api object in global scope.
API calls
axios
axios is a promise-based HTTP client that lets you make HTTP requests to your web services or execute arbitrary HTTP/HTTPS requests.
Follow the official documentation for 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 the timeZone object in the intent handler.
luxon
Luxon is another modern library for working with dates and times in JavaScript. Most valuable features of this library:
- DateTime, Duration, and Interval types
- Immutable, chainable, 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, and extras. This is the most usable JavaScript 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`);
});