Bot “Smart Scheduler” — quick in the uptake

Demonstration of “Smart Scheduler” usage
  1. Understand words with errors.
  2. Understand voice messages.
  3. Pin files to your reminders.
  4. Be integrated to your Trello board.

Brief history of creation

Why Telegram?

There are 3 reasons why I’ve decided to create bot in Telegram messenger:

  1. Huge users base
  2. Many companies locate their work chats in this messenger
  3. There are many guides in internet how to set up bot in Telegram

Why Node.js?

Simply because javascript is currently one of the most popular programming languages. I was also thinking of trying to write bot on Python or php, but I stayed with js, as it had similar syntax to familiar to me C++.

How it all started

First version of “Smart Scheduler” had a very different technical side, but served the same purpose — creating reminders from natural speech. It was just a pilot project to see if my hypothesis was correct, so I didn’t bother myself about the quality of code. Main goal was to create MVP, so the whole application was nothing but a monolith. I presented it at russian IT-themed web-site Habrahabr. Then, after seeing that people appreciate the idea of project, I decided to improve code structure and continue on developing the bot.

Bot application architecture

The operating algorithm is as follows:

Data base: PostgreSQL

Choice of DBMS is due to the fact that it’s already integrated to Heroku, where the bot is hosted, and it’s free. There are 3 tables in bot’s DB: User IDs, Chats and Schedules.

Date parsing

Parsing numbers

To extract time from natural language message, we first need to replace all numbers written in words with real numbers. For this I’ve made npm library parse-word-to-number. It takes a string and returns a string in which all possible words were replaced with numbers.
For matching words from source string with corresponding words from dictionary I’ve used Damerau-Levenshtein distance. This allows the library to understand words even with few mistakes.

parse-word-to-number features
  1. Select the next word and search for it in dictionary.
  2. If it’s found in dictionary, then:
    ⠀⠀If previous word is number:
    ⠀⠀⠀⠀If previous number rank is bigger than current, then sum current number with previous and merge them.
    ⠀⠀⠀⠀Else if current number can be multiplied by previous number, then multiply it and merge with previous number.
  3. Otherwise write it to string.

Time and text parsing

After all numbers in message were replaced with actual numbers, we can finally extract time definitions. For that I’ve made another npm library date-parser. It takes a string and returns array of objects of class ParsedDate (more information about in readme). Each of this objects represents one event with it’s time and text description. Thereby library can extract multiple reminders from string with one call.

date-parser usage
Source string simplification
Parsing using regular expressions
Unused words become a description

Timezone setting

Last but not least part of creating reminders is user’s timezone. It’s necessary to allow every people on globe to use bot correctly. To adjust timezone I’ve made two options, both of them are started via /tz command.

Send geolocation

If you’re chatting with bot from smartphone, then you can set up your timezone in 1 click: just send your geolocation and bot will automatically determine your timezone.

Type manually

Or, if you’re using telegram from PC, you can simply write your timezone GMT offset in “±HH:MM” format.
This is valid for mobile version as well.

Future updates

Even with so many usefull features, Smart Scheduler is still can be much better. My main tasks for improving this bot are:

  1. Adding ability to integrate bot with user’s Google calendar.
  2. Adding ability to define time for common words, such as “morning, day, evening, night”.
  3. Improvement of user-bot interface by removing most of text commands and replacing them with convenient inline buttons.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store