From d2b83008c9e6ec9226bac71ca79bfa2f3c999d46 Mon Sep 17 00:00:00 2001 From: Tim Schmidt Date: Sat, 15 Dec 2018 12:30:48 +0100 Subject: HC: added emote and invite, TG: switched to HTML --- control.py | 29 ++++++++++++++++++++++++----- hackchatcustom.py | 9 +++++++++ telegrambot.py | 2 +- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/control.py b/control.py index f2ae5f3..0ccc421 100755 --- a/control.py +++ b/control.py @@ -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("[%s] %s" % (htmlescape(nick), htmlescape(message))) else: - toTG("\\[*%s*#%s] %s" % (mdescape(nick), trip, mdescape(message))) + toTG("[%s#%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): -- cgit v1.2.1