sakoora0x/laravel-telegram-bot

This is my package laravel-telegram-bot

Fund package maintenance!
Mollsoft

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 1

pkg:composer/sakoora0x/laravel-telegram-bot

dev-main 2025-10-24 06:02 UTC

This package is not auto-updated.

Last update: 2025-10-25 03:27:05 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package for Laravel 11+ allows you to easily create interactive Telegram bots, using Laravel routing, and using Blade templates to conduct a dialogue with the user.

Installation

You can install the package via composer:

composer require sakoora0x/laravel-telegram-bot
php artisan telegram:install

You can publish and run the migrations with:

php artisan vendor:publish --tag="telegram-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="telegram-config"

Optionally, you can publish the views using:

php artisan vendor:publish --tag="telegram-views"

Configuration for Laravel Sail

Optionally, if you use Sail for local development, you need to add PHP parameter PHP_CLI_SERVER_WORKERS="10" in file supervisord.conf:

[program:php]
command=%(ENV_SUPERVISOR_PHP_COMMAND)s
user=%(ENV_SUPERVISOR_PHP_USER)s
environment=LARAVEL_SAIL="1",PHP_CLI_SERVER_WORKERS="10"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

Authentication Setup

You can use Laravel Auth, edit file config/auth.php and edit section guards:

'guards' => [
    'web' => [...],
    'telegram' => [
        'driver' => 'telegram',
        'provider' => 'users',
    ]
],

After this you can use middleware auth:telegram in your routes.

Scheduled Tasks

If you want to work with automatic dialog truncation, you must run command php artisan telegram:truncate every minute using Schedule.

Live Pages Setup

You can configure "live pages" (auto-refreshing pages). In the bootstrap/app.php file, add an alias to the withMiddleware section:

->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'telegram.live' => \sakoora0x\Telegram\Middleware\LiveMiddleware::class,
    ]);
})

Then connect the middleware to the desired route:

Route::telegram('/', [\App\Telegram\Controllers\MyController::class, 'index'])
    ->middleware(['telegram.live:30']);

The argument is the frequency in seconds for how often to update the page.

And add to the routes/console.php file:

Schedule::command('telegram:live')
    ->runInBackground()
    ->everyMinute();

Usage

Create New Telegram Bot

php artisan telegram:new-bot

Set Webhook for Bot

php artisan telegram:set-webhook

Unset Webhook for Bot

php artisan telegram:unset-webhook

Manual Polling (on localhost) for Bot

php artisan telegram:polling [BOT_ID]

Features

Inline Keyboard

If you want to create a button to change the current URI query params, use this template:

<inline-keyboard>
    <row>
        <column query-param="value">Change query param</column>
    </row>
</inline-keyboard>

If you want to send POST data, you must use this template:

<inline-keyboard>
    <row>
        <column data-field="value">Send field value</column>
    </row>
</inline-keyboard>

If the POST data is long, you can encrypt it using this template:

<inline-keyboard>
    <row>
        <column data-field="long value" encode="true">Encoded send data</column>
    </row>
</inline-keyboard>

If you want to make a redirect to another page from a button, use this template:

<inline-keyboard>
    <row>
        <column data-redirect="/">Redirect to /</column>
    </row>
</inline-keyboard>

Edit Form

Create a form class for data editing:

class MyForm extends \sakoora0x\Telegram\EditForm\BaseForm
{
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'min:5', 'max:255'],
            'phone' => ['required', 'string', 'min:10', 'max:15'],
        ];
    }

    public function titles(): array
    {
        return [
            'name' => 'Your name',
            'phone' => 'Your phone number'
        ];
    }
}

Use the form in your controller:

class MyController
{
    public function edit(MyForm $form): mixed
    {
        $form->setDefault([
            'name' => 'Default name',
            'phone' => '1234567890',
        ]);

        if ($form->validate()) {
            // $form->get();
        }

        return view('...', compact('form'));
    }

    public function create(MyForm $form): mixed
    {
        if ($form->isCreate()->validate()) {
            // $form->get();
        }

        return view('...', compact('form'));
    }
}

Display the form in your Blade template:

<message>
    <x-telegram-edit-form :form="$form">
        <x-slot:name>
            <line>Please, enter your First Name:</line>
        </x-slot:name>
    </x-telegram-edit-form>
</message>

Testing

The package includes comprehensive tests covering all core functionality.

composer test

Test Results:

  • ✅ 88 passing tests
  • ⏭️ 1 skipped
  • 138 assertions
  • Duration: ~2.4 seconds

See TESTING.md for detailed testing documentation.

Future Ideas

  1. Add query-history=false parameter to Inline Button so that the current URL is not saved in referer, preventing form reset on back navigation.
  2. Enable users to upload photos/videos/documents and parse captions in messages.
  3. Add phone number sharing button in Reply Button and receive results in TelegramRequest.
  4. Read results from forwarded contacts in TelegramRequest.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.