Skip to main content

Telegram Bots datepicker & Aiogram datepicker

Project description

Aiogram datepicker widget

Demo:

aiogram-datepicker

Installing

pip install aiogram-datepicker

Simple usage

import logging
import os
from datetime import datetime

from aiogram import Bot, Dispatcher
from aiogram.types import Message, CallbackQuery
from aiogram.utils import executor

from aiogram_datepicker import Datepicker, DatepickerSettings

logging.basicConfig(level=logging.INFO)

bot = Bot(token=os.environ['API_TOKEN'])
dp = Dispatcher(bot, run_tasks_by_default=True)


def _get_datepicker_settings():
    return DatepickerSettings() #some settings


@dp.message_handler(state='*')
async def _main(message: Message):
    datepicker = Datepicker(_get_datepicker_settings())

    markup = datepicker.start_calendar()
    await message.answer('Select a date: ', reply_markup=markup)


@dp.callback_query_handler(Datepicker.datepicker_callback.filter())
async def _process_datepicker(callback_query: CallbackQuery, callback_data: dict):
    datepicker = Datepicker(_get_datepicker_settings())

    date = await datepicker.process(callback_query, callback_data)
    if date:
        await callback_query.message.answer(date.strftime('%d/%m/%Y'))

    await callback_query.answer()


if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)

Settings

DatepickerSettings(
    initial_view='day',  #available views -> day, month, year
    initial_date=datetime.now().date(),  #default date
    views={
        'day': {
            'show_weekdays': True,
            'weekdays_labels': ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'],
            'header': ['prev-year', 'days-title', 'next-year'],
            'footer': ['prev-month', 'select', 'next-month'],
            #available actions -> prev-year, days-title, next-year, prev-month, select, next-month, ignore
        },
        'month': {
            'months_labels': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            'header': ['prev-year', 'year', 'next-year'],
            'footer': ['select'],
            #available actions -> prev-year, year, next-year, select, ignore
        },
        'year': {
            'header': [],
            'footer': ['prev-years', 'next-years'],
            #available actions -> prev-years, ignore, next-years
        }
    },
    labels={
        'prev-year': '<<',
        'next-year': '>>',
        'prev-years': '<<',
        'next-years': '>>',
        'days-title': '{month} {year}',
        'selected-day': '{day} *',
        'selected-month': '{month} *',
        'present-day': '• {day} •',
        'prev-month': '<',
        'select': 'Select',
        'next-month': '>',
        'ignore': ''
    },
    custom_actions=[] #some custom actions

)

Custom action example

from aiogram_datepicker import Datepicker, DatepickerSettings, DatepickerCustomAction

class TodayAction(DatepickerCustomAction):
    action: str = 'today'
    label: str = 'Today'

    available_views = ('day',)

    def get_action(self, view: str, year: int, month: int, day: int) -> InlineKeyboardButton:
        return InlineKeyboardButton(self.label,
                                    callback_data=self._get_callback(view, self.action, year, month, day))

    async def process(self, query: CallbackQuery, view: str, _date: date) -> bool:
        if view == 'day':
            await self.set_view(query, 'day', datetime.now().date())
            return False
        elif view == 'month':
            await self.set_view(query, 'month', date(_date.year, datetime.now().date().month, _date.day))
            return False
        elif view == 'year':
            await self.set_view(query, 'month', date(datetime.now().date().year, _date.month, _date.day))
            return False

settings = DatepickerSettings(
    views={
        'day': {
            'footer': ['prev-month', 'today', 'next-month', ['select'], ['cancel']],
        },
        'month': {
            'footer': ['select', 'today']
        },
        'year': {
            'header': ['today'],
        }
    },
    custom_actions=[TodayAction]
)

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

aiogram_datepicker-0.0.6.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

aiogram_datepicker-0.0.6-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_datepicker-0.0.6.tar.gz.

File metadata

  • Download URL: aiogram_datepicker-0.0.6.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/21.8.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for aiogram_datepicker-0.0.6.tar.gz
Algorithm Hash digest
SHA256 752a0365bfec75abd85ba9db1cbbabfe6c3f7f36510af4ace0e3e7d6023966d5
MD5 04e4912054e177a0d04407ab7adc51a7
BLAKE2b-256 b2c47508e994a3dc5e8909aaff2807dfba421518cb8014eebb5561619caef2e9

See more details on using hashes here.

File details

Details for the file aiogram_datepicker-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: aiogram_datepicker-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.6 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/21.8.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.5

File hashes

Hashes for aiogram_datepicker-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 615bb020d04554d9333e3fcd6f21fd0b5f5ebcfb0080e4a1907ee97d8dc1cdd8
MD5 97f4474af557bf79f8e6aab07e99de5f
BLAKE2b-256 63617df75be909922f0ffbc4a343bcbb966ef4f51158ae9f0bcbda4f525187a3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page