PHP code example of hugphp / telegram

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

    

hugphp / telegram example snippets


return [
    'bot_token' => env('TELEGRAM_BOT_TOKEN', 'your-bot-token'),
    'default_chat_id' => env('TELEGRAM_DEFAULT_CHAT_ID', '68*****41'),
    'api_base_url' => env('TELEGRAM_API_BASE_URL', 'https://api.telegram.org'),
    'webhook_url' => env('TELEGRAM_WEBHOOK_URL', 'https://your-app.com/telegram/webhook'),
    'webhook_max_connections' => env('TELEGRAM_WEBHOOK_MAX_CONNECTIONS', 40),
    'webhook_allowed_updates' => env('TELEGRAM_WEBHOOK_ALLOWED_UPDATES', []),
    'http_timeout' => env('TELEGRAM_HTTP_TIMEOUT', 10),
    'http_retries' => env('TELEGRAM_HTTP_RETRIES', 3),
    'http_retry_delay' => env('TELEGRAM_HTTP_RETRY_DELAY', 500),
];

use HugPHP\Telegram\Facades\Telegram;

$chatId = config('telegram.default_chat_id', '68*****41');
$message = '<b>Hello</b> from <i>HugPHP</i> <a href="https://hugphp.com">Telegram</a>!';
$response = Telegram::sendMessage($chatId, $message, [
    'parse_mode' => 'HTML',
    'reply_markup' => json_encode([
        'inline_keyboard' => [
            [['text' => 'Visit Website', 'url' => 'https://github.com/hugphp/telegram']],
            [['text' => 'Open MinApp', 'web_app' => ['url' => 'https://github.com/hugphp/telegram']]],
        ],
    ]),
]);

$response = Telegram::sendPhoto($chatId, 'https://tailwindcss.com/plus-assets/img/component-images/dark-project-app-screenshot.png', [
    'caption' => 'Test photo',
]);

$response = Telegram::sendVideo($chatId, 'https://cdn.pixabay.com/video/2025/03/28/268290_large.mp4', [
    'caption' => 'Test video',
]);

$response = Telegram::sendDocument($chatId, 'https://www.fsa.usda.gov/Internet/FSA_File/tech_assist.pdf', [
    'caption' => 'Test document',
]);

$response = Telegram::sendLocation($chatId, 51.5074, -0.1278, [
    'live_period' => 3600,
]);

$response = Telegram::sendContact($chatId, '+1234567890', 'John Doe', [
    'last_name' => 'Smith',
]);

$webhookUrl = config('telegram.webhook_url', '');
$response = Telegram::setWebhook($webhookUrl, [
    'max_connections' => config('telegram.webhook_max_connections', 40),
    'allowed_updates' => config('telegram.webhook_allowed_updates', []),
]);

$response = Telegram::getWebhookInfo();

$response = Telegram::deleteWebhook();

$response = Telegram::getUpdates([
    'limit' => 10,
]);



use HugPHP\Telegram\Facades\Telegram;
use Illuminate\Support\Facades\Route;

// -----

Route::get('/validate-all-functionalities', function (): array {
    $chatId = config('telegram.default_chat_id', '68*****41');
    $webhookUrl = config('telegram.webhook_url', '');
    $results = [];

    // Test sendMessage
    try {
        $messageInHtml = '<b>Hello</b> from <i>HugPHP</i> <a href="https://hugphp.com">Telegram</a>!';
        $results['sendMessage'] = [
            'status' => 'success',
            'response' => Telegram::sendMessage($chatId, $messageInHtml, [
                'parse_mode' => 'HTML',
                'reply_markup' => json_encode([
                    'inline_keyboard' => [
                        [['text' => 'Visit Website', 'url' => 'https://github.com/hugphp/telegram']],
                        [['text' => 'Open MinApp', 'web_app' => ['url' => 'https://github.com/hugphp/telegram']]],
                    ],
                ]),
            ]),
        ];
    } catch (\RuntimeException $e) {
        $results['sendMessage'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    // Test sendPhoto
    try {
        $results['sendPhoto'] = [
            'status' => 'success',
            'response' => Telegram::sendPhoto($chatId, 'https://tailwindcss.com/plus-assets/img/component-images/dark-project-app-screenshot.png', [
                'caption' => 'Test photo',
            ]),
        ];
    } catch (\RuntimeException $e) {
        $results['sendPhoto'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    // Test sendVideo
    try {
        $results['sendVideo'] = [
            'status' => 'success',
            'response' => Telegram::sendVideo($chatId, 'https://cdn.pixabay.com/video/2025/03/28/268290_large.mp4', [
                'caption' => 'Test video',
            ]),
        ];
    } catch (\RuntimeException $e) {
        $results['sendVideo'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    // Test sendDocument
    try {
        $results['sendDocument'] = [
            'status' => 'success',
            'response' => Telegram::sendDocument($chatId, 'https://www.fsa.usda.gov/Internet/FSA_File/tech_assist.pdf', [
                'caption' => 'Test document',
            ]),
        ];
    } catch (\RuntimeException $e) {
        $results['sendDocument'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    // Test sendLocation
    try {
        $results['sendLocation'] = [
            'status' => 'success',
            'response' => Telegram::sendLocation($chatId, 51.5074, -0.1278, [
                'live_period' => 3600,
            ]),
        ];
    } catch (\RuntimeException $e) {
        $results['sendLocation'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    // Test sendContact
    try {
        $results['sendContact'] = [
            'status' => 'success',
            'response' => Telegram::sendContact($chatId, '+1234567890', 'John Doe', [
                'last_name' => 'Smith',
            ]),
        ];
    } catch (\RuntimeException $e) {
        $results['sendContact'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    // Test setWebhook
    try {
        $results['setWebhook'] = [
            'status' => 'success',
            'response' => Telegram::setWebhook($webhookUrl, [
                'max_connections' => config('telegram.webhook_max_connections', 40),
                'allowed_updates' => config('telegram.webhook_allowed_updates', []),
            ]),
        ];
    } catch (\RuntimeException $e) {
        $results['setWebhook'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    // Test getWebhookInfo
    try {
        $results['getWebhookInfo'] = [
            'status' => 'success',
            'response' => Telegram::getWebhookInfo(),
        ];
    } catch (\RuntimeException $e) {
        $results['getWebhookInfo'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    // Test deleteWebhook
    try {
        $results['deleteWebhook'] = [
            'status' => 'success',
            'response' => Telegram::deleteWebhook(),
        ];
    } catch (\RuntimeException $e) {
        $results['deleteWebhook'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    // Test getUpdates
    try {
        $results['getUpdates'] = [
            'status' => 'success',
            'response' => Telegram::getUpdates([
                'limit' => 10,
            ]),
        ];
    } catch (\RuntimeException $e) {
        $results['getUpdates'] = ['status' => 'error', 'error' => $e->getMessage()];
    }

    return $results;
});
bash
php artisan vendor:publish --provider="HugPHP\Telegram\TelegramServiceProvider"