Skip to content

Commit

Permalink
context: CallbackContext typing
Browse files Browse the repository at this point in the history
  • Loading branch information
shamaevnn committed Dec 1, 2021
1 parent 53ba079 commit d45031b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 13 deletions.
7 changes: 4 additions & 3 deletions tgbot/handlers/admin/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from django.utils.timezone import now
from telegram import ParseMode, Update
from telegram.ext import CallbackContext

from tgbot.handlers.admin import static_text
from tgbot.handlers.admin.utils import _get_csv_from_qs_values
from tgbot.models import User


def admin(update: Update, context) -> None:
def admin(update: Update, context: CallbackContext) -> None:
""" Show help info about all secret admins commands """
u = User.get_user(update, context)
if not u.is_admin:
Expand All @@ -17,7 +18,7 @@ def admin(update: Update, context) -> None:
update.message.reply_text(static_text.secret_admin_commands)


def stats(update: Update, context) -> None:
def stats(update: Update, context: CallbackContext) -> None:
""" Show help info about all secret admins commands """
u = User.get_user(update, context)
if not u.is_admin:
Expand All @@ -36,7 +37,7 @@ def stats(update: Update, context) -> None:
)


def export_users(update: Update, context) -> None:
def export_users(update: Update, context: CallbackContext) -> None:
u = User.get_user(update, context)
if not u.is_admin:
update.message.reply_text(static_text.only_for_admins)
Expand Down
5 changes: 3 additions & 2 deletions tgbot/handlers/broadcast_message/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import telegram
from telegram import Update
from telegram.ext import CallbackContext

from .manage_data import CONFIRM_DECLINE_BROADCAST, CONFIRM_BROADCAST
from .keyboards import keyboard_confirm_decline_broadcasting
Expand All @@ -11,7 +12,7 @@
from tgbot.tasks import broadcast_message


def broadcast_command_with_message(update: Update, context):
def broadcast_command_with_message(update: Update, context: CallbackContext):
""" Type /broadcast <some_text>. Then check your message in HTML format and broadcast to users."""
u = User.get_user(update, context)

Expand Down Expand Up @@ -44,7 +45,7 @@ def broadcast_command_with_message(update: Update, context):
)


def broadcast_decision_handler(update: Update, context) -> None:
def broadcast_decision_handler(update: Update, context: CallbackContext) -> None:
# callback_data: CONFIRM_DECLINE_BROADCAST variable from manage_data.py
""" Entered /broadcast <some_text>.
Shows text in HTML style with two buttons:
Expand Down
5 changes: 3 additions & 2 deletions tgbot/handlers/location/handlers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import telegram
from telegram import Update
from telegram.ext import CallbackContext

from tgbot.handlers.location.static_text import share_location, thanks_for_location
from tgbot.handlers.location.keyboards import send_location_keyboard
from tgbot.models import User, Location


def ask_for_location(update: Update, context) -> None:
def ask_for_location(update: Update, context: CallbackContext) -> None:
""" Entered /ask_location command"""
u = User.get_user(update, context)

Expand All @@ -17,7 +18,7 @@ def ask_for_location(update: Update, context) -> None:
)


def location_handler(update: Update, context) -> None:
def location_handler(update: Update, context: CallbackContext) -> None:
# receiving user's location
u = User.get_user(update, context)
lat, lon = update.message.location.latitude, update.message.location.longitude
Expand Down
5 changes: 3 additions & 2 deletions tgbot/handlers/onboarding/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from django.utils import timezone
from telegram import ParseMode, Update
from telegram.ext import CallbackContext

from tgbot.handlers.onboarding import static_text
from tgbot.handlers.utils.info import extract_user_data_from_update
from tgbot.models import User
from tgbot.handlers.onboarding.keyboards import make_keyboard_for_start_command


def command_start(update: Update, context) -> None:
def command_start(update: Update, context: CallbackContext) -> None:
u, created = User.get_user_and_created(update, context)

if created:
Expand All @@ -21,7 +22,7 @@ def command_start(update: Update, context) -> None:
reply_markup=make_keyboard_for_start_command())


def secret_level(update: Update, context) -> None:
def secret_level(update: Update, context: CallbackContext) -> None:
# callback_data: SECRET_LEVEL_BUTTON variable from manage_data.py
""" Pressed 'secret_level_button_text' after /start command"""
user_id = extract_user_data_from_update(update)['user_id']
Expand Down
3 changes: 2 additions & 1 deletion tgbot/handlers/utils/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import telegram
from telegram import Update
from telegram.ext import CallbackContext

from dtb.settings import TELEGRAM_LOGS_CHAT_ID
from tgbot.models import User


def send_stacktrace_to_tg_chat(update: Update, context) -> None:
def send_stacktrace_to_tg_chat(update: Update, context: CallbackContext) -> None:
u = User.get_user(update, context)

logging.error("Exception while handling an update:", exc_info=context.error)
Expand Down
3 changes: 2 additions & 1 deletion tgbot/handlers/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import telegram
from telegram import Update
from telegram.ext import CallbackContext

from tgbot.models import User

Expand All @@ -55,7 +56,7 @@ def _get_file_id(m: Dict) -> str:
return best_photo["file_id"]


def show_file_id(update: Update, context) -> None:
def show_file_id(update: Update, context: CallbackContext) -> None:
""" Returns file_id of the attached file/media """
u = User.get_user(update, context)

Expand Down
5 changes: 3 additions & 2 deletions tgbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.db import models
from django.db.models import QuerySet
from telegram import Update
from telegram.ext import CallbackContext

from dtb.settings import DEBUG
from tgbot.handlers.utils.info import extract_user_data_from_update
Expand All @@ -29,7 +30,7 @@ def __str__(self):
return f'@{self.username}' if self.username is not None else f'{self.user_id}'

@classmethod
def get_user_and_created(cls, update: Update, context) -> Tuple[User, bool]:
def get_user_and_created(cls, update: Update, context: CallbackContext) -> Tuple[User, bool]:
""" python-telegram-bot's Update, Context --> User instance """
data = extract_user_data_from_update(update)
u, created = cls.objects.update_or_create(user_id=data["user_id"], defaults=data)
Expand All @@ -45,7 +46,7 @@ def get_user_and_created(cls, update: Update, context) -> Tuple[User, bool]:
return u, created

@classmethod
def get_user(cls, update: Update, context) -> User:
def get_user(cls, update: Update, context: CallbackContext) -> User:
u, _ = cls.get_user_and_created(update, context)
return u

Expand Down

0 comments on commit d45031b

Please sign in to comment.