Compare commits

...

1 Commits

Author SHA1 Message Date
ccbbe5ce73 Implement bot-owners feature 2017-10-30 11:08:40 -04:00
3 changed files with 40 additions and 21 deletions

View File

@ -42,6 +42,7 @@ First, copy config.json.example to config.json. Then fill in the fields:
* `hosts.bare`: Just the (sub)domain of the server. * `hosts.bare`: Just the (sub)domain of the server.
* `user_id_format`: A Python `str.format`-style string to format user IDs as * `user_id_format`: A Python `str.format`-style string to format user IDs as
* `db_url`: A SQLAlchemy URL for the database. See the [SQLAlchemy docs](http://docs.sqlalchemy.org/en/latest/core/engines.html). * `db_url`: A SQLAlchemy URL for the database. See the [SQLAlchemy docs](http://docs.sqlalchemy.org/en/latest/core/engines.html).
* `bot_owners`: A list of matrix users the bot won't use a `<username>` prefix for. Can be left blank to print a prefix for all matrix users. Useful if you would like the bridge to replace Telegram for yourself, but also want to allow others to use the Matrix room.
**Synapse configuration** **Synapse configuration**

View File

@ -12,6 +12,12 @@
"bare": "DOMAIN.TLD" "bare": "DOMAIN.TLD"
}, },
"bot_owners": [
"@youruser:DOMAIN.TLD",
"@youruser:matrix.org",
"@youruser:example.com"
],
"user_id_format": "@telegram_{}:DOMAIN.TLD", "user_id_format": "@telegram_{}:DOMAIN.TLD",
"db_url": "sqlite:///database.db", "db_url": "sqlite:///database.db",

View File

@ -38,6 +38,11 @@ try:
MATRIX_HOST_EXT = CONFIG['hosts']['external'] MATRIX_HOST_EXT = CONFIG['hosts']['external']
MATRIX_HOST_BARE = CONFIG['hosts']['bare'] MATRIX_HOST_BARE = CONFIG['hosts']['bare']
try:
MATRIX_BOT_OWNERS = CONFIG['bot_owners']
except KeyError:
MATRIX_BOT_OWNERS = {}
MATRIX_PREFIX = MATRIX_HOST + '_matrix/client/r0/' MATRIX_PREFIX = MATRIX_HOST + '_matrix/client/r0/'
MATRIX_MEDIA_PREFIX = MATRIX_HOST + '_matrix/media/r0/' MATRIX_MEDIA_PREFIX = MATRIX_HOST + '_matrix/media/r0/'
@ -205,6 +210,9 @@ async def matrix_transaction(request):
if event['type'] == 'm.room.message': if event['type'] == 'm.room.message':
user_id = event['user_id'] user_id = event['user_id']
if user_id in MATRIX_BOT_OWNERS:
displayname = ''
else:
if matrix_is_telegram(user_id): if matrix_is_telegram(user_id):
continue continue
@ -229,13 +237,16 @@ async def matrix_transaction(request):
continue continue
if content['msgtype'] == 'm.text': if content['msgtype'] == 'm.text':
msg, mode = format_matrix_msg('<{}> {}', displayname, content) prefix = '' if displayname == '' else '<' + displayname + '> '
msg, mode = format_matrix_msg('{}{}', prefix, content)
response = await group.send_text(msg, parse_mode=mode) response = await group.send_text(msg, parse_mode=mode)
elif content['msgtype'] == 'm.notice': elif content['msgtype'] == 'm.notice':
msg, mode = format_matrix_msg('[{}] {}', displayname, content) prefix = '' if displayname == '' else '[' + displayname + '] '
msg, mode = format_matrix_msg('{}{}', prefix, content)
response = await group.send_text(msg, parse_mode=mode) response = await group.send_text(msg, parse_mode=mode)
elif content['msgtype'] == 'm.emote': elif content['msgtype'] == 'm.emote':
msg, mode = format_matrix_msg('* {} {}', displayname, content) prefix = '' if displayname == '' else '* ' + displayname + ' '
msg, mode = format_matrix_msg('{}{}', prefix, content)
response = await group.send_text(msg, parse_mode=mode) response = await group.send_text(msg, parse_mode=mode)
elif content['msgtype'] == 'm.image': elif content['msgtype'] == 'm.image':
try: try:
@ -255,7 +266,8 @@ async def matrix_transaction(request):
.format(url.netloc, quote(url.path)) .format(url.netloc, quote(url.path))
url_str = await shorten_url(url_str) url_str = await shorten_url(url_str)
caption = '<{}> {} ({})'.format(displayname, prefix = '' if displayname == '' else '<' + displayname + '> '
caption = '{}{} ({})'.format(prefix,
content['body'], url_str) content['body'], url_str)
response = await group.send_photo(img_file, caption=caption) response = await group.send_photo(img_file, caption=caption)
except: except: