Skip to content

Commit

Permalink
Replace broken legacy Matrix Client SDK
Browse files Browse the repository at this point in the history
Latest synpase update broke the legacy SDK:
matrix-org/matrix-python-sdk#321

Even though the fix is merged, PyPI package is unlikey to update soon as
SDK is no longer maintained, so it is now replaced with matrix-nio.
  • Loading branch information
axkg committed Jul 26, 2021
1 parent a6db55a commit a1731b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
48 changes: 24 additions & 24 deletions run/send_matrix.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python3
import sys
import time
from matrix_client.errors import MatrixHttpLibError
from matrix_client.client import MatrixClient
from matrix_client.api import MatrixHttpApi
import asyncio
import socket

from nio import AsyncClient, LoginResponse

if len(sys.argv) != 6:
sys.stderr.write('usage: %s HOMESERVER_URL USERNAME PASSWORD ROOM_ID MESSAGE_TEXT\n' % sys.argv[0])
Expand All @@ -20,24 +21,23 @@
if username.find(':') > 0:
username = username.split(':')[0]

matrix = None

for retry in range(0, 4):
try:
client = MatrixClient(homeserver)
token = client.login(username, password)
matrix = MatrixHttpApi(homeserver, token)
break
except MatrixHttpLibError:
sys.stderr.write('Connection failed, retrying...\n')
time.sleep(0.25)

if matrix == None:
sys.stderr.write('Could not connect to homeserver. Message not sent.\n')
sys.exit(-2)

try:
client.rooms[room_id].send_text(message)
except:
sys.stderr.write('Failed to send message to room.\n')
sys.exit(-3)
async def main() -> None:
client = AsyncClient(homeserver, username)
response = await client.login(password, device_name=socket.gethostname())

if not isinstance(response, LoginResponse):
sys.stderr.write('Failed to connect to Matrix server.\n')
sys.exit(-1)

await client.room_send(
room_id=room_id,
message_type="m.room.message",
content = {
"msgtype": "m.text",
"body": message
}
)
await client.sync(timeout=30000)
sys.exit(0)

asyncio.get_event_loop().run_until_complete(main())
2 changes: 1 addition & 1 deletion setup/pi/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ function install_sns_packages () {
function install_matrix_packages () {
install_python3_pip
setup_progress "Installing matrix python packages..."
pip3 install matrix_client
pip3 install matrix-nio
}

function check_pushover_configuration () {
Expand Down

0 comments on commit a1731b4

Please sign in to comment.