Skip to content

Commit

Permalink
Merge pull request #6 from FlymeDllVa/languages
Browse files Browse the repository at this point in the history
Added languages for #4 #5 and fix #1
  • Loading branch information
flymedllva authored Mar 19, 2021
2 parents 4068174 + bff2c21 commit 401991d
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 368 deletions.
108 changes: 4 additions & 104 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,104 +1,4 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.idea
__pycache__
dist
telebot_calendar.egg-info
55 changes: 7 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,17 @@ The file **telebot_calendar.py** used to create, modify, and retrieve user data

## Usage
To use the telebot_calendar you need to have [Telebot](https://github.com/eternnoir/pyTelegramBotAPI) installed first. Working example.py
```python
@bot.message_handler(commands=["start"])
def check_other_messages(message):
"""
Catches a message with the command "start" and sends the calendar
:param message:
:return:
"""
See the example in the file `example.py`

now = datetime.datetime.now() # Get the current date
bot.send_message(
message.chat.id,
"Selected date",
reply_markup=telebot_calendar.create_calendar(
name=calendar_1.prefix,
year=now.year,
month=now.month, # Specify the NAME of your calendar
),
)
## Languages
To set the language, add its instance to the calendar class

```python
from telebot_calendar import Calendar, RUSSIAN_LANGUAGE

@bot.callback_query_handler(func=lambda call: call.data.startswith(calendar_1.prefix))
def callback_inline(call: CallbackQuery):
"""
Обработка inline callback запросов
:param call:
:return:
"""

# At this point, we are sure that this calendar is ours. So we cut the line by the separator of our calendar
name, action, year, month, day = call.data.split(calendar_1.sep)
# Processing the calendar. Get either the date or None if the buttons are of a different type
date = telebot_calendar.calendar_query_handler(
bot=bot, call=call, name=name, action=action, year=year, month=month, day=day
)
# There are additional steps. Let's say if the date DAY is selected, you can execute your code. I sent a message.
if action == "DAY":
bot.send_message(
chat_id=call.from_user.id,
text=f"You have chosen {date.strftime('%d.%m.%Y')}",
reply_markup=ReplyKeyboardRemove(),
)
print(f"{calendar_1}: Day: {date.strftime('%d.%m.%Y')}")
elif action == "CANCEL":
bot.send_message(
chat_id=call.from_user.id,
text="Cancellation",
reply_markup=ReplyKeyboardRemove(),
)
print(f"{calendar_1}: Cancellation")
calendar = Calendar(language=RUSSIAN_LANGUAGE)
```
You can also create your own language class. If you will do it for other languages, we will be grateful to PR

## Demo
![](https://github.com/FlymeDllVa/telebot-calendar/blob/master/demo.gif)
22 changes: 12 additions & 10 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import datetime

import telebot
import telebot_calendar
from telebot_calendar import CallbackData
from telebot_calendar import Calendar, CallbackData, RUSSIAN_LANGUAGE

from telebot.types import ReplyKeyboardRemove, CallbackQuery

Expand All @@ -14,7 +13,8 @@
bot = telebot.TeleBot(API_TOKEN)

# Creates a unique calendar
calendar_1 = CallbackData("calendar_1", "action", "year", "month", "day")
calendar = Calendar(language=RUSSIAN_LANGUAGE)
calendar_1_callback = CallbackData("calendar_1", "action", "year", "month", "day")


@bot.message_handler(commands=["start"])
Expand All @@ -30,15 +30,17 @@ def check_other_messages(message):
bot.send_message(
message.chat.id,
"Selected date",
reply_markup=telebot_calendar.create_calendar(
name=calendar_1.prefix,
reply_markup=calendar.create_calendar(
name=calendar_1_callback.prefix,
year=now.year,
month=now.month, # Specify the NAME of your calendar
),
)


@bot.callback_query_handler(func=lambda call: call.data.startswith(calendar_1.prefix))
@bot.callback_query_handler(
func=lambda call: call.data.startswith(calendar_1_callback.prefix)
)
def callback_inline(call: CallbackQuery):
"""
Обработка inline callback запросов
Expand All @@ -47,9 +49,9 @@ def callback_inline(call: CallbackQuery):
"""

# At this point, we are sure that this calendar is ours. So we cut the line by the separator of our calendar
name, action, year, month, day = call.data.split(calendar_1.sep)
name, action, year, month, day = call.data.split(calendar_1_callback.sep)
# Processing the calendar. Get either the date or None if the buttons are of a different type
date = telebot_calendar.calendar_query_handler(
date = calendar.calendar_query_handler(
bot=bot, call=call, name=name, action=action, year=year, month=month, day=day
)
# There are additional steps. Let's say if the date DAY is selected, you can execute your code. I sent a message.
Expand All @@ -59,14 +61,14 @@ def callback_inline(call: CallbackQuery):
text=f"You have chosen {date.strftime('%d.%m.%Y')}",
reply_markup=ReplyKeyboardRemove(),
)
print(f"{calendar_1}: Day: {date.strftime('%d.%m.%Y')}")
print(f"{calendar_1_callback}: Day: {date.strftime('%d.%m.%Y')}")
elif action == "CANCEL":
bot.send_message(
chat_id=call.from_user.id,
text="Cancellation",
reply_markup=ReplyKeyboardRemove(),
)
print(f"{calendar_1}: Cancellation")
print(f"{calendar_1_callback}: Cancellation")


bot.polling(none_stop=True)
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

setup(
name="telebot-calendar",
version="1.1",
version="1.2",
packages=[""],
url="https://github.com/FlymeDllVa/telebot-calendar",
license="MIT",
keywords="python telebot calendar tool",
author="FlymeDllVa",
author_email="[email protected]",
description="Simple calendar for Telebot",
install_requires=["pyTelegramBotAPI",],
install_requires=[
"pyTelegramBotAPI",
],
)
Loading

0 comments on commit 401991d

Please sign in to comment.