PHPackages                             hugphp/telegram - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. hugphp/telegram

ActiveLibrary[API Development](/categories/api)

hugphp/telegram
===============

A Laravel package for interacting with the Telegram Bot API

v0.1.0(1y ago)03MITPHPPHP ^8.3.0CI passing

Since Apr 19Pushed 1y agoCompare

[ Source](https://github.com/hugphp/telegram)[ Packagist](https://packagist.org/packages/hugphp/telegram)[ Fund](https://www.paypal.com/paypalme/enunomaduro)[ GitHub Sponsors](https://github.com/nunomaduro)[ RSS](/packages/hugphp-telegram/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (9)Versions (2)Used By (0)

 [![Hugphp Telegram](https://raw.githubusercontent.com/hugphp/telegram/master/docs/example.png)](https://raw.githubusercontent.com/hugphp/telegram/master/docs/example.png)

 [![GitHub Workflow Status (master)](https://github.com/hugphp/telegram/actions/workflows/tests.yml/badge.svg)](https://github.com/hugphp/telegram/actions) [![Total Downloads](https://camo.githubusercontent.com/d974840535b415537ca8b9fb51aecbb3254d7f8352a8ac764724beb259d082c3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6875677068702f74656c656772616d)](https://packagist.org/packages/hugphp/telegram) [![Latest Version](https://camo.githubusercontent.com/4f7c48fe31d28118e311c8075f55380815e2e646e173978367e9ddea6156de83/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6875677068702f74656c656772616d)](https://packagist.org/packages/hugphp/telegram) [![License](https://camo.githubusercontent.com/6c5c5d6779c5d52f4bb959ad5db58109a65e3fae20551af5103c814dfe0cb1df/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6875677068702f74656c656772616d)](https://packagist.org/packages/hugphp/telegram)

---

HugPHP Telegram
===============

[](#hugphp-telegram)

A robust and type-safe Laravel package for interacting with the [Telegram Bot API](https://core.telegram.org/bots/api). This package provides a simple, performant, and flexible interface for sending messages, media, locations, contacts, and managing webhooks in your Laravel applications.

Features
--------

[](#features)

- **Type-Safe**: Built with strict typing and validated by PHPStan for 100% type safety.
- **Easy Integration**: Seamlessly integrates with Laravel’s HTTP client and configuration system.
- **Comprehensive API Support**: Send messages, photos, videos, documents, locations, contacts, and manage webhooks.
- **Retry Logic**: Configurable retries and timeouts for reliable API communication.
- **Tested**: &gt;83% test coverage with Pest, ensuring reliability and stability.

### Feature Roadmap

[](#feature-roadmap)

FeatureMethodStatusSend Message`sendMessage`✅ CompletedSend Photo`sendPhoto`✅ CompletedSend Video`sendVideo`✅ CompletedSend Document`sendDocument`✅ CompletedSend Location`sendLocation`✅ CompletedSend Contact`sendContact`✅ CompletedSet Webhook`setWebhook`✅ CompletedGet Webhook Info`getWebhookInfo`✅ CompletedDelete Webhook`deleteWebhook`✅ CompletedGet Updates`getUpdates`✅ CompletedHandle Callback Query`handleCallbackQuery`☐ PendingFile Uploads`fileUploads`☐ PendingSend Notification`sendNotification`☐ PendingInstallation
------------

[](#installation)

Install the package via Composer:

```
composer require hugphp/telegram
```

Publish the configuration file:

```
php artisan vendor:publish --provider="HugPHP\Telegram\TelegramServiceProvider"
```

This creates a `config/telegram.php` file for configuring your Telegram bot settings.

Configuration
-------------

[](#configuration)

Edit `config/telegram.php` to set your bot token and other settings:

```
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),
];
```

Add the following to your `.env` file:

```
TELEGRAM_BOT_TOKEN=your-bot-token-from-botfather
TELEGRAM_DEFAULT_CHAT_ID=68*****41
TELEGRAM_WEBHOOK_URL=https://your-app.com/telegram/webhook
```

Usage
-----

[](#usage)

The package provides a `Telegram` facade for easy interaction with the Telegram Bot API. Below are examples for each supported method, based on tested functionality.

### Sending a Message

[](#sending-a-message)

Send a formatted HTML message with an inline keyboard:

```
use HugPHP\Telegram\Facades\Telegram;

$chatId = config('telegram.default_chat_id', '68*****41');
$message = 'Hello from HugPHP Telegram!';
$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']]],
        ],
    ]),
]);
```

**Example Response**:

```
{
  "ok": true,
  "result": {
    "message_id": 34,
    "from": {
      "id": 78*****897,
      "is_bot": true,
      "first_name": "hugphp telegram",
      "username": "HugPHPTelegramBot"
    },
    "chat": {
      "id": 68*****41,
      "first_name": "Mike",
      "username": "micheal_ataklt",
      "type": "private"
    },
    "date": 1745171806,
    "text": "Hello from HugPHP Telegram!",
    "entities": [
      {"offset": 0, "length": 5, "type": "bold"},
      {"offset": 11, "length": 6, "type": "italic"},
      {"offset": 18, "length": 8, "type": "text_link", "url": "https://hugphp.com/"}
    ],
    "reply_markup": {
      "inline_keyboard": [
        [{"text": "Visit Website", "url": "https://github.com/hugphp/telegram"}],
        [{"text": "Open MinApp", "web_app": {"url": "https://github.com/hugphp/telegram"}}]
      ]
    }
  }
}
```

### Sending a Photo

[](#sending-a-photo)

Send a photo with a caption:

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

**Example Response**:

```
{
  "ok": true,
  "result": {
    "message_id": 35,
    "from": {
      "id": 78*****897,
      "is_bot": true,
      "first_name": "hugphp telegram",
      "username": "HugPHPTelegramBot"
    },
    "chat": {
      "id": 68*****41,
      "first_name": "Mike",
      "username": "micheal_ataklt",
      "type": "private"
    },
    "date": 1745171806,
    "photo": [
      {
        "file_id": "AgACAgQAAxkDAAMPaAPvxocvYpM5Bxm1xZJVv-R3-XYAApq3MRux3V1S-1eP43y186gBAAMCAANzAAM2BA",
        "file_unique_id": "AQADmrcxG7HdXVJ4",
        "file_size": 843,
        "width": 90,
        "height": 53
      },
      ...
    ],
    "caption": "Test photo"
  }
}
```

### Sending a Video

[](#sending-a-video)

Send a video with a caption:

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

**Example Response**:

```
{
  "ok": true,
  "result": {
    "message_id": 36,
    "from": {
      "id": 78*****897,
      "is_bot": true,
      "first_name": "hugphp telegram",
      "username": "HugPHPTelegramBot"
    },
    "chat": {
      "id": 68*****41,
      "first_name": "Mike",
      "username": "micheal_ataklt",
      "type": "private"
    },
    "date": 1745171807,
    "video": {
      "duration": 21,
      "width": 1920,
      "height": 1080,
      "file_name": "268290_large.mp4",
      "mime_type": "video/mp4",
      "file_id": "BAACAgQAAxkDAAMUaAUz0xMkTfAt6U5fpThEhdoNLWYAAqYHAAKpO_xTHX5XZpe5yrI2BA",
      "file_unique_id": "AgADpgcAAqk7_FM",
      "file_size": 12950075
    },
    "caption": "Test video"
  }
}
```

### Sending a Document

[](#sending-a-document)

Send a document with a caption:

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

**Example Response**:

```
{
  "ok": true,
  "result": {
    "message_id": 37,
    "from": {
      "id": 78*****897,
      "is_bot": true,
      "first_name": "hugphp telegram",
      "username": "HugPHPTelegramBot"
    },
    "chat": {
      "id": 68*****41,
      "first_name": "Mike",
      "username": "micheal_ataklt",
      "type": "private"
    },
    "date": 1745171808,
    "document": {
      "file_name": "tech_assist.pdf",
      "mime_type": "application/pdf",
      "file_id": "BQACAgQAAxkDAAMQaAPwZ9Q1XR_bK67ZeHQTnSdtDDMAAuEHAAIU78RTo-YwBHZMf-s2BA",
      "file_unique_id": "AgAD4QcAAhTvxFM",
      "file_size": 60232
    },
    "caption": "Test document"
  }
}
```

### Sending a Location

[](#sending-a-location)

Send a live location:

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

**Example Response**:

```
{
  "ok": true,
  "result": {
    "message_id": 38,
    "from": {
      "id": 78*****897,
      "is_bot": true,
      "first_name": "hugphp telegram",
      "username": "HugPHPTelegramBot"
    },
    "chat": {
      "id": 68*****41,
      "first_name": "Mike",
      "username": "micheal_ataklt",
      "type": "private"
    },
    "date": 1745171808,
    "location": {
      "latitude": 51.507394,
      "longitude": -0.127813,
      "live_period": 3600
    }
  }
}
```

### Sending a Contact

[](#sending-a-contact)

Send a contact with first and last name:

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

**Example Response**:

```
{
  "ok": true,
  "result": {
    "message_id": 39,
    "from": {
      "id": 78*****897,
      "is_bot": true,
      "first_name": "hugphp telegram",
      "username": "HugPHPTelegramBot"
    },
    "chat": {
      "id": 68*****41,
      "first_name": "Mike",
      "username": "micheal_ataklt",
      "type": "private"
    },
    "date": 1745171809,
    "contact": {
      "phone_number": "+1234567890",
      "first_name": "John Doe",
      "last_name": "Smith"
    }
  }
}
```

### Setting a Webhook

[](#setting-a-webhook)

Set a webhook for receiving updates:

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

**Example Response**:

```
{
  "ok": true,
  "result": true,
  "description": "Webhook was set"
}
```

### Getting Webhook Info

[](#getting-webhook-info)

Retrieve webhook information:

```
$response = Telegram::getWebhookInfo();
```

**Example Response**:

```
{
  "ok": true,
  "result": {
    "url": "https://your-app.com/telegram/webhook",
    "has_custom_certificate": false,
    "pending_update_count": 4,
    "max_connections": 40,
    "ip_address": "3.18.7.81"
  }
}
```

### Deleting a Webhook

[](#deleting-a-webhook)

Delete the current webhook:

```
$response = Telegram::deleteWebhook();
```

**Example Response**:

```
{
  "ok": true,
  "result": true,
  "description": "Webhook was deleted"
}
```

### Getting Updates

[](#getting-updates)

Retrieve bot updates:

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

**Example Response**:

```
{
  "ok": true,
  "result": [
    {
      "update_id": 261265780,
      "message": {
        "message_id": 18,
        "from": {
          "id": 68*****41,
          "is_bot": false,
          "first_name": "Mike",
          "username": "micheal_ataklt",
          "language_code": "en"
        },
        "chat": {
          "id": 68*****41,
          "first_name": "Mike",
          "username": "micheal_ataklt",
          "type": "private"
        },
        "date": 1745143447,
        "location": {
          "latitude": 8.891756,
          "longitude": 38.834948,
          "live_period": 2147483647
        }
      }
    },
    ...
  ]
}
```

Testing
-------

[](#testing)

The package includes a comprehensive test suite with 100% coverage using Pest. You can test all functionalities in your Laravel project by accessing the `/validate-all-functionalities` route:

```
