public static function send_messages_to_conversation(int $conversationid, array $messages = []) {
        global $CFG, $USER;

        // Check if messaging is enabled.
        if (empty($CFG->messaging)) {
            throw new moodle_exception('disabled', 'message');
        }

        // Ensure the current user is allowed to run this function.
        $context = context_system::instance();
        self::validate_context($context);

        $params = self::validate_parameters(self::send_messages_to_conversation_parameters(), [
            'conversationid' => $conversationid,
            'messages' => $messages
        ]);

        $messages = [];
        foreach ($params['messages'] as $message) {
            $createdmessage = \core_message\api::send_message_to_conversation($USER->id, $params['conversationid'], $message['text'],
                $message['textformat']);
            $createdmessage->text = message_format_message_text((object) [
                'smallmessage' => $createdmessage->text,
                'fullmessageformat' => external_validate_format($message['textformat']),
                'fullmessagetrust' => $createdmessage->fullmessagetrust
            ]);
            $messages[] = $createdmessage;
        }

        return $messages;
    }