telegram-mirror-bot.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import asyncio
  2. import traceback
  3. import html.parser
  4. import irc
  5. import signal
  6. from telegram import Bot
  7. from io import StringIO
  8. TOKEN = "..."
  9. TOKEN_TEST = "..."
  10. SERVER = "127.0.0.1"
  11. PORT = 6645
  12. CHANNELS = ["#dev","#memes","#philosophy","#markets","#math","#random",]
  13. BOTNICK = "tgbridge"
  14. def signal_handler(sig, frame):
  15. print("Caught termination signal, cleaning up and exiting...")
  16. ircc.disconnect(SERVER, PORT)
  17. print("Shut down successfully")
  18. exit(0)
  19. signal.signal(signal.SIGINT, signal_handler)
  20. signal.signal(signal.SIGTERM, signal_handler)
  21. class HTMLTextExtractor(html.parser.HTMLParser):
  22. def __init__(self):
  23. super(HTMLTextExtractor, self).__init__()
  24. self.reset()
  25. self.strict = False
  26. self.convert_charrefs= True
  27. self.text = StringIO()
  28. def handle_data(self, d):
  29. self.text.write(d)
  30. def get_text(self):
  31. return self.text.getvalue()
  32. def html_to_text(html):
  33. s = HTMLTextExtractor()
  34. s.feed(html)
  35. return s.get_text()
  36. def append_log(channel, username, message):
  37. with open(f"/srv/http/log/{channel}.txt", "a") as fd:
  38. fd.write(f"<{username}> {message}\n")
  39. with open(f"/srv/http/log/all.txt", "a") as fd:
  40. fd.write(f"{channel} <{username}> {message}\n")
  41. ircc = irc.IRC()
  42. ircc.connect(SERVER, PORT, CHANNELS, BOTNICK)
  43. async def main():
  44. while True:
  45. text = ircc.get_response()
  46. # print(text)
  47. if not len(text) > 0:
  48. continue
  49. text_list = text.split(' ')
  50. nick = text_list[0].split('!')[0][1:]
  51. if text_list[1] == "PRIVMSG":
  52. channel = text_list[2]
  53. message = ' '.join(text_list[3:])
  54. # remove the prefix
  55. message = message[1:]
  56. # ignore test msgs
  57. if message.lower() == "test" or message.lower() == "echo":
  58. continue
  59. if nick == "testbot":
  60. continue
  61. # Strip all HTML tags
  62. #message = html_to_text(message)
  63. message = message.replace("<", "&lt;")
  64. message = message.replace(">", "&gt;")
  65. # Limit line lengths
  66. message = message[:300]
  67. # Left pad nickname
  68. nick = nick.replace("<", "&lt;")
  69. nick = nick.replace(">", "&gt;")
  70. nick = nick.rjust(12)
  71. # Limit line lengths
  72. message = message[:300]
  73. msg = f"<code>{channel} {nick} |</code> {message}"
  74. # print(msg)
  75. append_log(channel, nick, message)
  76. # Keep retrying until the fucker is sent
  77. while True:
  78. try:
  79. async with Bot(TOKEN) as bot:
  80. await bot.send_message("@darkfi_darkirc", msg,
  81. parse_mode="HTML",
  82. disable_notification=True,
  83. disable_web_page_preview=True)
  84. break
  85. #except telegram.error.BadRequest:
  86. # pass
  87. except:
  88. print(channel, msg)
  89. print(traceback.format_exc())
  90. await asyncio.sleep(3)
  91. asyncio.run(main())