diff options
author | Tim Schmidt <tim.schmidt@ewe.net> | 2018-12-15 12:30:48 +0100 |
---|---|---|
committer | Tim Schmidt <tim.schmidt@ewe.net> | 2018-12-15 12:30:48 +0100 |
commit | d2b83008c9e6ec9226bac71ca79bfa2f3c999d46 (patch) | |
tree | e26289c7ece76ef98aa523bac0459e2d048f4bcc | |
parent | disabled debug logging (diff) | |
download | hchat-tg-bridge-d2b83008c9e6ec9226bac71ca79bfa2f3c999d46.tar.gz hchat-tg-bridge-d2b83008c9e6ec9226bac71ca79bfa2f3c999d46.zip |
HC: added emote and invite, TG: switched to HTML
-rwxr-xr-x | control.py | 29 | ||||
-rw-r--r-- | hackchatcustom.py | 9 | ||||
-rw-r--r-- | telegrambot.py | 2 |
3 files changed, 34 insertions, 6 deletions
@@ -7,6 +7,7 @@ import threading import traceback import signal import logging +import html # Custom scripts import hackchatcustom as hackchat @@ -41,6 +42,9 @@ def mdescape(s): s = s.replace(x, "\\" + x) return s +def htmlescape(s): + return html.escape(s) + ### HV-Bot def getUser(update): """Convenience function that extracts a nick and tripcode (if any) from @@ -69,21 +73,34 @@ def onMessage(chat, update): log("[%s] %s" % (sender, message)) if nick != config.USER: if trip == 'null': - toTG("\\[*%s*] %s" % (mdescape(nick), mdescape(message))) + toTG("[<b>%s</b>] %s" % (htmlescape(nick), htmlescape(message))) else: - toTG("\\[*%s*#%s] %s" % (mdescape(nick), trip, mdescape(message))) + toTG("[<b>%s</b>#%s] %s" % (htmlescape(nick), trip, htmlescape(message))) def onJoin(chat, update): """Callback function handling users joining the channel.""" user = getUser(update) log("# %s joined" % user) - toTG("# %s joined" % mdescape(user)) + toTG("# %s joined" % htmlescape(user)) def onLeave(chat, update): """Callback function handling users leaving the channel.""" user = getUser(update) log("# %s left" % user) - toTG("# %s left" % mdescape(user)) + toTG("# %s left" % htmlescape(user)) + +def onEmote(chat, update): + """Callback function handling users sending emotes to the channel.""" + text = update["text"] + log("* %s" % text) + toTG("* %s" % htmlescape(text)) + +def onInvite(chat, update): + """Callback function handling users sending invite to the bot.""" + user = update["from"] + newChannel = update["invite"] + log(">>> %s invited you to hack.chat/?%s" % (user, newChannel)) + toTG(">>> %s invited you to hack.chat/?%s" % (htmlescape(user), htmlescape(newChannel))) def startHCBot(): """Starts the HC bot.""" @@ -92,6 +109,8 @@ def startHCBot(): bot.on_message += [onMessage] bot.on_join += [onJoin] bot.on_leave += [onLeave] + bot.on_emote += [onEmote] + bot.on_invite += [onInvite] hcBot = bot bot.run() @@ -156,7 +175,7 @@ def cmdOnline(bot, update): users = list(hcBot.online_users) users.sort() tgBot.send("Users online:\n%s" % - mdescape(", ".join(users))) + htmlescape(", ".join(users))) ### Common diff --git a/hackchatcustom.py b/hackchatcustom.py index 1c10adc..67de077 100644 --- a/hackchatcustom.py +++ b/hackchatcustom.py @@ -30,6 +30,8 @@ class HackChat: self.on_message = [] self.on_join = [] self.on_leave = [] + self.on_emote = [] + self.on_invite = [] self.stopped = False @@ -107,6 +109,13 @@ class HackChat: elif result["cmd"] == "onlineSet": for nick in result["nicks"]: self.online_users.append(nick) + elif result["cmd"] == "info": + if result["type"] == "emote": + for handler in list(self.on_emote): + handler(self, result) + elif result["type"] == "invite": + for handler in list(self.on_invite): + handler(self, result) def stop(self): """Gracefully stops all bot threads and closes WebSocket connection.""" diff --git a/telegrambot.py b/telegrambot.py index 314e40c..061c41c 100644 --- a/telegrambot.py +++ b/telegrambot.py @@ -45,7 +45,7 @@ class TGBot(): self._updater.bot.send_message( chat_id=config.CHAT_ID, text=text, - parse_mode=telegram.ParseMode.MARKDOWN + parse_mode=telegram.ParseMode.HTML ) def addCommand(self, command, handler): |