Send error message if database could not be sent

This commit is contained in:
Davte 2020-06-20 15:32:37 +02:00
parent 1b4532e2cd
commit 5fe875e49c
Signed by: Davte
GPG Key ID: D848081D6F892DA9
2 changed files with 24 additions and 17 deletions

View File

@ -524,7 +524,7 @@ async def _stop_button(bot: Bot,
return result return result
async def _send_bot_database(bot, update, user_record): async def _send_bot_database(bot: Bot, user_record: OrderedDict, language: str):
if not all( if not all(
[ [
bot.db_url.endswith('.db'), bot.db_url.endswith('.db'),
@ -533,20 +533,21 @@ async def _send_bot_database(bot, update, user_record):
): ):
return bot.get_message( return bot.get_message(
'admin', 'db_command', 'not_sqlite', 'admin', 'db_command', 'not_sqlite',
update=update, user_record=user_record, language=language,
db_type=bot.db_url.partition(':///')[0] db_type=bot.db_url.partition(':///')[0]
) )
await bot.send_document( sent_update = await bot.send_document(
chat_id=user_record['telegram_id'], chat_id=user_record['telegram_id'],
document_path=extract(bot.db.url, starter='sqlite:///'), document_path=extract(bot.db.url, starter='sqlite:///'),
caption=bot.get_message( caption=bot.get_message(
'admin', 'db_command', 'file_caption', 'admin', 'db_command', 'file_caption',
update=update, user_record=user_record language=language
) )
) )
return bot.get_message( return bot.get_message(
'admin', 'db_command', 'db_sent', 'admin', 'db_command',
update=update, user_record=user_record ('error' if isinstance(sent_update, Exception) else 'db_sent'),
language=language
) )
@ -1879,8 +1880,10 @@ def init(telegram_bot: Bot,
description=admin_messages[ description=admin_messages[
'db_command']['description'], 'db_command']['description'],
authorization_level='admin') authorization_level='admin')
async def send_bot_database(bot, update, user_record): async def send_bot_database(bot, user_record, language):
return await _send_bot_database(bot, update, user_record) return await _send_bot_database(bot=bot,
user_record=user_record,
language=language)
@telegram_bot.command(command='/errors', @telegram_bot.command(command='/errors',
aliases=[], aliases=[],

View File

@ -37,9 +37,21 @@ default_admin_messages = {
'it': "🔄 Clicka di nuovo per confermare", 'it': "🔄 Clicka di nuovo per confermare",
}, },
'db_command': { 'db_command': {
'db_sent': {
'en': "Database sent.",
'it': "Database inviato.",
},
'description': { 'description': {
'en': "Ask for bot database via Telegram", 'en': "Ask for bot database via Telegram",
'it': "Ricevi il database del bot via Telegram" 'it': "Ricevi il database del bot via Telegram",
},
'error': {
'en': "Error sending database.",
'it': "Errore durante l'invio del database.",
},
'file_caption': {
'en': "Here is bot database.",
'it': "Ecco il database!"
}, },
'not_sqlite': { 'not_sqlite': {
'en': "Only SQLite databases may be sent via Telegram, since they " 'en': "Only SQLite databases may be sent via Telegram, since they "
@ -49,14 +61,6 @@ default_admin_messages = {
"in quanto composti di un solo file.\n" "in quanto composti di un solo file.\n"
"Questo bot ha invece un database `{db_type}`." "Questo bot ha invece un database `{db_type}`."
}, },
'file_caption': {
'en': "Here is bot database.",
'it': "Ecco il database!"
},
'db_sent': {
'en': "Database sent.",
'it': "Database inviato."
}
}, },
'error': { 'error': {
'text': { 'text': {