diff --git a/telematrix/__init__.py b/telematrix/__init__.py index 10145ec..39524f2 100644 --- a/telematrix/__init__.py +++ b/telematrix/__init__.py @@ -227,13 +227,13 @@ async def matrix_transaction(request): if content['msgtype'] == 'm.text': msg, mode = format_matrix_msg('<{}> {}', displayname, content) - await group.send_text(msg, parse_mode=mode) + response = await group.send_text(msg, parse_mode=mode) elif content['msgtype'] == 'm.notice': msg, mode = format_matrix_msg('[{}] {}', displayname, content) - await group.send_text(msg, parse_mode=mode) + response = await group.send_text(msg, parse_mode=mode) elif content['msgtype'] == 'm.emote': msg, mode = format_matrix_msg('* {} {}', displayname, content) - await group.send_text(msg, parse_mode=mode) + response = await group.send_text(msg, parse_mode=mode) elif content['msgtype'] == 'm.image': try: url = urlparse(content['url']) @@ -254,12 +254,13 @@ async def matrix_transaction(request): caption = '<{}> {} ({})'.format(displayname, content['body'], url_str) - await group.send_photo(img_file, caption=caption) + response = await group.send_photo(img_file, caption=caption) except: pass else: print('Unsupported message type {}'.format(content['msgtype'])) print(json.dumps(content, indent=4)) + elif event['type'] == 'm.room.member': if matrix_is_telegram(event['state_key']): continue @@ -300,13 +301,22 @@ async def matrix_transaction(request): msg = '> {} has joined the room'.format(displayname) if msg: - await group.send_text(msg) + response = await group.send_text(msg) elif content['membership'] == 'leave': msg = '< {} has left the room'.format(displayname) - await group.send_text(msg) + response = await group.send_text(msg) elif content['membership'] == 'ban': msg = '') - if 'text' in re_msg['from']: + if 'text' in re_msg: quoted_msg = '\n'.join(['>{}'.format(x) for x in re_msg['text'].split('\n')]) - quoted_msg = 'Reply to {} ({}):\n{}\n\n{}' \ - .format(msg_from, date, quoted_msg, message) - quoted_html = '
{}
' \ .format(html.escape(re_msg['text']) .replace('\n', '
')) - quoted_html = 'Reply to {} ({}):
{}

{}

' \ - .format(html.escape(msg_from), html.escape(str(date)), + else: + quoted_msg = '' + quoted_html = '' + + if reply_mx_id: + quoted_msg = 'Reply to {}:\n{}\n\n{}' \ + .format(reply_mx_id.displayname, quoted_msg, message) + quoted_html = 'Reply to {}:
{}

{}

' \ + .format(html.escape(room_id), html.escape(reply_mx_id.matrix_event_id), html.escape(reply_mx_id.displayname), quoted_html, html_message) else: - quoted_msg = 'Reply to {} ({})\n\n{}' \ - .format(msg_from, date, message) - quoted_html = 'Reply to {} ({})

{}

' \ - .format(html.escape(msg_from), html.escape(str(date)), - html_message) - + quoted_msg = 'Reply to {}:\n{}\n\n{}' \ + .format(msg_from, quoted_msg, message) + quoted_html = 'Reply to {}:
{}

{}

' \ + .format(html.escape(msg_from), + quoted_html, html_message) j = await send_matrix_message(room_id, user_id, txn_id, body=quoted_msg, @@ -593,6 +635,19 @@ async def aiotg_message(chat, match): await asyncio.sleep(0.5) j = await send_matrix_message(room_id, user_id, txn_id + 'join', body=message, msgtype='m.text') + elif 'event_id' in j: + name = chat.sender['first_name'] + if 'last_name' in chat.sender: + name += " " + chat.sender['last_name'] + name += " (Telegram)" + message = db.Message( + chat.message['chat']['id'], + chat.message['message_id'], + room_id, + j['event_id'], + name) + db.session.add(message) + db.session.commit() def main(): diff --git a/telematrix/database.py b/telematrix/database.py index 52f66cf..9d65099 100644 --- a/telematrix/database.py +++ b/telematrix/database.py @@ -52,6 +52,27 @@ class MatrixUser(Base): self.matrix_id = matrix_id self.name = name +class Message(Base): + """Describes a message in a room bridged between Telegram and Matrix""" + __tablename__ = "message" + + id = sa.Column(sa.Integer, primary_key=True) + tg_group_id = sa.Column(sa.BigInteger) + tg_message_id = sa.Column(sa.BigInteger) + + matrix_room_id = sa.Column(sa.String) + matrix_event_id = sa.Column(sa.String) + + displayname = sa.Column(sa.String) + + def __init__(self, tg_group_id, tg_message_id, matrix_room_id, matrix_event_id, displayname): + self.tg_group_id = tg_group_id + self.tg_message_id = tg_message_id + + self.matrix_room_id = matrix_room_id + self.matrix_event_id = matrix_event_id + + self.displayname = displayname def initialize(*args, **kwargs): """Initializes the database and creates tables if necessary."""