Bridge !commands to Telegram /commands
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
* Matrix → Telegram
|
* Matrix → Telegram
|
||||||
* [x] Plaintext messages
|
* [x] Plaintext messages
|
||||||
* [x] Formatted messages
|
* [x] Formatted messages
|
||||||
* [ ] Bot commands (!command -> /command)
|
* [x] Bot commands (!command -> /command)
|
||||||
* [x] Mentions
|
* [x] Mentions
|
||||||
* [x] Rich quotes
|
* [x] Rich quotes
|
||||||
* [ ] Locations (not implemented in Riot)
|
* [ ] Locations (not implemented in Riot)
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
from .from_matrix import matrix_reply_to_telegram, matrix_to_telegram
|
from .from_matrix import matrix_reply_to_telegram, matrix_to_telegram, matrix_text_to_telegram
|
||||||
from .from_telegram import telegram_reply_to_matrix, telegram_to_matrix
|
from .from_telegram import telegram_reply_to_matrix, telegram_to_matrix
|
||||||
|
|||||||
@@ -106,6 +106,8 @@ class MatrixParser(HTMLParser):
|
|||||||
elif tag == "pre":
|
elif tag == "pre":
|
||||||
entity_type = MessageEntityPre
|
entity_type = MessageEntityPre
|
||||||
args["language"] = ""
|
args["language"] = ""
|
||||||
|
elif tag == "command":
|
||||||
|
entity_type = MessageEntityBotCommand
|
||||||
elif tag == "li":
|
elif tag == "li":
|
||||||
self._list_entry_is_new = True
|
self._list_entry_is_new = True
|
||||||
elif tag == "a":
|
elif tag == "a":
|
||||||
@@ -157,6 +159,8 @@ class MatrixParser(HTMLParser):
|
|||||||
url = self._open_tags_meta[0]
|
url = self._open_tags_meta[0]
|
||||||
if url:
|
if url:
|
||||||
text = url
|
text = url
|
||||||
|
elif previous_tag == "command":
|
||||||
|
text = f"/{text}"
|
||||||
list_entry_handled_once = False
|
list_entry_handled_once = False
|
||||||
# In order to maintain order of things like blockquotes in lists or lists in blockquotes,
|
# In order to maintain order of things like blockquotes in lists or lists in blockquotes,
|
||||||
# we can't just have ifs/elses and we need to actually loop through the open tags in order.
|
# we can't just have ifs/elses and we need to actually loop through the open tags in order.
|
||||||
@@ -180,7 +184,7 @@ class MatrixParser(HTMLParser):
|
|||||||
prefix = "* " if self._list_entry_is_new else 3 * " "
|
prefix = "* " if self._list_entry_is_new else 3 * " "
|
||||||
if not self._list_entry_is_new and not self._line_is_new:
|
if not self._list_entry_is_new and not self._line_is_new:
|
||||||
prefix = ""
|
prefix = ""
|
||||||
extra_offset = len(indent) + len(prefix)
|
extra_offset += len(indent) + len(prefix)
|
||||||
text = indent + prefix + text
|
text = indent + prefix + text
|
||||||
self._list_entry_is_new = False
|
self._list_entry_is_new = False
|
||||||
list_entry_handled_once = True
|
list_entry_handled_once = True
|
||||||
@@ -206,10 +210,20 @@ class MatrixParser(HTMLParser):
|
|||||||
self.entities.append(entity)
|
self.entities.append(entity)
|
||||||
|
|
||||||
|
|
||||||
|
command_regex = re.compile("(\s|^)!([A-Za-z0-9]+)")
|
||||||
|
|
||||||
|
|
||||||
|
def matrix_text_to_telegram(text):
|
||||||
|
text = command_regex.sub(r"\1/\2", text)
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
def matrix_to_telegram(html):
|
def matrix_to_telegram(html):
|
||||||
try:
|
try:
|
||||||
parser = MatrixParser()
|
parser = MatrixParser()
|
||||||
parser.feed(add_surrogates(html.replace("\n", "")))
|
html = html.replace("\n", "")
|
||||||
|
html = command_regex.sub(r"\1<command>\2</command>", html)
|
||||||
|
parser.feed(add_surrogates(html))
|
||||||
return remove_surrogates(parser.text.strip()), parser.entities
|
return remove_surrogates(parser.text.strip()), parser.entities
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception("Failed to convert Matrix format:\nhtml=%s", html)
|
log.exception("Failed to convert Matrix format:\nhtml=%s", html)
|
||||||
|
|||||||
@@ -573,11 +573,10 @@ class Portal:
|
|||||||
def _handle_matrix_text(self, client, message, reply_to):
|
def _handle_matrix_text(self, client, message, reply_to):
|
||||||
if "format" in message and message["format"] == "org.matrix.custom.html":
|
if "format" in message and message["format"] == "org.matrix.custom.html":
|
||||||
message, entities = formatter.matrix_to_telegram(message["formatted_body"])
|
message, entities = formatter.matrix_to_telegram(message["formatted_body"])
|
||||||
return client.send_message(self.peer, message, entities=entities,
|
return client.send_message(self.peer, message, entities=entities, reply_to=reply_to)
|
||||||
reply_to=reply_to)
|
|
||||||
else:
|
else:
|
||||||
return client.send_message(self.peer, message["body"],
|
message = formatter.matrix_text_to_telegram(message["body"])
|
||||||
reply_to=reply_to)
|
return client.send_message(self.peer, message, reply_to=reply_to)
|
||||||
|
|
||||||
async def _handle_matrix_file(self, client, message, reply_to):
|
async def _handle_matrix_file(self, client, message, reply_to):
|
||||||
file = await self.main_intent.download_file(message["url"])
|
file = await self.main_intent.download_file(message["url"])
|
||||||
|
|||||||
Reference in New Issue
Block a user