mirror of
https://github.com/bsedin/matrix-webhook.git
synced 2026-04-09 19:35:39 +00:00
try another login on connection lost
This commit is contained in:
@@ -16,6 +16,7 @@ from signal import SIGINT, SIGTERM
|
|||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from markdown import markdown
|
from markdown import markdown
|
||||||
from nio import AsyncClient
|
from nio import AsyncClient
|
||||||
|
from nio.exceptions import LocalProtocolError
|
||||||
|
|
||||||
SERVER_ADDRESS = ('', int(os.environ.get('PORT', 4785)))
|
SERVER_ADDRESS = ('', int(os.environ.get('PORT', 4785)))
|
||||||
MATRIX_URL = os.environ.get('MATRIX_URL', 'https://matrix.org')
|
MATRIX_URL = os.environ.get('MATRIX_URL', 'https://matrix.org')
|
||||||
@@ -42,14 +43,21 @@ async def handler(request):
|
|||||||
status, ret = HTTPStatus.UNAUTHORIZED, 'I need the good "key"'
|
status, ret = HTTPStatus.UNAUTHORIZED, 'I need the good "key"'
|
||||||
if data['key'] == API_KEY:
|
if data['key'] == API_KEY:
|
||||||
status, ret = HTTPStatus.OK, 'OK'
|
status, ret = HTTPStatus.OK, 'OK'
|
||||||
await CLIENT.room_send(room_id=str(request.rel_url)[1:],
|
content = {
|
||||||
message_type="m.room.message",
|
"msgtype": "m.text",
|
||||||
content={
|
"body": data['text'],
|
||||||
"msgtype": "m.text",
|
"format": "org.matrix.custom.html",
|
||||||
"body": data['text'],
|
"formatted_body": markdown(data['text'], extensions=['extra']),
|
||||||
"format": "org.matrix.custom.html",
|
}
|
||||||
"formatted_body": markdown(data['text'], extensions=['extra']),
|
try:
|
||||||
})
|
await CLIENT.room_send(room_id=str(request.rel_url)[1:],
|
||||||
|
message_type="m.room.message",
|
||||||
|
content=content)
|
||||||
|
except LocalProtocolError: # Connection lost, try another login
|
||||||
|
await CLIENT.login(MATRIX_PW)
|
||||||
|
await CLIENT.room_send(room_id=str(request.rel_url)[1:],
|
||||||
|
message_type="m.room.message",
|
||||||
|
content=content)
|
||||||
|
|
||||||
return web.Response(text='{"status": %i, "ret": "%s"}' % (status, ret),
|
return web.Response(text='{"status": %i, "ret": "%s"}' % (status, ret),
|
||||||
content_type='application/json',
|
content_type='application/json',
|
||||||
|
|||||||
Reference in New Issue
Block a user