diff --git a/app_service.py b/app_service.py index b2e187a..da65c07 100644 --- a/app_service.py +++ b/app_service.py @@ -6,6 +6,8 @@ import asyncio import logging import mimetypes import time +import html +import datetime from urllib.parse import unquote, quote, urlparse, parse_qs from aiohttp import web, ClientSession, MultipartWriter from pprint import pprint @@ -231,11 +233,43 @@ async def aiotg_message(chat, match): print('Unknown telegram chat {}'.format(chat)) return + user_id = USER_ID_FORMAT.format(chat.sender['id']) txn_id = quote('{}:{}'.format(chat.message['message_id'], chat.id)) - message = match.group(0) - j = await send_matrix_message(room_id, user_id, txn_id, body=message, msgtype='m.text') + message = match.group(0) + if 'forward_from' in chat.message: + fw_from = chat.message['forward_from'] + if 'last_name' in fw_from: + msg_from = '{} {} (Telegram)'.format(fw_from['first_name'], fw_from['last_name']) + else: + msg_from = '{} (Telegram)'.format(fw_from['first_name']) + date = datetime.datetime.fromtimestamp(fw_from['date']) + + quoted_msg = '\n'.join(['>{}'.format(x) for x in message.split('\n')]) + quoted_msg = 'Forwarded from {}, said at {}:\n{}'.format(msg_from, date, quoted_msg) + + quoted_html = '
{}'.format(html.escape(message).replace('\n', '
{}'.format(html.escape(re_msg['text']).replace('\n', '
{}
'.format(html.escape(msg_from), html.escape(str(date)), quoted_html, html.escape(message).replace('\n', '