Просмотр исходного кода

darikrc: [telegram bot] truncate msgs at 4096 and added support for ACTION

dasman 1 год назад
Родитель
Сommit
15ffab0752
1 измененных файлов с 43 добавлено и 30 удалено
  1. 43 30
      bin/darkirc/script/bots/telegram-mirror-bot.py

+ 43 - 30
bin/darkirc/script/bots/telegram-mirror-bot.py

@@ -4,10 +4,16 @@ import html.parser
 import irc
 import signal
 from telegram import Bot
+from telegram.constants import MessageLimit
 from io import StringIO
 
 TOKEN = "..."
 TOKEN_TEST = "..."
+
+MAX_CHANNEL_LENGTH = 10
+MAX_NICK_LENGTH = 10
+MAX_MESSAGE_LENGTH = MessageLimit.MAX_TEXT_LENGTH - (MAX_CHANNEL_LENGTH + MAX_NICK_LENGTH + 16)
+
 SERVER = "127.0.0.1"
 PORT = 6645
 CHANNELS = ["#dev","#memes","#philosophy","#markets","#math","#random",]
@@ -54,7 +60,6 @@ ircc.connect(SERVER, PORT, CHANNELS, BOTNICK)
 async def main():
     while True:
         text = ircc.get_response()
-        # print(text)
         if not len(text) > 0:
             continue
         text_list = text.split(' ')
@@ -71,39 +76,47 @@ async def main():
                 continue
 
             # Strip all HTML tags
-            #message = html_to_text(message)
-            message = message.replace("<", "&lt;")
-            message = message.replace(">", "&gt;")
-            # Limit line lengths
-            message = message[:300]
-
-            # Left pad nickname
+            channel = channel.replace("<", "&lt;")
+            channel = channel.replace(">", "&gt;")
             nick = nick.replace("<", "&lt;")
             nick = nick.replace(">", "&gt;")
-            nick = nick.rjust(12)
-
-            # Limit line lengths
-            message = message[:300]
-            msg = f"<code>{channel} {nick} |</code> {message}"
-
-            # print(msg)
+            message = message.replace("<", "&lt;")
+            message = message.replace(">", "&gt;")
+    
+            # https://www.irchelp.org/protocol/ctcpspec.html
+            #
+            # "This is used by losers on IRC to simulate 'role playing' games"
+            # "Presumably other users on the channel are suitably impressed."
+            if message.find("ACTION") == 1:
+                message = nick + message[7:]
+                nick = "*"
 
             append_log(channel, nick, message)
 
-            # Keep retrying until the fucker is sent
-            while True:
-                try:
-                    async with Bot(TOKEN) as bot:
-                        await bot.send_message("@darkfi_darkirc", msg,
-                                            parse_mode="HTML",
-                                            disable_notification=True,
-                                            disable_web_page_preview=True)
-                    break
-                #except telegram.error.BadRequest:
-                #    pass
-                except:
-                    print(channel, msg)
-                    print(traceback.format_exc())
-                    await asyncio.sleep(3)
+            # Pad and left/right justify channel and nickname
+            channel = channel[:MAX_CHANNEL_LENGTH].ljust(MAX_CHANNEL_LENGTH)
+            nick = nick[:MAX_NICK_LENGTH].rjust(MAX_NICK_LENGTH)
+
+            # Send messages to Telegram in chunks
+            while len(message) > 0:
+                string_to_telegram = f"<code>{channel} {nick} |</code> {message[:MAX_MESSAGE_LENGTH]}"
+
+                # Keep retrying until the fucker is sent
+                while True:
+                    try:
+                        async with Bot(TOKEN) as bot:
+                            await bot.send_message("@darkfi_darkirc", string_to_telegram,
+                                                parse_mode="HTML",
+                                                disable_notification=True,
+                                                disable_web_page_preview=True)
+                        break
+                    #except telegram.error.BadRequest:
+                    #    pass
+                    except:
+                        print(channel, string_to_telegram)
+                        print(traceback.format_exc())
+                        await asyncio.sleep(3)
+
+                message = message[MAX_MESSAGE_LENGTH:]
 
 asyncio.run(main())