WT Max integration library for Joomla

  • Category: Libraries
  • Version: 0.1.1
  • Date:
Downloads 629 Hits 396 CTR 159% Pack Free

MAX API Joomla library with a system plugin for configuration and connection diagnostics.

WT Max integration library for Joomla

Description

Joomla package that installs:

  • the WebTolk/Wtmax library
  • the System - WT Max system plugin

The package wraps the main webtolk/max SDK and provides a ready entry point \Webtolk\Wtmax\Wtmax::getInstance(). The bot token and service settings are stored in the system plugin.

Main SDK repository: https://github.com/WebTolk/Max-platform-PHP-SDK

Package contents

  • Joomla library Webtolk/Wtmax
  • system plugin for storing the token
  • API connection status field
  • separate file logging option

How it works

After installation:

  1. enable the System - WT Max plugin
  2. enter the MAX bot token
  3. enable separate file logging if needed
  4. in Joomla code, get the ready SDK via Wtmax::getInstance()

Internally, the library creates:

  • Joomla\Http\HttpFactory
  • Laminas\Diactoros\RequestFactory
  • Laminas\Diactoros\StreamFactory
  • a PSR-3 logger from Joomla core

Quick start

Get a ready SDK instance

<?php

declare(strict_types=1);

use Webtolk\Wtmax\Wtmax;

defined('_JEXEC') or die;

$max = Wtmax::getInstance();

$bot = $max->bots()->me();

echo $bot->getId();
echo $bot->getUsername();

Send a message to a chat

The example is based on the upstream messages()->sendToChat() flow.

<?php

declare(strict_types=1);

use Webtolk\Max\Payload\NewMessageBody;
use Webtolk\Wtmax\Wtmax;

defined('_JEXEC') or die;

$chatId = 123456;

$message = Wtmax::getInstance()->messages()->sendToChat(
	$chatId,
	NewMessageBody::text('Hello from Joomla WT Max library')
);

echo $message->getBody()?->getText() ?? '';

Send an image

The example is based on the upstream upload flow: uploads()->upload() + toAttachment() + messages()->sendToChat().

<?php

declare(strict_types=1);

use Webtolk\Max\Payload\NewMessageBody;
use Webtolk\Max\Payload\UploadType;
use Webtolk\Wtmax\Wtmax;
use RuntimeException;

defined('_JEXEC') or die;

$chatId = 123456;
$imagePath = JPATH_ROOT . '/images/sample.jpg';
$binaryImage = file_get_contents($imagePath);

if ($binaryImage === false)
{
	throw new RuntimeException('Image file was not read: ' . $imagePath);
}

$max = Wtmax::getInstance();

$imageAttachment = $max->uploads()
	->upload(UploadType::IMAGE, $binaryImage, 'image/jpeg')
	->toAttachment();

$message = $max->messages()->sendToChat(
	$chatId,
	NewMessageBody::text('Sending an image from Joomla')
		->withAttachments([$imageAttachment])
);

echo $message->getBody()?->getMessageId() ?? '';

Handle a callback and answer it

The example is based on the upstream messages()->answerCallback() flow.

<?php

declare(strict_types=1);

use Webtolk\Max\Payload\CallbackAnswerPayload;
use Webtolk\Wtmax\Wtmax;

defined('_JEXEC') or die;

$payload = file_get_contents('php://input');

if ($payload !== false && $payload !== '')
{
	$update = json_decode($payload, true, 512, JSON_THROW_ON_ERROR);
	$callbackId = $update['callback']['callback_id'] ?? null;

	if ($callbackId !== null)
	{
		Wtmax::getInstance()->messages()->answerCallback(
			(string) $callbackId,
			(new CallbackAnswerPayload())->withNotification('Button processed')
		);
	}
}

If you need a manual Max instance

Wtmax::getInstance() is convenient for typical Joomla usage, but you can also assemble the SDK manually if needed.

<?php

declare(strict_types=1);

require_once JPATH_LIBRARIES . '/Webtolk/Wtmax/src/libraries/vendor/autoload.php';

use Joomla\Http\HttpFactory;
use Laminas\Diactoros\RequestFactory;
use Laminas\Diactoros\StreamFactory;
use Webtolk\Max\Config\MaxConfig;
use Webtolk\Max\Max;
use Psr\Log\NullLogger;

defined('_JEXEC') or die;

$token = 'YOUR_BOT_TOKEN';

$max = new Max(
	new MaxConfig($token),
	new NullLogger()
);

$max->setTransport(
	(new HttpFactory())->getHttp([], ['curl', 'stream']),
	new RequestFactory(),
	new StreamFactory(),
);

Where the token is stored

The token is stored in the parameters of the System - WT Max system plugin.

Main parameters:

  • MAX bot token
  • Log to a separate file
  • Custom log file

Logging

If logging is enabled, the library writes to a separate file:

/logs/wtmax.max-api.log

 

Joomla

Extension type:
Package
Package composition:
Library, Plugin
Joomla version:
6.1.0, 6.1.1

What's new

Addition

Sending messages to Max via the onWtmaxSendMessage trigger

Added the onWtmaxSendMessage event to send messages from third-party Joomla extensions to the default chat.
Addition

The default chat for sending messages

Added the default chat_id setting for outgoing messages (MAX chat selection via Joomla ModalSelect). The plugin shows information about this chat_id.
Addition

Log of outgoing messages

The outgoing message log has been added to the plugin table #__plg_system_wtmax_messages.
Addition

Central processing of incoming webhooks

Added central processing of incoming webhooks from MAX bots. The central processing of webhooks triggers the onWtmaxIncomingWebhook event for system plugins and plugins of the wtmax group
Change

Updating the PHP SDK Max

The PHP SDK Max has been updated to version 0.1.1.

WebTolk Joomla Extensions

106 Extensions
12 Categories
542 Versions released
747332 Downloads