Python inline calendar for telegram bots
Project description
python-telegram-bot-calendar
Very simple inline calendar for your bot.
Getting Started
This library is tested on Python 3.6 and 3.7.
Installation
pip install python-telegram-bot-calendar
Usage
There is one main class - DetailedTelegramCalendar that can be used as follows. This is the example for pyTelegramBotAPI library. Other libraries are also supported.
from telegram_bot_calendar import DetailedTelegramCalendar, LSTEP
...
@bot.message_handler(commands=['start'])
def start(m):
calendar, step = DetailedTelegramCalendar().build()
bot.send_message(m.chat.id,
f"Select {LSTEP[step]}",
reply_markup=calendar)
@bot.callback_query_handler(func=DetailedTelegramCalendar.func())
def cal(c):
result, key, step = DetailedTelegramCalendar().process(c.data)
if not result and key:
bot.edit_message_text(f"Select {LSTEP[step]}",
c.message.chat.id,
c.message.message_id,
reply_markup=key)
elif result:
bot.edit_message_text(f"You selected {result}",
c.message.chat.id,
c.message.message_id)
In start handler the calendar is created. Several arguments can be passed:
calendar_id
- small integer or string, used for calendar identification. It used when you need several different calendars (default - 0)current_date
-datetime.date
object, initial date value (default - today date)additional_buttons
- 1D list of buttons that will be added to the bottom of the calendarlocale
- eitheren
,ru
, oreo
, can be added moremin_date
andmax_date
- both are used as min and max values for the calendar
As you can see, special function that is provided should be passed to callback query handler. It will automatically work. The function takes only one argument - calendar_id
that is 0 by default.
In the body of the handler function you need to call process function on callback data. WARNING! You need to create the calendar object again if it was not saved before.
The function process
return tuple of size 3 - result
, keyboard
, step
.
result
-datetime.date
object if user finished selecting. OtherwiseNone
keyboard
- inline keyboard markup if the result is not ready. OtherwiseNone
step
-YEAR
,MONTH
, orDAY
if not ready.None
is also possible if there is no change in keyboard.
Advanced use
Several calendars
You can create as many calendars as you want. However, in order to handle them properly set different calendar_id's
when you want to distinguish them. Take a look at examples.
Date ranges
In the class constructor min_date
and max_date
- both are used as min and max values for the calendar. If you add them, the calendar will not show undesired dates. Example:
Custom style
You can also write your own code. One of the examples is redefining the steps order.
In the package you can find WMonthTelegramCalendar
and WYearTelegramCalendar
that start from day and month selecting, not from year.
You can also redefine style parameters. Example:
class MyStyleCalendar(DetailedTelegramCalendar):
# previous and next buttons style. they are emoji now!
prev_button = "⬅️"
next_button = "➡️"
# you do not want empty cells when month and year are being selected
empty_month_button = ""
empty_year_button = ""
You will get:
Custom Translation
your_translation_months = list('abcdefghijkl')
your_translation_days_of_week = list('yourtra')
class MyTranslationCalendar(DetailedTelegramCalendar):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.days_of_week['yourtransl'] = your_translation_days_of_week
self.months['yourtransl'] = your_translation_months
Examples
- simple_pytelegrambotapi.py - simple example with pyTelegramBotAPI
- simple_aiogram.py - simple example with aiogram
- simple_telethon.py - simple example with telethon
- custom_translation.py - custom translation of calendar
- date_ranges.py - define date ranges for the bot
- redefine_style.py - simple example of redefining styles
- several_calendars.py - several calendars in one bot
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/yourFeature
) - Commit your Changes (
git commit -m 'Add some yourFeature'
) - Push to the Branch (
git push origin feature/yourFeature
) - Open a Pull Request
Authors
- Artem Bakhanov - @artembakhanov
License
This project is licensed under the MIT License - see the LICENSE file for details
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python-telegram-bot-calendar-1.0.5.tar.gz
.
File metadata
- Download URL: python-telegram-bot-calendar-1.0.5.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1536021edc6d9ed8976ecb2f20869d9558ce946e10e01c8c8b66444757d70f7 |
|
MD5 | 137f3590d30018cea6e2963952df2c75 |
|
BLAKE2b-256 | 87d70b80bbdc4030c492f7d95499fe2a649905155b795fcee2841f51a3fec02c |
File details
Details for the file python_telegram_bot_calendar-1.0.5-py3-none-any.whl
.
File metadata
- Download URL: python_telegram_bot_calendar-1.0.5-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d4ce5aefcb54629b9f3189d9f0ca2a0ff3cdf763d6a986f581ffbc2b967bd4d |
|
MD5 | fcd01de2df66e8d521b01cadacd37019 |
|
BLAKE2b-256 | 92aa5c37e79a487e803a1f4b195c8a8c2ba23a663968370b7f8d4497ecebd6a2 |