Queer European MD passionate about IT
Browse Source

/when command implemented

Davte 4 years ago
parent
commit
9ab3fb3616
3 changed files with 90 additions and 2 deletions
  1. 1 1
      davtelepot/__init__.py
  2. 28 0
      davtelepot/messages.py
  3. 61 1
      davtelepot/useful_tools.py

+ 1 - 1
davtelepot/__init__.py

@@ -11,7 +11,7 @@ __author__ = "Davide Testa"
 __email__ = "davide@davte.it"
 __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
 __license__ = "GNU General Public License v3.0"
-__version__ = "2.5.13"
+__version__ = "2.5.14"
 __maintainer__ = "Davide Testa"
 __contact__ = "t.me/davte"
 

+ 28 - 0
davtelepot/messages.py

@@ -1072,4 +1072,32 @@ default_useful_tools_messages = {
             'it': "Verifica se il bot è online",
         },
     },
+    'when_command': {
+        'description': {
+            'en': "Use this command in reply to get information about a message",
+            'it': "Usa questo comando in risposta per ottenere informazioni "
+                  "su un messaggio",
+        },
+        'help_section': None,
+        'forwarded_message': {
+            'en': "<b>— Original message —</b>",
+            'it': "<b>— Messaggio originale —</b>",
+        },
+        'instructions': {
+            'en': "Use this command in reply to a message to get its original "
+                  "sending time.",
+            'it': "Usa questo comando in risposta per ottenere l'ora di invio "
+                  "di un messaggio.",
+        },
+        'language_labelled_commands': {
+            'en': "when",
+            'it': "quando",
+        },
+        'who_when': {
+            'en': "👤 {who}\n"
+                  "🗓 {when:%Y-%m-%d ore %H:%M:%S}",
+            'it': "👤 {who}\n"
+                  "🗓 {when:%Y-%m-%d ore %H:%M:%S}",
+        },
+    }
 }

+ 61 - 1
davtelepot/useful_tools.py

@@ -1,6 +1,7 @@
 """General purpose functions for Telegram bots."""
 
 # Standard library
+import datetime
 import json
 
 from collections import OrderedDict
@@ -9,7 +10,7 @@ from collections import OrderedDict
 from .api import TelegramError
 from .bot import Bot
 from .messages import default_useful_tools_messages
-from .utilities import get_cleaned_text, recursive_dictionary_update
+from .utilities import get_cleaned_text, recursive_dictionary_update, get_user
 
 
 async def _message_info_command(bot: Bot, update: dict, language: str):
@@ -89,6 +90,53 @@ async def _ping_command(bot: Bot, update: dict):
     return "<i>Pong!</i>"
 
 
+async def _when_command(bot: Bot, update: dict, language: str):
+    reply_markup = None
+    text = ''
+    if 'reply_to_message' not in update:
+        return bot.get_message(
+            'useful_tools', 'when_command', 'instructions',
+            language=language
+        )
+    update = update['reply_to_message']
+    date = (
+        datetime.datetime.fromtimestamp(update['date'])
+        if 'date' in update
+        else None
+    )
+    text += bot.get_message(
+        'useful_tools', 'when_command', 'who_when',
+        language=language,
+        who=get_user(update['from']),
+        when=date
+    )
+    if 'forward_date' in update:
+        original_datetime= (
+            datetime.datetime.fromtimestamp(update['forward_date'])
+            if 'forward_from' in update
+            else None
+        )
+        text += "\n\n" + bot.get_message(
+            'useful_tools', 'when_command', 'forwarded_message',
+            language=language,
+            who=get_user(update['forward_from']),
+            when=original_datetime
+        ) + "\n"
+        text += bot.get_message(
+            'useful_tools', 'when_command', 'who_when',
+            language=language,
+            who=get_user(update['forward_from']),
+            when=original_datetime
+        )
+    await bot.send_message(
+        text=text,
+        reply_markup=reply_markup,
+        reply_to_message_id=update['message_id'],
+        disable_notification=True,
+        chat_id=update['chat']['id']
+    )
+
+
 def init(telegram_bot: Bot, useful_tools_messages=None):
     """Define commands for `telegram_bot`.
 
@@ -140,3 +188,15 @@ def init(telegram_bot: Bot, useful_tools_messages=None):
                           authorization_level='everybody')
     async def ping_command(bot, update):
         return await _ping_command(bot=bot, update=update)
+
+    @telegram_bot.command(command='/when',
+                          aliases=None,
+                          reply_keyboard_button=None,
+                          show_in_keyboard=False,
+                          **{key: val for key, val
+                             in useful_tools_messages['when_command'].items()
+                             if key in ('description', 'help_section',
+                                        'language_labelled_commands')},
+                          authorization_level='everybody')
+    async def when_command(bot, update, language):
+        return await _when_command(bot=bot, update=update, language=language)