PHP code example of madmagestelegram / laravel

1. Go to this page and download the library: Download madmagestelegram/laravel 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/ */

    

madmagestelegram / laravel example snippets


 
declare(strict_types=1);

namespace App\Providers;

class TelegramServiceProvider extends \MadmagesTelegram\Laravel\HandlerServiceProvider
{

    /** @var array Handlers for telegram commands */
    protected array $telegramCommands = [];

    // Other handlers
    protected array $messageHandlers = [
        // This handler is for telegram-commands,
        // i.e. it makes $this->telegramCommands working
        \MadmagesTelegram\Laravel\Handler\Command\CommandHandler::class
    ];
    protected array $editMessageHandlers = [];
    protected array $channelPostHandlers = [];
    protected array $editedChannelPostHandlers = [];
    protected array $inlineQueryHandlers = [];
    protected array $chosenInlineResultHandlers = [];
    protected array $callbackQueryHandlers = [];
    protected array $preCheckoutQueryHandlers = [];
    protected array $shippingQueryHandlers = [];
    protected array $pollAnswerHandlers = [];
    protected array $pollHandlers = [];
}


 declare(strict_types=1);

namespace App\Module\Telegram\Command;

use MadmagesTelegram\Laravel\Handler\Command\AbstractCommand;
use MadmagesTelegram\Laravel\WebhookClient;
use MadmagesTelegram\Types\Type\Message;
use Illuminate\Http\JsonResponse;

class Start extends AbstractCommand
{

    private WebhookClient $client;

    public function __construct(WebhookClient $client)
    {
        // It possible to response, during webhook request 
        // https://core.telegram.org/bots/api#making-requests-when-getting-updates
        $this->client = $client;
    }
    
     public function getName(): string
    {
        return 'start';
    }

    public function execute(Message $message, bool $isPrivate): ?JsonResponse
    {
        return $this->client->sendMessage($message->getChat()->getId(), 'Hello there !');
    }
}

 
declare(strict_types=1);

namespace App\Providers;

class TelegramServiceProvider extends \MadmagesTelegram\Laravel\HandlerServiceProvider
{
    protected array $telegramCommands = [
        \App\Module\Telegram\Command\Start::class
    ];
}