Giter VIP home page Giter VIP logo

telegram-inline-calendar's Introduction

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
  • Turkish
  • Indonesian

Supported Telegram bot libraries:

📦 Install

There are two versions:

v1.x - if you are using CommonJS modules

npm i telegram-inline-calendar

v2.x - if you are using ES modules

npm i telegram-inline-calendar@ecmascript

🎚️ Changelog (v1.x or v2.x)

🗺 API (v1.x or v2.x)

🖥️ Examples (v1.x or v2.x)

🚀 Usage

node-telegram-bot-api

CommonJS

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

const TelegramBot = require('node-telegram-bot-api');
const Calendar = require('telegram-inline-calendar');
process.env.NTBA_FIX_319 = 1;
const bot = new TelegramBot(TOKEN, {polling: true});
const calendar = new Calendar(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);
        }
    }
});

ESM

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

import TelegramBot from 'node-telegram-bot-api';
import {Calendar} from 'telegram-inline-calendar';
process.env.NTBA_FIX_319 = 1;
const bot = new TelegramBot(TOKEN, {polling: true});
const calendar = new Calendar(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)) {
        var res;
        res = calendar.clickButtonCalendar(query);
        if (res !== -1) {
            bot.sendMessage(query.message.chat.id, "You selected: " + res);
        }
    }
});

telegraf

CommonJS

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

const {Telegraf} = require('telegraf');
const Calendar = require('telegram-inline-calendar');
const bot = new Telegraf(TOKEN);
const calendar = new Calendar(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'));

ESM

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

import {Telegraf} from 'telegraf';
import {Calendar} from 'telegram-inline-calendar';
const bot = new Telegraf(TOKEN, {polling: true});
const calendar = new Calendar(bot, {
    date_format: 'DD-MM-YYYY',
    language: 'en',
    bot_api: 'telegraf'
});

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

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

telebot

CommonJS

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

const Telebot = require('telebot');
const Calendar = require('telegram-inline-calendar');
const bot = new Telebot(TOKEN);
const calendar = new Calendar(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();

ESM

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

import Telebot from 'telebot';
import {Calendar} from 'telegram-inline-calendar';
const bot = new Telebot(TOKEN, {polling: true});
const calendar = new Calendar(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)) {
        var res;
        res = calendar.clickButtonCalendar(query);
        if (res !== -1) {
            bot.sendMessage(query.message.chat.id, "You selected: " + res);
        }
    }
});
bot.connect();

grammY

CommonJS

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

const { Bot } = require('grammy');
const Calendar = require('telegram-inline-calendar');
const bot = new Bot(TOKEN);
const calendar = new Calendar(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();

ESM

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

import { Bot } from 'grammy';
import {Calendar} from 'telegram-inline-calendar';
const bot = new Bot(TOKEN, {polling: true});
const calendar = new Calendar(bot, {
    date_format: 'DD-MM-YYYY',
    language: 'en',
    bot_api: 'grammy'
});

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

bot.on("callback_query:data", (ctx) => {
    if (ctx.msg.message_id == calendar.chats.get(ctx.chat.id)) {
        var res;
        res = calendar.clickButtonCalendar(ctx);
        if (res !== -1) {
            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/tr/id)
    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>"
    start_date: false,                             //Minimum date of the calendar in the format "YYYY-MM-DD" or "YYYY-MM-DD HH:mm" or "now"
    stop_date: false,                              //Maximum date of the calendar in the format "YYYY-MM-DD" or "YYYY-MM-DD HH:mm" or "now"
    custom_start_msg: false,                       //Text of the message sent with the calendar/time selector
    lock_date: false,                              //Enable blocked dates list
    lock_datetime: false                           //Enable list of blocked dates and times
}

License

The MIT License (MIT)

Copyright © 2022-2024 Dmitry Vyazin

telegram-inline-calendar's People

Contributors

hdiiofficial avatar knorpelsenf avatar scribesavant avatar vds13 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

telegram-inline-calendar's Issues

TypeError: NavCalendar is not a constructor

Hello, all options give errors. How to solve the problem?

const NavCalendar = require('telegram-inline-calendar')

ReferenceError: require is not defined in ES module scope, you can use import instead

import { NavCalendar } from 'telegram-inline-calendar'

SyntaxError: Named export 'NavCalendar' not found. The requested module 'telegram-inline-calendar' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using:
import pkg from 'telegram-inline-calendar';
const { NavCalendar } = pkg;

import pkg from 'telegram-inline-calendar'
const { NavCalendar } = pkg
console.log(pkg)
const calendar = new NavCalendar(bot, {
date_format: 'MMM D, YYYY h:mm A',
language: 'de',
start_week_day: 1,
bot_api: "telebot",
time_selector_mod: true,
time_range: "08:00-15:59",
time_step: "15m"
});

[class Calendar]
TypeError: NavCalendar is not a constructor

import * as pkg from 'telegram-inline-calendar'
const { NavCalendar } = pkg
console.log(pkg)

[Module: null prototype] { default: [class Calendar] }
TypeError: NavCalendar is not a constructor

Changing days order

Hi again! Could you please help me - how could i change this piece of code so that the days order starts from Monday?

Screenshot 2023-01-11 at 17 33 01

Get the wrong time when calling clickButtonCalendar

When I call
result = session.calendar.clickButtonCalendar(callbackQuery) to get the time from callbackQuery I receive the wrong time when selecting it from the range 00:00-00:59. For example, I choose 00:15 and result = 'YYYY-MM-DD 12:15' but definitely must be 'YYYY-MM-DD 00:15'. Am I doing something wrong?

'Bad Request' Error

Hi there! Got this error while entering /start command

Unhandled rejection Error: TELEGRAM: 400 Bad Request: BUTTON DATA_INVALID

Could you please provide some info - is the cause of this issue and how it can be solved?
Thank you

Change message

Is there a way to change the message to other than please select a date?

Error when trying to move through months

So when I press the advance button to go to the next month nothing happens. it loads for a bit but neither the month nor the days change. And when i press it again, the bot simply crashes. Here is the error log:

image.

Also when I press the go back button it changes to january and skips february. And the it gets stuck on january and it cant advance back to march.

I can't switch months 😞

First i want to thank you for your fantastic calendar 🎉

I do have a problem, I just think I don't know how to do it correctly.
I can see the callendar on the chat, but I can change month or year just once and than it stops.

I have the following Wizard scene:

const reminderWizard = (userId) => {
	return new WizardScene(
		"my-wizard",
		async (ctx) => {
			await ctx.reply(`What's your name?`);
			ctx.wizard.cursor = 0;
			return ctx.wizard.next();
		},
		async (ctx) => {
			const resp = ctx.message.text;
			ctx.scene.session.state.subject = resp;

			await ctx.reply(`What's your job?`);
			return ctx.wizard.next();
		},
		async (ctx) => {
			const resp = ctx.message.text;
			ctx.scene.session.state.description = resp;
			console.log("Received response: ", resp);


			// show calendar!!!
			calendar.startNavCalendar(ctx.message);

			return ctx.wizard.next();
		},
		async (ctx) => {
			const resp = ctx.message.text;
			console.log("Received response: ", resp);
			
			// Hmmm how does this work???
			if (
				ctx.callbackQuery.message.message_id ==
				calendar.chats.get(ctx.callbackQuery.message.chat.id)
			) {
				res = calendar.clickButtonCalendar(ctx.callbackQuery);
				if (res !== -1) {
					await ctx.reply("You selected: " + res);
				}
			} else {
				await ctx.reply(`Thank you for your payment!`);
			}
			
			return ctx.wizard.next();
		},
		async (ctx) => {
			const resp = ctx.message;

			console.log("Received response: ", resp);

			await ctx.reply("I registered your subscription");

			return await ctx.scene.leave();
		}
	);
};

My assumption is that this snippet is not enough:

if (
				ctx.callbackQuery.message.message_id ==
				calendar.chats.get(ctx.callbackQuery.message.chat.id)
			) {
				res = calendar.clickButtonCalendar(ctx.callbackQuery);
				if (res !== -1) {
					await ctx.reply("You selected: " + res);
				}
			}

Can you please help me with a solution?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.