Queer European MD passionate about IT
Browse Source

/ping command implemented

/length command returns characters in text message even, not only in reply
Davte 4 years ago
parent
commit
82d120dc58
2 changed files with 54 additions and 5 deletions
  1. 9 3
      davtelepot/messages.py
  2. 45 2
      davtelepot/useful_tools.py

+ 9 - 3
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.</i>",
         },
     },
-}
+    'ping_command': {
+        'description': {
+            'en': "Check if bot is online",
+            'it': "Verifica se il bot è online",
+        },
+    },
+}

+ 45 - 2
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 "<i>Pong!</i>"
+
+
 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)