PHP code example of rmscms / telegram

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

    

rmscms / telegram example snippets


return [
    'bots' => [
        'mybot' => [
            'token' => env('TELEGRAM_BOT_TOKEN', ''),
            'channel_id' => env('TELEGRAM_CHANNEL_ID', ''),
            'certificate_path' => env('TELEGRAM_CERTIFICATE_PATH', ''),
            'webhook_url' => env('TELEGRAM_WEBHOOK_URL', ''),
            'allowed_updates' => null,
            'commands' => [],
        ],
    ],
    'default' => 'mybot',
    'async_requests' => env('TELEGRAM_ASYNC_REQUESTS', false),
    'http_client_handler' => null,
    'base_bot_url' => env('TELEGRAM_BASE_BOT_URL', 'https://api.telegram.org/bot'),
    'resolve_command_dependencies' => true,
    'commands' => [
        \Telegram\Bot\Commands\HelpCommand::class,
    ],
    'command_groups' => [],
    'shared_commands' => [],
];

use RMS\Telegram\Telegram;

Route::get('/send-text', function () {
    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->message('Hello, this is a <b>bold</b> message!')
        ->withHtml()
        ->withoutNotification()
        ->send();

    return 'Text message sent!';
});

use RMS\Telegram\Telegram;
use RMS\Telegram\Keyboard;
use RMS\Telegram\Button;

Route::get('/send-photo', function () {
    $keyboard = Keyboard::inline()
        ->row([
            Button::make('Visit Site')->url('https://example.com'),
            Button::make('Action')->callbackData('action_data'),
        ]);

    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->photo('images/photo.jpg')
        ->message('This is a <b>photo</b> caption!')
        ->withHtml()
        ->withKeyboard($keyboard)
        ->withoutNotification()
        ->send();

    return 'Photo sent!';
});

use RMS\Telegram\Telegram;
use RMS\Telegram\Keyboard;
use RMS\Telegram\Button;

Route::get('/send-document', function () {
    $keyboard = Keyboard::inline()
        ->row([
            Button::make('Visit Site')->url('https://example.com'),
        ]);

    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->document('documents/file.pdf')
        ->message('This is a <b>document</b> caption!')
        ->withHtml()
        ->withKeyboard($keyboard)
        ->send();

    return 'Document sent!';
});

use RMS\Telegram\Telegram;
use RMS\Telegram\MediaGroup;
use RMS\Telegram\Keyboard;
use RMS\Telegram\Button;

Route::get('/send-media-group', function () {
    $mediaGroup = MediaGroup::make()
        ->addPhoto('images/photo1.jpg', 'Photo 1 caption')
        ->addVideo('videos/video1.mp4', 'Video caption');

    $keyboard = Keyboard::inline()
        ->row([
            Button::make('Click')->url('https://example.com'),
        ]);

    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->mediaGroup($mediaGroup)
        ->withHtml()
        ->withKeyboard($keyboard)
        ->withoutNotification()
        ->send();

    return 'Media group sent!';
});

use RMS\Telegram\Telegram;
use RMS\Telegram\Keyboard;
use RMS\Telegram\Button;

Route::get('/update-message', function () {
    $keyboard = Keyboard::inline()
        ->row([
            Button::make('Updated')->url('https://example.com'),
        ]);

    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->withMessageId(123)
        ->message('This is an <b>updated</b> message!')
        ->withHtml()
        ->withKeyboard($keyboard)
        ->update();

    return 'Message updated!';
});

Route::get('/update-caption', function () {
    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->withMessageId(123)
        ->photo('images/photo.jpg')
        ->message('This is an <b>updated</b> caption!')
        ->withHtml()
        ->update();

    return 'Caption updated!';
});

use RMS\Telegram\Telegram;

Route::get('/delete-message', function () {
    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->withMessageId(123)
        ->delete();

    return 'Message deleted!';
});

use RMS\Telegram\Telegram;

Route::get('/pin-message', function () {
    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->withMessageId(123)
        ->withoutNotification()
        ->pin();

    return 'Message pinned!';
});

use RMS\Telegram\Telegram;

Route::get('/send-markdown', function () {
    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->message('This is a *bold* message\!')
        ->withMarkdown()
        ->send();

    return 'Markdown message sent!';
});

use RMS\Telegram\Telegram;
use RMS\Telegram\Keyboard;
use RMS\Telegram\Button;

Route::get('/send-with-keyboard', function () {
    $keyboard = Keyboard::inline()
        ->row([
            Button::make('Visit')->url('https://example.com'),
            Button::make('Action')->callbackData('action_data'),
        ]);

    app('rms.telegram')
        ->to(config('telegram.bots.' . config('telegram.default') . '.channel_id'))
        ->message('Choose an option:')
        ->withHtml()
        ->withKeyboard($keyboard)
        ->send();

    return 'Message with keyboard sent!';
});

'public' => [
    'driver' => 'local',
    'root' => storage_path('app/public'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],
bash
   php artisan vendor:publish --tag=telegram-config
   
bash
php artisan telegram:test