PHP code example of yuri-kovalev / max-bot-symfony-bundle

1. Go to this page and download the library: Download yuri-kovalev/max-bot-symfony-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yuri-kovalev / max-bot-symfony-bundle example snippets




return [
    // ...
    MaxMessenger\Bot\Bundle\MaxBotBundle::class => ['all' => true],
];



declare(strict_types=1);

namespace App\MaxBot\Configurator;

use MaxMessenger\Bot\Bundle\Attribute\AsMaxBotConfigurator;
use MaxMessenger\Bot\Bundle\Contract\MaxBotConfiguratorInterface;
use MaxMessenger\Bot\MaxBot;
use MaxMessenger\Bot\MaxBot\Event\MessageCreatedEvent;

#[AsMaxBotConfigurator]
final readonly class BotEventsConfigurator implements MaxBotConfiguratorInterface
{
    public function configure(MaxBot $maxBot): void
    {
        $maxBot->onMessageCreated(static function (MessageCreatedEvent $event): void {
            if (trim($event->getMessage()?->getText() ?? '') !== 'ping') {
                return;
            }

            $event->reply('pong');
        });
    }
}



declare(strict_types=1);

namespace App\MaxBot\Command;

use MaxMessenger\Bot\Bundle\Attribute\AsMaxBotCommand;
use MaxMessenger\Bot\Bundle\Max\Command\AbstractCommand;
use MaxMessenger\Bot\MaxApiClient;
use MaxMessenger\Bot\Model\Response\MessageCreatedUpdate;
use MaxMessenger\Bot\Model\Response\Update;

#[AsMaxBotCommand(priority: 100)]
final readonly class StartCommand extends AbstractCommand
{
    public function __construct(
        private MaxApiClient $maxApiClient,
    ) {
    }

    protected function getCommandName(): string
    {
        return '/start';
    }

    public function execute(Update $update): void
    {
        if (!$update instanceof MessageCreatedUpdate) {
            return;
        }

        $chatId = $update->getMessage()->getRecipient()->getChatId();
        $this->maxApiClient->sendMessageToChat($chatId, 'Welcome! I am ready to help.');
    }
}