Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
845af9008c | |||
e5545ac9be | |||
c25e34ebc3 | |||
532c3af254 | |||
9ce2aa35ca | |||
b8e3eb6bf4 | |||
b41badee88 | |||
7da7bc0baa | |||
ac924fa420 | |||
55f0487391 | |||
8ad042dbb9 | |||
fad2be42ef | |||
f44c06daa0 | |||
af7731f00e | |||
e20654668b | |||
30b5c6d3c6 | |||
7b8def4107 | |||
67647026ac |
1
.gitignore
vendored
1
.gitignore
vendored
@ -152,4 +152,5 @@ Temporary Items
|
||||
|
||||
# Config
|
||||
config.json
|
||||
asconfig.yaml
|
||||
database.db
|
||||
|
@ -42,6 +42,8 @@ First, copy config.json.example to config.json. Then fill in the fields:
|
||||
* `hosts.bare`: Just the (sub)domain of the server.
|
||||
* `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).
|
||||
* `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.
|
||||
* `print_url_with_image`: Set to `false` to disable sending the Matrix url of an image to Telegram.
|
||||
|
||||
**Synapse configuration**
|
||||
|
||||
|
@ -12,8 +12,16 @@
|
||||
"bare": "DOMAIN.TLD"
|
||||
},
|
||||
|
||||
"bot_owners": [
|
||||
"@youruser:DOMAIN.TLD",
|
||||
"@youruser:matrix.org",
|
||||
"@youruser:example.com"
|
||||
],
|
||||
|
||||
"user_id_format": "@telegram_{}:DOMAIN.TLD",
|
||||
"db_url": "sqlite:///database.db",
|
||||
|
||||
"print_url_with_image": true,
|
||||
|
||||
"as_port": 5000
|
||||
}
|
||||
|
@ -38,6 +38,11 @@ try:
|
||||
MATRIX_HOST_EXT = CONFIG['hosts']['external']
|
||||
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_MEDIA_PREFIX = MATRIX_HOST + '_matrix/media/r0/'
|
||||
|
||||
@ -45,6 +50,8 @@ try:
|
||||
DATABASE_URL = CONFIG['db_url']
|
||||
|
||||
AS_PORT = CONFIG['as_port'] if 'as_port' in CONFIG else 5000
|
||||
|
||||
PRINT_URL_WITH_IMAGE = CONFIG['print_url_with_image'] if 'print_url_with_image' in CONFIG else True
|
||||
except (OSError, IOError) as exception:
|
||||
print('Error opening config file:')
|
||||
print(exception)
|
||||
@ -183,7 +190,7 @@ async def matrix_transaction(request):
|
||||
for alias in aliases:
|
||||
print(alias)
|
||||
if alias.split('_')[0] != '#telegram' \
|
||||
or alias.split(':')[-1] != MATRIX_HOST_BARE:
|
||||
or not alias.endswith(MATRIX_HOST_BARE):
|
||||
continue
|
||||
|
||||
tg_id = alias.split('_')[1].split(':')[0]
|
||||
@ -205,6 +212,9 @@ async def matrix_transaction(request):
|
||||
|
||||
if event['type'] == 'm.room.message':
|
||||
user_id = event['user_id']
|
||||
if user_id in MATRIX_BOT_OWNERS:
|
||||
displayname = ''
|
||||
else:
|
||||
if matrix_is_telegram(user_id):
|
||||
continue
|
||||
|
||||
@ -229,13 +239,16 @@ async def matrix_transaction(request):
|
||||
continue
|
||||
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
elif content['msgtype'] == 'm.image':
|
||||
try:
|
||||
@ -249,13 +262,17 @@ async def matrix_transaction(request):
|
||||
# Download the file
|
||||
await download_matrix_file(url, content['body'])
|
||||
with open('/tmp/{}'.format(content['body']), 'rb') as img_file:
|
||||
url_str = ''
|
||||
if PRINT_URL_WITH_IMAGE:
|
||||
# Create the URL and shorten it
|
||||
url_str = MATRIX_HOST_EXT + \
|
||||
'_matrix/media/r0/download/{}{}' \
|
||||
.format(url.netloc, quote(url.path))
|
||||
url_str = await shorten_url(url_str)
|
||||
url_str = ' (' + url_str + ')'
|
||||
|
||||
caption = '<{}> {} ({})'.format(displayname,
|
||||
prefix = '' if displayname == '' else '<' + displayname + '> '
|
||||
caption = '{}{}{}'.format(prefix,
|
||||
content['body'], url_str)
|
||||
response = await group.send_photo(img_file, caption=caption)
|
||||
except:
|
||||
@ -449,7 +466,10 @@ async def register_join_matrix(chat, room_id, user_id):
|
||||
|
||||
await matrix_put('client', 'profile/{}/displayname'.format(user_id),
|
||||
user_id, {'displayname': name})
|
||||
await matrix_post('client', 'join/{}'.format(room_id), user_id, {})
|
||||
j = await matrix_post('client', 'join/{}'.format(room_id), user_id, {})
|
||||
if 'errcode' in j and j['errcode'] == 'M_FORBIDDEN':
|
||||
print("Error with <{}> joining room <{}>. This is likely because guests are not allowed to join the room."
|
||||
.format(user_id, room_id))
|
||||
|
||||
async def update_matrix_displayname_avatar(tg_user):
|
||||
name = tg_user['first_name']
|
||||
|
Loading…
Reference in New Issue
Block a user