try another login on connection lost
This commit is contained in:
parent
887dc95e3d
commit
6c138a65b4
1 changed files with 16 additions and 8 deletions
|
@ -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",
|
|
||||||
content={
|
|
||||||
"msgtype": "m.text",
|
"msgtype": "m.text",
|
||||||
"body": data['text'],
|
"body": data['text'],
|
||||||
"format": "org.matrix.custom.html",
|
"format": "org.matrix.custom.html",
|
||||||
"formatted_body": markdown(data['text'], extensions=['extra']),
|
"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',
|
||||||
|
|
Loading…
Reference in a new issue