Skip to content

Commit

Permalink
Update base.py
Browse files Browse the repository at this point in the history
  • Loading branch information
artembakhanov authored Jan 13, 2021
1 parent 0fc0368 commit ff280e8
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions telegram_bot_calendar/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

from dateutil.relativedelta import relativedelta

try:
from telethon import Button

TELETHON_INSTALLED = True
except ImportError:
TELETHON_INSTALLED = False

from telegram_bot_calendar.static import MONTHS, DAYS_OF_WEEK

calendar.setfirstweekday(calendar.MONDAY)
Expand Down Expand Up @@ -42,12 +49,13 @@ class TelegramCalendar:
step = None

def __init__(self, calendar_id=0, current_date=None, additional_buttons=None, locale='en', min_date=None,
max_date=None, **kwargs):
max_date=None, telethon=False, **kwargs):
"""
:param date current_date: Where calendar starts, if None the current date is used
:param view: The type of the calendar: either detailed, w/month, or w/year
"""

if current_date is None: current_date = date.today()
if min_date is None: min_date = date(1, 1, 1)
if max_date is None: max_date = date(2999, 12, 31)
Expand All @@ -59,6 +67,11 @@ def __init__(self, calendar_id=0, current_date=None, additional_buttons=None, lo
self.min_date = min_date
self.max_date = max_date

self.telethon = telethon
if self.telethon and not TELETHON_INSTALLED:
raise ImportError(
"Telethon is not installed. Please install telethon or use pip install python-telegram-bot-calendar[telethon]")

if not additional_buttons: additional_buttons = []
self.additional_buttons = rows(additional_buttons, self.size_additional_buttons)

Expand All @@ -76,10 +89,10 @@ def __init__(self, calendar_id=0, current_date=None, additional_buttons=None, lo
}

@staticmethod
def func(calendar_id=0):
def func(calendar_id=0, telethon=False):
def inn(callback):
start = CB_CALENDAR + "_" + str(calendar_id)
return callback.data.startswith(start)
return callback.decode("utf-8").startswith(start) if telethon else callback.data.startswith(start)

return inn

Expand Down Expand Up @@ -117,12 +130,23 @@ def _build_callback(self, action, step, data, *args, is_random=False, **kwargs):
return "_".join(params) + salt

def _build_button(self, text, action, step=None, data=None, is_random=False, **kwargs):
return {
'text': text,
'callback_data': self._build_callback(action, step, data, is_random=is_random)
}
if self.telethon:
return Button.inline(text=str(text), data=self._build_callback(action, step, data, is_random=is_random))
else:
return {
'text': text,
'callback_data': self._build_callback(action, step, data, is_random=is_random)
}

def _build_keyboard(self, buttons):
if self.telethon:
return buttons
return self._build_json_keyboard(buttons)

def _build_json_keyboard(self, buttons):
"""
Build keyboard in json to send to Telegram API over HTTP.
"""
return json.dumps({"inline_keyboard": buttons + self.additional_buttons})

def _valid_date(self, d):
Expand Down

0 comments on commit ff280e8

Please sign in to comment.