PHP code example of tarik02 / laravel-telegram

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

    

tarik02 / laravel-telegram example snippets




namespace App\Telegram;

use Illuminate\Contracts\Container\Container;
use Tarik02\Telegram\Methods\SendMessage;

use Tarik02\LaravelTelegram\{
    Contracts\Dispatcher as TelegramDispatcher,
    Request,
    Response
};

/**
 * Class Dispatcher
 * @package App\Telegram
 */
class Dispatcher implements TelegramDispatcher
{
    /**
     * @var Container
     */
    protected Container $container;

    /**
     * @param Container $container
     * @return void
     */
    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    /**
     * @param Request $request
     * @return Response|null
     */
    public function dispatch(Request $request): ?Response
    {
        $update = $request->update();

        if (null !== $message = $update->message()) {
            $text = \sprintf(
                "Hello, %s. I received your message with text:\n%s",
                $message->from()->firstName(),
                $message->text() ?? ''
            );

            return Response::reply(
                SendMessage::make()
                    ->withChatId($message->chat()->id())
                    ->withText($text)
            );
        }

        return null;
    }
}



namespace App\Telegram;

use Tarik02\LaravelTelegram\Kernel as TelegramKernel;

/**
 * Class Kernel
 * @package App\Telegram
 */
class Kernel extends TelegramKernel
{
    /**
     * @var string[]
     */
    protected array $middleware = [
    ];
}



return [
    'bots' => [
        'main' => [
            'token' => env('TELEGRAM_BOT_TOKEN'),
            'username' => env('TELEGRAM_BOT_USERNAME'),
            'dispatcher' => \App\Telegram\Dispatcher::class,

            'webhook' => [
                'path' => '/' . env('TELEGRAM_BOT_TOKEN'),
            ],
        ],
    ],
];


// ...

+ use Telegram;

// ...

  $this->routes(function () {
+             Telegram::webhookRoutes();



use Tarik02\LaravelTelegram\Http\Controllers\WebhookController;

Route::post('/some-secret-path-for-example-containing-bot-token')
    ->name('telegram.webhook.main')
    ->uses([WebhookController::class, 'index']);

  # Get information about webhook:
$ php artisan telegram:webhook:get-info

# Delete webhook:
$ php artisan telegram:webhook:delete



return [
    'bots' => [
        'bot1' => [
            'token' => env('TELEGRAM_BOT1_TOKEN'),
            'username' => env('TELEGRAM_BOT1_USERNAME'),
            'dispatcher' => \App\Telegram\Bot1Dispatcher::class,

            'webhook' => [
                'path' => '/' . env('TELEGRAM_BOT1_TOKEN'),
            ],
        ],
        'bot2' => [
            'token' => env('TELEGRAM_BOT2_TOKEN'),
            'username' => env('TELEGRAM_BOT2_USERNAME'),
            'dispatcher' => \App\Telegram\Bot2Dispatcher::class,

            'webhook' => [
                'path' => '/' . env('TELEGRAM_BOT2_TOKEN'),
            ],
        ],
    ],
];
bash
$ php artisan telegram:updates:get
bash
$ php artisan telegram:webhook:set