From e0cf040accfd7dc062e55c011fc2f79caa866fd9 Mon Sep 17 00:00:00 2001 From: Thomas Hackl <hackl@data-quest.de> Date: Fri, 10 Sep 2021 12:26:36 +0200 Subject: [PATCH] first try at posting messages into a room --- controllers/matrix_chat.php | 6 ++++++ models/MatrixRoom.php | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/controllers/matrix_chat.php b/controllers/matrix_chat.php index 62538a3..ffc66df 100644 --- a/controllers/matrix_chat.php +++ b/controllers/matrix_chat.php @@ -51,8 +51,14 @@ class MatrixChatController extends AuthenticatedController $this->hasToCreate = false; $room->requireMembership($this->account->getLinkedAccount()); + + $room->sendMessage( + 'Avengers... assemble.', + '<!-- HTML -->Hulk <strong>SMASH</strong>!' + ); } } + } public function require_room_action() diff --git a/models/MatrixRoom.php b/models/MatrixRoom.php index ac8db99..7caf4fe 100644 --- a/models/MatrixRoom.php +++ b/models/MatrixRoom.php @@ -104,7 +104,7 @@ class MatrixRoom extends SimpleORMap ); $newRoom = MatrixClient::get()->createRoom($sysaccount, Context::getHeaderLine(), false, '', - [],$permission_set); + [], $permission_set); $mapping = new self(); $mapping->range_id = $context->getId(); @@ -158,4 +158,27 @@ class MatrixRoom extends SimpleORMap } } + /** + * Sends a message into the current room, sender is the Stud.IP system account. + * + * @param string $title + * @param string $body + */ + public function sendMessage($title, $body) + { + $content = [ + 'msgtype' => 'm.text', + 'format' => 'm.format.custom_html', + 'body' => strip_tags($title . ":\n" . $body), + 'formatted_body' => htmlReady('<strong>' . $title . '</strong><br>' . $body), + ]; + MatrixClient::get()->sendRoomEvent( + MatrixAccount::requireSystemAccount(), + $this->getLinkedRoom(), + 'm.room.message', + '', + $content + ); + } + } \ No newline at end of file -- GitLab