/telegram-inline-calendar

Date and time picker and Inline calendar for Node.js telegram bots

Primary LanguageJavaScriptMIT LicenseMIT

telegram-inline-calendar

Date and time picker and inline calendar for Node.js telegram bots.

Bot API npm package npm download

📙 Description

Using this simple inline calendar you can allow your Telegram bot to ask dates.

Supported languages:

  • English
  • French
  • Russian
  • Spanish
  • Italian
  • German

Supported Telegram bot libraries:

📦 Install

npm i telegram-inline-calendar

🚀 Usage

node-telegram-bot-api

const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN';

const TelegramBot = require('node-telegram-bot-api');
const NavCalendar = require('telegram-inline-calendar')
process.env.NTBA_FIX_319 = 1;
const bot = new TelegramBot(TOKEN, {polling: true});
const calendar = new NavCalendar(bot, {
    date_format: 'DD-MM-YYYY',
    language: 'en'
});


bot.onText(/\/start/, (msg) => calendar.startNavCalendar(msg));

bot.on("callback_query", (query) => {
    if (query.message.message_id == calendar.chats.get(query.message.chat.id)) {
        res = calendar.clickButtonCalendar(query);
        if (res !== -1) {
            bot.sendMessage(query.message.chat.id, "You selected: " + res);
        }
    }
});

telegraf

const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN';

const {Telegraf} = require('telegraf');
const NavCalendar = require('telegram-inline-calendar')
const bot = new Telegraf(TOKEN);
const calendar = new NavCalendar(bot, {
    date_format: 'DD-MM-YYYY',
    language: 'en',
    bot_api: 'telegraf'
});

bot.start((ctx) => calendar.startNavCalendar(ctx.message));

bot.on("callback_query", (ctx) => {
    if (ctx.callbackQuery.message.message_id == calendar.chats.get(ctx.callbackQuery.message.chat.id)) {
        res = calendar.clickButtonCalendar(ctx.callbackQuery);
        if (res !== -1) {
            bot.telegram.sendMessage(ctx.callbackQuery.message.chat.id, "You selected: " + res);
        }
    }
});
bot.launch();
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));

telebot

const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN';

const Telebot = require('telebot');
const NavCalendar = require('telegram-inline-calendar')
const bot = new Telebot(TOKEN);
const calendar = new NavCalendar(bot, {
    date_format: 'DD-MM-YYYY',
    language: 'en',
    bot_api: 'telebot'
});


bot.on('/start', (msg) => calendar.startNavCalendar(msg));

bot.on("callbackQuery", (query) => {
    if (query.message.message_id == calendar.chats.get(query.message.chat.id)) {
        res = calendar.clickButtonCalendar(query);
        if (res !== -1) {
            bot.sendMessage(query.message.chat.id, "You selected: " + res);
        }
    }
});
bot.connect();

grammY

const TOKEN = process.env.TELEGRAM_TOKEN || 'YOUR_TELEGRAM_BOT_TOKEN';

const { Bot } = require('grammy');
const NavCalendar = require('telegram-inline-calendar')
const bot = new Bot(TOKEN);
const calendar = new NavCalendar(bot, {
    date_format: 'DD-MM-YYYY',
    language: 'en',
    bot_api: 'grammy'
});

bot.command('start', ctx => calendar.startNavCalendar(ctx.msg))

bot.on("callback_query:data", async (ctx) => {
    if (ctx.msg.message_id === calendar.chats.get(ctx.chat.id)) {
        res = calendar.clickButtonCalendar(ctx.callbackQuery);
        if (res !== -1) {
            await ctx.reply("You selected: " + res);
        }
    }
});
bot.start();

⚙️ Default options

{
    date_format: 'YYYY-MM-DD',                     //Datetime 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
    start_week_day: 0,                             //First day of the week(Sunday - `0`, Monday - `1`, Tuesday - `2` and so on)
    time_selector_mod: false,                      //Enable time selection after a date is selected.
    time_range: "00:00-23:59",                     //Allowed time range in "HH:mm-HH:mm" format
    time_step: "30m"                               //Time step in the format "<Time step><m | h>"
}

License

The MIT License (MIT)

Copyright © 2022-2023 Dmitry Vyazin