Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
VDS13 committed Jan 12, 2023
1 parent 1f442c3 commit 77c743c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Calendar
| [options.date_format] | <code>String</code> | <code>"YYYY-MM-DD"</code> | Date output format. |
| [options.bot_api] | <code>String</code> | <code>"node-telegram-bot-api"</code> | Telegram bot library. |
| [options.close_calendar] | <code>Boolean</code> | <code>true</code> | Close calendar after date selection. |
| [options.start_week_day] | <code>Number</code> | <code>0</code> | First day of the week (Sunday - `0`, Monday - `1`, Tuesday - `2` and so on). |

<a name="Calendar+startNavCalendar"></a>

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [1.2.0][1.2.0] - 2023-01-12

[issues#2](https://github.com/VDS13/telegram-inline-calendar/issues/2)

Added option `start_week_day`, which sets the first day of the week (Sunday - `0`, Monday - `1`, Tuesday - `2` and so on).

## [1.1.2][1.1.2] - 2023-01-02

[issues#1](https://github.com/VDS13/telegram-inline-calendar/issues/1)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ bot.connect();
date_format: 'YYYY-MM-DD', //date result format
language: 'en', //language (en/es/de/es/fr/it)
bot_api: 'node-telegram-bot-api', //telegram bot library
close_calendar: true //Close calendar after date selection
close_calendar: true, //Close calendar after date selection
start_week_day: 0 //First day of the week(Sunday - `0`, Monday - `1`, Tuesday - `2` and so on)
}
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "telegram-inline-calendar",
"version": "1.1.2",
"version": "1.2.0",
"description": "Date Selection tool & Inline calendar for Node.js telegram bots",
"main": "./index.js",
"directories": {
Expand Down
27 changes: 17 additions & 10 deletions src/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@ module.exports = class Calendar {
this.options.date_format = (typeof options.date_format === 'undefined') ? 'YYYY-MM-DD' : options.date_format;
this.options.bot_api = (typeof options.bot_api === 'undefined') ? 'node-telegram-bot-api' : options.bot_api;
this.options.close_calendar = (typeof options.bot_api === 'undefined') ? true : options.close_calendar;
this.options.start_week_day = (typeof options.start_week_day === 'undefined') ? 0 : options.start_week_day;
}
weekDaysButtons(day) {
return (day + this.options.start_week_day > 6) ? (day + this.options.start_week_day - 7) : (day + this.options.start_week_day);
}
startWeekDay(day) {
return (day - this.options.start_week_day < 0) ? (day - this.options.start_week_day + 7) : (day - this.options.start_week_day);
}
twoDigits(num) {
if (num < 10)
return ('0' + num).slice(-2);
return num
}
colRowNavigation(date, cd) {
var tmp = cd - 7 + date.getDay();
var tmp = cd - 7 + this.startWeekDay(date.getDay());
return Math.ceil(tmp/7) + 4;
}
howMuchDays(year, month) {
var date1 = new Date(year, month-1, 1);
var date2 = new Date(year, month, 1);
return Math.round((date2 - date1) / 1000 / 3600 / 24);
}
editMessageReplyMarkup(date, query) {
editMessageReplyMarkupCalendar(date, query) {
if (this.options.bot_api == 'node-telegram-bot-api')
this.bot.editMessageReplyMarkup(this.createNavigationKeyboard(date),{message_id: query.message.message_id, chat_id: query.message.chat.id});
else if (this.options.bot_api == 'telegraf')
Expand All @@ -35,7 +42,7 @@ module.exports = class Calendar {
this.bot.editMessageReplyMarkup({messageId: query.message.message_id, chatId: query.message.chat.id}, menu);
}
}
sendMessage(menu, msg) {
sendMessageCalendar(menu, msg) {
if (this.options.bot_api == 'node-telegram-bot-api')
this.bot.sendMessage(msg.chat.id, lang.select[this.options.language], menu).then((msg_promise) => this.chats.set(msg_promise.chat.id, msg_promise.message_id));
else if (this.options.bot_api == 'telegraf')
Expand Down Expand Up @@ -64,13 +71,13 @@ module.exports = class Calendar {
cnk.inline_keyboard[0][2] = {text: '>>', callback_data: 'n_' + dayjs(date).format("YYYY") + '_++'};
cnk.inline_keyboard.push([{},{},{},{},{},{},{}]);
for(j = 0; j < 7; j++) {
cnk.inline_keyboard[1][j] = {text: lang.week[this.options.language][j], callback_data: ' '};
cnk.inline_keyboard[1][j] = {text: lang.week[this.options.language][this.weekDaysButtons(j)], callback_data: ' '};
}
var d = 1;
for (i = 2; i <= cr - 2; i++) {
cnk.inline_keyboard.push([{},{},{},{},{},{},{}]);
for(j = 0; j < 7; j++) {
if ((i == 2 && j < date.getDay()) || d > cd) {
if ((i == 2 && j < this.startWeekDay(date.getDay())) || d > cd) {
cnk.inline_keyboard[i][j] = {text: ' ', callback_data: ' '};
} else {
cnk.inline_keyboard[i][j] = {text: d, callback_data: 'n_' + date.getFullYear() + '-' + this.twoDigits(date.getMonth() + 1) + '-' + this.twoDigits(d) + '_0'};
Expand All @@ -95,7 +102,7 @@ module.exports = class Calendar {
menu.replyMarkup = this.createNavigationKeyboard(now);
else
menu.reply_markup = this.createNavigationKeyboard(now);
this.sendMessage(menu, msg);
this.sendMessageCalendar(menu, msg);
}
clickButtonCalendar(query) {
if (query.data == ' ') {
Expand All @@ -109,12 +116,12 @@ module.exports = class Calendar {
case '++':
date = new Date(code[1]);
date.setFullYear(date.getFullYear() + 1);
this.editMessageReplyMarkup(date, query);
this.editMessageReplyMarkupCalendar(date, query);
break;
case '--':
date = new Date(code[1]);
date.setFullYear(date.getFullYear() - 1);
this.editMessageReplyMarkup(date, query);
this.editMessageReplyMarkupCalendar(date, query);
break;
case '+':
date = new Date(code[1]);
Expand All @@ -124,7 +131,7 @@ module.exports = class Calendar {
} else {
date.setMonth(date.getMonth() + 1);
}
this.editMessageReplyMarkup(date, query);
this.editMessageReplyMarkupCalendar(date, query);
break;
case '-':
date = new Date(code[1]);
Expand All @@ -134,7 +141,7 @@ module.exports = class Calendar {
} else {
date.setMonth(date.getMonth() - 1);
}
this.editMessageReplyMarkup(date, query);
this.editMessageReplyMarkupCalendar(date, query);
break;
case '0':
if (this.options.close_calendar === true) {
Expand Down
16 changes: 16 additions & 0 deletions src/language.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
"fr": "Veuillez sélectionner une date:",
"it": "Seleziona una data:"
},
"selecttime": {
"ru": "Пожалуйста, выберите время:",
"en": "Please select a time:",
"de": "Bitte wählen Sie eine Zeit aus:",
"es": "Seleccione una hora:",
"fr": "Veuillez sélectionner une heure:",
"it": "Seleziona un orario:"
},
"navigafion": {
"ru": "Навигационный календарь",
"en": "Navigation Calendar",
Expand All @@ -46,5 +54,13 @@
"es": "Calendario de diálogo",
"fr": "Calendrier de dialogue",
"it": "Calendario di dialogo"
},
"back": {
"ru": "Назад",
"en": "Back",
"de": "Zurück",
"es": "Atrás",
"fr": "Dos",
"it": "Indietro"
}
}

0 comments on commit 77c743c

Please sign in to comment.