Add option for non-native replies. Fixes #42

This commit is contained in:
Tulir Asokan
2018-02-08 15:02:31 +02:00
parent fb90605557
commit 9eff63c220
4 changed files with 41 additions and 16 deletions
+9 -4
View File
@@ -40,10 +40,11 @@ bridge:
displayname_template: "{displayname} (Telegram)"
# Set the preferred order of user identifiers which to use in the Matrix puppet display name.
# In the (hopefully unlikely) scenario that none of the given keys are found, the numeric user ID is used.
# In the (hopefully unlikely) scenario that none of the given keys are found, the numeric user
# ID is used.
#
# If the bridge is working properly, a phone number or an username should always be known, but the other one can
# very well be empty.
# If the bridge is working properly, a phone number or an username should always be known, but
# the other one can very well be empty.
#
# Valid keys:
# "full name" (First and/or last name)
@@ -57,6 +58,9 @@ bridge:
- username
- phone number
# Whether or not to use native Matrix replies. At the time of writing, only riot-web supports
# replies and the format of them is subject to change.
native_replies: False
# The prefix for commands. Only required in non-management rooms.
command_prefix: "!tg"
@@ -67,7 +71,8 @@ bridge:
- "internal-hs.example.com"
- "@user:public.example.com"
# Admins can do things like delete portal rooms.
# Admins can do things like delete portal rooms. Here you must specify the exact MXID, domains
# are not accepted.
admins:
- "@admin:internal-hs.example.com"
+4
View File
@@ -275,6 +275,10 @@ class IntentAPI:
events.remove(event_id)
self.set_pinned_messages(room_id, events)
def get_event(self, room_id, event_id):
self.ensure_joined(room_id)
return self.client._send("GET", f"/rooms/{room_id}/event/{event_id}")
def set_typing(self, room_id, is_typing=True, timeout=5000):
self.ensure_joined(room_id)
return self.client.set_typing(room_id, is_typing, timeout)
+14 -2
View File
@@ -20,6 +20,8 @@ from collections import deque
import re
import logging
from matrix_client.errors import MatrixRequestError
from telethon.tl.types import *
from . import user as u, puppet as p
@@ -194,7 +196,7 @@ def matrix_to_telegram(html, tg_space=None):
# endregion
# region Telegram to Matrix
def telegram_event_to_matrix(evt, source):
def telegram_event_to_matrix(evt, source, native_replies=False, main_intent=None):
text = evt.message
html = telegram_to_matrix(evt.message, evt.entities) if evt.entities else None
@@ -225,7 +227,17 @@ def telegram_event_to_matrix(evt, source):
PeerChannel) else source.tgid
msg = DBMessage.query.get((evt.reply_to_msg_id, space))
if msg:
quote = f"<a href=\"https://matrix.to/#/{msg.mx_room}/{msg.mxid}\">Quote<br></a>"
if native_replies:
quote = f"<a href=\"https://matrix.to/#/{msg.mx_room}/{msg.mxid}\">Quote<br></a>"
else:
try:
event = main_intent.get_event(msg.mx_room, msg.mxid)
content = event["content"]
body = content["formatted_body"] if "formatted_body" in content else content["body"]
reply_to = f"<a href='https://matrix.to/#/{event['sender']}'>event['sender']</a>"
quote = f"Reply to {reply_to}<blockquote>{body}</blockquote>"
except (ValueError, KeyError, MatrixRequestError):
quote = "Reply to someone (failed to fetch message)<br/>"
if html:
html = quote + html
else:
+14 -10
View File
@@ -122,7 +122,7 @@ class Portal:
if len(self._dedup) > 20:
del self._dedup_mxid[self._dedup.popleft()]
return False
return None
def get_input_entity(self, user):
return user.client.get_input_entity(self.peer)
@@ -416,10 +416,11 @@ class Portal:
else:
self.log.debug("Unhandled Matrix event: %s", message)
return
self.is_duplicate(response, event_id)
tg_space = self.tgid if self.peer_type == "channel" else sender.tgid
self.is_duplicate(response, (event_id, tg_space))
self.db.add(DBMessage(
tgid=response.id,
tg_space=self.tgid if self.peer_type == "channel" else sender.tgid,
tg_space=tg_space,
mx_room=self.mxid,
mxid=event_id))
self.db.commit()
@@ -679,7 +680,9 @@ class Portal:
def handle_telegram_text(self, source, sender, evt):
self.log.debug(f"Sending {evt.message} to {self.mxid} by {sender.id}")
text, html = formatter.telegram_event_to_matrix(evt, source)
text, html = formatter.telegram_event_to_matrix(evt, source,
config["bridge.native_replies"],
self.main_intent)
sender.intent.set_typing(self.mxid, is_typing=False)
return sender.intent.send_text(self.mxid, text, html=html)
@@ -690,10 +693,12 @@ class Portal:
tg_space = self.tgid if self.peer_type == "channel" else source.tgid
temporary_identifier = f"${random.randint(1000000000000,9999999999999)}TGBRIDGETEMP"
mxid = self.is_duplicate(evt, temporary_identifier)
if mxid:
self.db.add(DBMessage(tgid=evt.id, mx_room=self.mxid, mxid=mxid, tg_space=tg_space))
self.db.commit()
duplicate_found = self.is_duplicate(evt, (temporary_identifier, tg_space))
if duplicate_found:
mxid, other_tg_space = duplicate_found
if tg_space != other_tg_space:
self.db.add(DBMessage(tgid=evt.id, mx_room=self.mxid, mxid=mxid, tg_space=tg_space))
self.db.commit()
return
if evt.message:
@@ -715,8 +720,7 @@ class Portal:
mxid = response["event_id"]
DBMessage.query \
.filter(DBMessage.mx_room == self.mxid,
DBMessage.mxid == temporary_identifier,
DBMessage.tg_space == tg_space) \
DBMessage.mxid == temporary_identifier) \
.update({"mxid": mxid})
self.db.add(DBMessage(tgid=evt.id, mx_room=self.mxid, mxid=mxid, tg_space=tg_space))
self.db.commit()