Make bot auto-join rooms
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
---
|
||||||
|
matrix_homeserver: ""
|
||||||
|
matrix_access_token": ""
|
||||||
|
matrix_user_id: ""
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
flask
|
flask
|
||||||
websocket-client
|
websocket-client
|
||||||
pyyaml
|
pyyaml
|
||||||
matrix-client
|
matrix-nio
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ import random
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from matrix_client.client import MatrixClient
|
from nio import AsyncClient, MatrixRoom, RoomMessageText
|
||||||
|
from nio.exceptions import OlmUnverifiedDeviceError
|
||||||
|
import asyncio
|
||||||
|
|
||||||
__version__ = "3.0.0"
|
__version__ = "3.0.0"
|
||||||
versionstring='Taurix TellMe server v' + __version__
|
versionstring='Taurix TellMe server v' + __version__
|
||||||
@@ -71,6 +73,48 @@ def send_smp_message(target, message):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def matrix_login_and_send(homeserver, access_token, user_id, target, message):
|
||||||
|
client = AsyncClient(homeserver, user_id)
|
||||||
|
client.access_token = access_token
|
||||||
|
|
||||||
|
invited_rooms = []
|
||||||
|
|
||||||
|
async def auto_join_callback(room: MatrixRoom, event: RoomMessageText, client: AsyncClient):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def invited_callback(room: MatrixRoom, event: RoomMessageText, client: AsyncClient):
|
||||||
|
invited_rooms.append(room.room_id)
|
||||||
|
|
||||||
|
client.add_event_callback(auto_join_callback, RoomMessageText)
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await client.sync(full_state=True)
|
||||||
|
for room_id, invite_state in client.invited_rooms.items():
|
||||||
|
await client.join(room_id)
|
||||||
|
invited_rooms.append(room_id)
|
||||||
|
|
||||||
|
if target not in client.rooms and target not in invited_rooms:
|
||||||
|
await client.join(target)
|
||||||
|
|
||||||
|
room = client.rooms.get(target)
|
||||||
|
if room:
|
||||||
|
await client.room_send(
|
||||||
|
room_id=target,
|
||||||
|
message_type="m.room.message",
|
||||||
|
content={"msgtype": "m.text", "body": message}
|
||||||
|
)
|
||||||
|
log.info("Sent message to Matrix room %s" % (target))
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
log.error("Could not join Matrix room %s" % (target))
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
log.error("Failed to send Matrix message: %s" % (e))
|
||||||
|
return False
|
||||||
|
finally:
|
||||||
|
await client.close()
|
||||||
|
|
||||||
|
|
||||||
def send_matrix_message(target, message):
|
def send_matrix_message(target, message):
|
||||||
try:
|
try:
|
||||||
homeserver = config.get('matrix_homeserver', 'https://matrix.org')
|
homeserver = config.get('matrix_homeserver', 'https://matrix.org')
|
||||||
@@ -81,15 +125,9 @@ def send_matrix_message(target, message):
|
|||||||
log.error("Matrix credentials not configured")
|
log.error("Matrix credentials not configured")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
client = MatrixClient(homeserver)
|
return asyncio.get_event_loop().run_until_complete(
|
||||||
client.login(token=access_token, user_id=user_id)
|
matrix_login_and_send(homeserver, access_token, user_id, target, message)
|
||||||
|
)
|
||||||
room = client.join_room(target)
|
|
||||||
room.send_text(message)
|
|
||||||
|
|
||||||
client.logout()
|
|
||||||
log.info("Sent message to Matrix room %s" % (target))
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error("Failed to send message to Matrix: %s" % (e))
|
log.error("Failed to send message to Matrix: %s" % (e))
|
||||||
return False
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user