logger = $logger; $this->matrixService = $matrixService; $serverUrl = "http://synapse:8008"; $user = "upschooling"; $this->client = new MatrixClient($serverUrl); //TODO: Replace hard coded pw $this->token = $this->client->login($user, "secret", true); $this->logger->debug("Logged in as " . $user . " on server " . $serverUrl); } /** * listens on all sorts of incoming matrix requests * * @return void * @throws MatrixException * @throws MatrixHttpLibException * @throws MatrixRequestException * @throws RoomNotFoundException */ public function listen() { $syncResponse = $this->client->api()->sync(); $nextBatch = $syncResponse['next_batch']; foreach (array_get($syncResponse, 'rooms.invite', []) as $roomId => $inviteRoom) { $this->handleInvite($roomId); } $rooms = $this->client->getRooms(); foreach ($rooms as $roomId => $room) { $messages = $this->client->api()->getRoomMessages($roomId, $nextBatch, "b", 100)["chunk"]; foreach ($messages as $message) { if ($message["type"] == "m.room.message" && $message["content"]["msgtype"] == "m.text") var_dump($message["content"]["body"]); } } } /** * TBD: Check if the user on the other side is actually allowed to talk to this matrix instance * @return void */ private function auth() { } /** * Given invitation object it'll enter the room * * @param int $roomId the roomID * @return void * @throws MatrixException|RoomNotFoundException */ private function handleInvite(int $roomId) { try { $this->client->joinRoom($roomId); $this->client->api()->sendMessageEvent($roomId, "upschooling.matrix.accepted", []); } catch (\Exception $e) { $this->logger->info("Join for room: " . $roomId . " failed."); } $this->matrixService->setProperty($roomId, "upschooling.s2s", ["approved" => true]); $this->logger->debug("joined " . $roomId); } }