From fda4cac348067440f5554a1c6dfa725a88f3f619 Mon Sep 17 00:00:00 2001 From: Davte Date: Sun, 15 Nov 2020 21:34:18 +0100 Subject: [PATCH] Import statements refactored --- davtelepot/__init__.py | 8 ++++---- davtelepot/administration_tools.py | 20 +++++++++++++------- davtelepot/api_helper.py | 4 ++-- davtelepot/bot.py | 11 ++++++++--- davtelepot/messages.py | 6 ++++++ requirements.txt | 2 ++ 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index 5f5c7cc..636569d 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -15,8 +15,8 @@ __version__ = "2.6.19" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" -from . import (administration_tools, authorization, bot, helper, languages, - suggestions, useful_tools, utilities) +from . import (administration_tools, api, authorization, bot, helper, languages, + messages, suggestions, useful_tools, utilities) -__all__ = [administration_tools, authorization, bot, helper, languages, - suggestions, useful_tools, utilities] +__all__ = [administration_tools, api, authorization, bot, helper, languages, + messages, suggestions, useful_tools, utilities] diff --git a/davtelepot/administration_tools.py b/davtelepot/administration_tools.py index 8051dbb..7721a27 100644 --- a/davtelepot/administration_tools.py +++ b/davtelepot/administration_tools.py @@ -23,7 +23,7 @@ from typing import Union, List, Tuple from sqlalchemy.exc import ResourceClosedError # Project modules -from . import messages +from .messages import default_admin_messages, default_talk_messages from .bot import Bot from .utilities import ( async_wrapper, CachedPage, Confirmator, extract, get_cleaned_text, @@ -362,7 +362,10 @@ async def _talk_button(bot: Bot, len(arguments) < 1 or type(arguments[0]) is not int ): - result = "Errore!" + result = bot.get_message( + 'talk', 'error', 'text', + update=update, user_record=user_record + ) else: with bot.db as db: other_user_record = db['users'].find_one( @@ -381,7 +384,10 @@ async def _talk_button(bot: Bot, len(arguments) < 1 or type(arguments[0]) is not int ): - result = "Errore!" + result = bot.get_message( + 'talk', 'error', 'text', + update=update, user_record=user_record + ) elif not Confirmator.get('stop_bots').confirm(telegram_id): result = bot.get_message( 'talk', 'end_session', @@ -1705,7 +1711,7 @@ async def _father_button(bot: Bot, user_record: OrderedDict, prefix='father:///', delimiter='|', data=['settings', 'edit', 'select', - selected_record['id'], 'edit_descr'] + selected_record['id'], 'edit_description'] ), make_button( text=bot.get_message( @@ -1731,7 +1737,7 @@ async def _father_button(bot: Bot, user_record: OrderedDict, ], 2 ) - elif len(data) > 3 and data[3] == 'edit_descr': + elif len(data) > 3 and data[3] == 'edit_description': result, text, reply_markup = await edit_bot_father_settings_via_message( bot=bot, user_record=user_record, @@ -1812,10 +1818,10 @@ def init(telegram_bot: Bot, ) asyncio.ensure_future(get_package_updates(telegram_bot)) if talk_messages is None: - talk_messages = messages.default_talk_messages + talk_messages = default_talk_messages telegram_bot.messages['talk'] = talk_messages if admin_messages is None: - admin_messages = messages.default_admin_messages + admin_messages = default_admin_messages telegram_bot.messages['admin'] = admin_messages db = telegram_bot.db if 'bot_father_commands' not in db.tables: diff --git a/davtelepot/api_helper.py b/davtelepot/api_helper.py index 1e63e4c..4a7e5cc 100644 --- a/davtelepot/api_helper.py +++ b/davtelepot/api_helper.py @@ -10,7 +10,7 @@ import aiohttp from bs4 import BeautifulSoup # Project modules -from . import api +from .api import TelegramBot api_url = "https://core.telegram.org/bots/api" @@ -105,7 +105,7 @@ async def print_api_methods(loop=None, """Get information from Telegram bot API web page.""" if loop is None: loop = asyncio.get_event_loop() - implemented_methods = dir(api.TelegramBot) + implemented_methods = dir(TelegramBot) async with aiohttp.ClientSession( loop=loop, timeout=aiohttp.ClientTimeout( diff --git a/davtelepot/bot.py b/davtelepot/bot.py index d71a282..f2da204 100644 --- a/davtelepot/bot.py +++ b/davtelepot/bot.py @@ -3225,9 +3225,14 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): and 'photos' in user_profile_photos and len(user_profile_photos['photos'])): current_photo = user_profile_photos['photos'][0][0] - if (user_picture_record is None - or current_photo['file_id'] - != user_picture_record['telegram_file_id']): + if ( + user_picture_record is None + or ( + isinstance(user_picture_record, dict) + and current_photo['file_id'] + != user_picture_record['telegram_file_id'] + ) + ): db['user_profile_photos'].insert(dict( user_id=user_record['id'], telegram_file_id=current_photo['file_id'], diff --git a/davtelepot/messages.py b/davtelepot/messages.py index 8d75bf9..456c50b 100644 --- a/davtelepot/messages.py +++ b/davtelepot/messages.py @@ -985,6 +985,12 @@ default_talk_messages = { 'en': 'End session?', 'it': 'Chiudere la sessione?', }, + 'error': { + 'text': { + 'en': "❌️ Error!", + 'it': "❌️ Errore!" + }, + }, 'help_text': { 'en': 'Press the button to search for user.', 'it': 'Premi il pulsante per scegliere un utente.', diff --git a/requirements.txt b/requirements.txt index 4841439..bf9c9ac 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ aiohttp bs4 dataset +beautifulsoup4 +SQLAlchemy