aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2020-04-20 00:25:07 +0200
committerNao Pross <naopross@thearcway.org>2020-04-20 00:25:07 +0200
commit92401488df871b15e747515472a797bececcc728 (patch)
tree61d4721d1750eb8e52f65dcc3c9d414b98e297aa
parentHC: added emote and invite, TG: switched to HTML (diff)
downloadhchat-tg-bridge-92401488df871b15e747515472a797bececcc728.tar.gz
hchat-tg-bridge-92401488df871b15e747515472a797bececcc728.zip
Add custom server ip, and custom bridge command
-rwxr-xr-xcontrol.py38
-rw-r--r--hackchatcustom.py5
-rw-r--r--telegrambot.py4
3 files changed, 27 insertions, 20 deletions
diff --git a/control.py b/control.py
index 0ccc421..f70a8e1 100755
--- a/control.py
+++ b/control.py
@@ -1,4 +1,4 @@
-#!/bin/python
+#!/usr/bin/env python3
import datetime
import sys
@@ -71,41 +71,43 @@ def onMessage(chat, update):
sender = getUser(update)
log("[%s] %s" % (sender, message))
- if nick != config.USER:
+ if not nick.endswith("@" + config.USER):
if trip == 'null':
- toTG("[<b>%s</b>] %s" % (htmlescape(nick), htmlescape(message)))
+ toTG("<code>[{:7s}]|</code>{}".format(htmlescape(nick), htmlescape(message)))
else:
- toTG("[<b>%s</b>#%s] %s" % (htmlescape(nick), trip, htmlescape(message)))
+ toTG("<code>[{:7s}#{}]</code> {}".format(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" % htmlescape(user))
+
+ log("* %s joined" % user)
+ toTG("<code>* %s joined</code>" % htmlescape(user))
def onLeave(chat, update):
"""Callback function handling users leaving the channel."""
user = getUser(update)
- log("# %s left" % user)
- toTG("# %s left" % htmlescape(user))
+
+ log("<code>%s</code> left" % user)
+ toTG("<code>* %s left</code>" % 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))
+ log("<code>* %s</code>" % text)
+ toTG("<code>* %s</code>" % 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)))
+ log(">>> <code>%s</code> invited you to hack.chat/?%s" % (user, newChannel))
+ toTG(">>> <code>%s</code> invited you to hack.chat/?%s" % (htmlescape(user), htmlescape(newChannel)))
def startHCBot():
"""Starts the HC bot."""
global hcBot
- bot = hackchat.HackChat(config.USER_AND_PASS, config.CHANNEL)
+ bot = hackchat.HackChat(config.USER_AND_PASS, config.SERVER, config.CHANNEL)
bot.on_message += [onMessage]
bot.on_join += [onJoin]
bot.on_leave += [onLeave]
@@ -137,16 +139,20 @@ def kill():
hcBot.ws.close()
### TG-Bot Config
-def onTGMessage(text):
+def onTGMessage(update):
"""Handles receiving messages from the Telegram bot.
Currently, they are simple forwarded to the HC bot
which will then send them"""
- hcBot.send_message(text)
+ hcBot._send_packet({
+ "cmd":"bridge",
+ "text":update.message.text,
+ "nick":update.message.from_user.username,
+ })
def toTG(s):
"""Handles sending messages to the Telegram bot.
Currently, the TG bot will simply send
- the message with Markdown parsing enabled."""
+ the message with HTML parsing enabled."""
tgBot.send(s)
def startTGBot():
diff --git a/hackchatcustom.py b/hackchatcustom.py
index 67de077..e6ac476 100644
--- a/hackchatcustom.py
+++ b/hackchatcustom.py
@@ -18,7 +18,7 @@ class HackChat:
third for the nickname of the sender of the message.
"""
- def __init__(self, nick, channel="programming"):
+ def __init__(self, nick, wsurl="wss://hack.chat/chat-ws", channel="programming"):
"""Connects to a channel on https://hack.chat.
Keyword arguments:
nick -- <str>; the nickname to use upon joining the channel
@@ -26,6 +26,7 @@ class HackChat:
"""
self.nick = nick
self.channel = channel
+ self.wsurl = wsurl
self.online_users = []
self.on_message = []
self.on_join = []
@@ -58,7 +59,7 @@ class HackChat:
if self.stopped:
raise ValueError("Can't run a stopped bot.")
- self.ws = websocket.create_connection("wss://hack.chat/chat-ws")
+ self.ws = websocket.create_connection(self.wsurl)
self._send_packet({"cmd": "join", "channel": self.channel, "nick": self.nick})
self._recv_thread.start()
self._ka_thread.start()
diff --git a/telegrambot.py b/telegrambot.py
index 061c41c..774da0a 100644
--- a/telegrambot.py
+++ b/telegrambot.py
@@ -24,7 +24,7 @@ class TGBot():
self._updater = Updater(token=config.API_TOKEN)
self._dispatcher = self._updater.dispatcher
self._dispatcher.add_handler(CommandHandler('start', _onStart))
- self._dispatcher.add_handler(MessageHandler(Filters.text, self._onText))
+ self._dispatcher.add_handler(MessageHandler(Filters.update, self._onText))
self.texthandlers = []
def run(self):
@@ -39,7 +39,7 @@ class TGBot():
if chat_id != config.CHAT_ID:
return
for handler in self.texthandlers:
- handler(update.message.text)
+ handler(update)
def send(self, text):
self._updater.bot.send_message(