How to send a message in PHP?

In order to send a message in your PHP code please use the function below.

if (class_exists('Kainex\WiseChatPro\Container', false)) {
	function addWiseChatProMessage($channelName, $userId, $message) {
		$container = Kainex\WiseChatPro\Container::getInstance();
		$messagesService = $container->get(Kainex\WiseChatPro\Services\WiseChatMessagesService::class);
		$usersDao = $container->get(Kainex\WiseChatPro\DAO\User\WiseChatUsersDAO::class);
		$channelsDao = $container->get(Kainex\WiseChatPro\DAO\WiseChatChannelsDAO::class);
		
		$user = $usersDao->getLatestByWordPressId($userId);
		if (!$user) {
			throw new \Exception('User never visited the chat: '.$userId);
		}
		$channel = $channelsDao->getByName($channelName);
		if (!$channel) {
			throw new \Exception('Channel does not exist: '.$channelName);
		}
	
		$messagesService->addMessage($user, $channel, $message, array());
	}
	
	// 'global' - The name of the channel, it must exist before executing this function
	// 1 - WordPress user ID, the user must visit the chat at least once before executing this function
	addWiseChatProMessage('global', 1, 'Test message');
}

Here is a version of the same logic, but for an older version of Wise Chat Pro (lower than 3.6):

if (class_exists('WiseChatContainer', false)) {
	function addWiseChatProMessage($channelName, $userId, $message) {
		$messagesService = WiseChatContainer::get('services/WiseChatMessagesService');
		$usersDao = WiseChatContainer::get('dao/user/WiseChatUsersDAO');
		$channelsDao = WiseChatContainer::get('dao/WiseChatChannelsDAO');
		
		$user = $usersDao->getLatestByWordPressId($userId);
		if (!$user) {
			throw new \Exception('User never visited the chat: '.$userId);
		}
		$channel = $channelsDao->getByName($channelName);
		if (!$channel) {
			throw new \Exception('Channel does not exist: '.$channelName);
		}
	
		$messagesService->addMessage($user, $channel, $message, array());
	}
	
	// 'global' - The name of the channel, it must exist before executing this function
	// 1 - WordPress user ID, the user must visit the chat at least once before executing this function
	addWiseChatProMessage('global', 1, 'Test message');
}