Customize Moment.js for My Chatbot to Sound More Like a Human
As I am developing my new reminder app that works primarily as a chatbot utilizing the latest technology of Natural Language Processing (NLP) and Natural Language Understanding (NLU).
I have realized that a chatbot shouldn’t reply with a message similar to below:
Me: Can you remind me to pick up some snacks in 2 hours?
Bot: Okay! I will remind you to pick up some snacks at 2020–03–25 14:35:29 -05:00.
It’s a common sense that this looks just wrong for a chatbot. It just looks too nerdy and confusing, even to a developer like me. So, I had to write a short node function that can convert these long, nerdy datetime values into human-friendly texts.
This is where moment.js comes into play. It’s a powerful node.js library when it comes to anything date and time related. After reading through the documentation and some Googling, I have noticed you can customize the Calendar function with your own rules to format the outputs such as below.
const moment = require('moment');moment.updateLocale('en', {
calendar: {
lastDay : '[yesterday at] LT',
sameDay : '[today at] LT',
nextDay : '[tomorrow at] LT',
lastWeek : '[last] dddd [at] LT',
nextWeek : '[on] dddd [at] LT',
sameElse : '[on] llll'
}
});
Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com
Any texts inside []
brackets are going to be rendered as is. For example, these are some of possible outputs of .calendar()
calls:
moment().add(2,'hours').calendar();
// -> today at 5:30 PMmoment().add(1,'day').calendar();
// -> tomorrow at 3:30 PMmoment().subtract(5,'days').calendar();
// -> last Thursday at 3:30 PMmoment().add(1,'month).calendar();
// -> on Mon, April 25, 2020 3:30 PM
This sets a good foundation for our chatbot to respond date and time with. For example, this is how our chatbot will respond now:
Me: Can you remind me to pick up some snacks in 2 hours?
Bot: Okay! I will remind you to pick up some snacks today at 5:30 PM.
The chatbot definitely sounds more like a human now. Here, I wanted to add a little bit more humanly flavor to the chatbot by adding some relative terms under certain conditions using .fromNow()
method of moment.js.
const datetimeToHumanTerms = (remind_at) => { // If remind_at is within the next 2 hours,
// we will use fromNow() to show more relativity
// Ex. "in 2 hours"
if (moment(remind_at).isSameOrBefore(moment().add(2,'hours'))) {
return moment(remind_at).fromNow();
} else {
return moment(remind_at).calendar();
}}
So, going back to the previous conversation example, the chatbot would respond as the following instead.
Me: Can you remind me to pick up some snacks in 2 hours?
Bot: Okay! I will remind you to pick up some snacks today in 2 hours.
There we go. Now the chatbot starts to sound more like a human. Obviously there’s a lot more to make a bot to sound like a human, I wanted to share a small part of my reminder bot’s backend algorithm which converts datetime values to human-friendly terms by utilizing moment.js library.
I hope this could help any young node/javascript developers a good starting point to handle datetime values for their applications.
If you have any questions or ideas to improve my proposed algorithm, please feel free to share. I always welcome new ideas to expand my knowledge as well as improve my applications like my reminder bot.
References
- General information on formatting in moment.js: [Moment.js | Docs — Format](https://momentjs.com/docs/#/displaying/format/)
- Relative time from now: [Moment.js | Docs — Time from Now](https://momentjs.com/docs/#/displaying/fromnow/)
- Human-friendly calendar terms in moment.js: [Moment.js | Docs — Calendar Time](https://momentjs.com/docs/#/displaying/calendar-time/)
- Customize calendar method: [Moment.js | Docs — Customize Calendar](https://momentjs.com/docs/#/customization/calendar/)