From 82d120dc5806d77a9ee42e71f0e9f36f70d4353d Mon Sep 17 00:00:00 2001 From: Davte Date: Thu, 14 May 2020 20:25:29 +0200 Subject: [PATCH] /ping command implemented /length command returns characters in text message even, not only in reply --- davtelepot/messages.py | 12 +++++++--- davtelepot/useful_tools.py | 47 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/davtelepot/messages.py b/davtelepot/messages.py index 40a2e3f..80fe1fc 100644 --- a/davtelepot/messages.py +++ b/davtelepot/messages.py @@ -1010,9 +1010,9 @@ default_unknown_command_message = { default_useful_tools_messages = { 'length_command': { 'description': { - 'en': "Use this command in reply to a message to get its length.", + 'en': "Use this command in reply to a message to get its length", 'it': "Usa questo comando in risposta a un messaggio per sapere " - "quanti caratteri contenga.", + "quanti caratteri contenga", }, 'help_section': { 'description': { @@ -1045,4 +1045,10 @@ default_useful_tools_messages = { "caratteri secondo i miei calcoli.", }, }, -} \ No newline at end of file + 'ping_command': { + 'description': { + 'en': "Check if bot is online", + 'it': "Verifica se il bot รจ online", + }, + }, +} diff --git a/davtelepot/useful_tools.py b/davtelepot/useful_tools.py index 82cc8e7..0796e45 100644 --- a/davtelepot/useful_tools.py +++ b/davtelepot/useful_tools.py @@ -6,11 +6,31 @@ from collections import OrderedDict # Project modules from .bot import Bot from .messages import default_useful_tools_messages -from .utilities import recursive_dictionary_update +from .utilities import get_cleaned_text, recursive_dictionary_update async def _length_command(bot: Bot, update: dict, user_record: OrderedDict): - if 'reply_to_message' not in update: + message_text = get_cleaned_text( + update=update, + bot=bot, + replace=[ + alias + for alias in bot.messages[ + 'useful_tools' + ][ + 'length_command' + ][ + 'language_labelled_commands' + ].values() + ] + ) + if message_text: + text = bot.get_message( + 'useful_tools', 'length_command', 'result', + user_record=user_record, update=update, + n=len(message_text) + ) + elif 'reply_to_message' not in update: text = bot.get_message( 'useful_tools', 'length_command', 'instructions', user_record=user_record, update=update @@ -31,6 +51,14 @@ async def _length_command(bot: Bot, update: dict, user_record: OrderedDict): ) +async def _ping_command(bot: Bot, update: dict): + """Return `pong` only in private chat.""" + chat_id = bot.get_chat_id(update=update) + if chat_id < 0: + return + return "Pong!" + + def init(telegram_bot: Bot, useful_tools_messages=None): """Define commands for `telegram_bot`. @@ -59,3 +87,18 @@ def init(telegram_bot: Bot, useful_tools_messages=None): ) async def length_command(bot, update, user_record): return await _length_command(bot=bot, update=update, user_record=user_record) + + @telegram_bot.command( + command='/ping', + aliases=None, + reply_keyboard_button=None, + show_in_keyboard=False, + **{ + key: val + for key, val in useful_tools_messages['ping_command'].items() + if key in ('description', 'help_section', 'language_labelled_commands') + }, + authorization_level='everybody' + ) + async def ping_command(bot, update): + return await _ping_command(bot=bot, update=update)