Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Telegram Bot Laravel Package

aymericcucherousset/telegram-bot

A simple PHP Telegram Bot library for sending messages, handling updates, and interacting with Telegram’s Bot API. Includes helpers for requests, webhooks/long polling, keyboards, and common methods to build bots quickly and cleanly.

View on GitHub
Deep Wiki
Context7

Testing

Testing is a first-class concern in the telegram-bot-sdk. The package is designed for high testability, leveraging strict typing, value objects, and clear boundaries between domain and infrastructure.

Unit Testing

Write fast, isolated tests for your domain logic, handlers, and value objects. Use PHPUnit as the test runner. Example:

use Aymericcucherousset\TelegramBot\Value\ChatId;
use PHPUnit\Framework\TestCase;

class ChatIdTest extends TestCase
{
	public function testValueObject(): void
	{
		$chatId = new ChatId(123456);
		$this->assertSame(123456, $chatId->toInt());
	}
}

Mocking HTTP Clients

The SDK relies on PSR-18 HTTP clients. For unit tests, mock the HTTP client to avoid real API calls. Use libraries like php-http/mock-client or PHPUnit's built-in mocking:

use Aymericcucherousset\TelegramBot\Api\HttpTelegramClient;
use Psr\Http\Client\ClientInterface;
use PHPUnit\Framework\TestCase;

class HttpTelegramClientTest extends TestCase
{
	public function testSendRequest(): void
	{
		$mock = $this->createMock(ClientInterface::class);
		// Configure $mock to expect/send requests
		$client = new HttpTelegramClient('TOKEN', $mock);
		// ...assertions...
	}
}

Test Utilities

  • FakeTelegramClient: Simulate Telegram API responses for deterministic tests.
  • SpyCommand: Assert command invocations and side effects.
  • Test factories: Quickly create Update, Message, and CallbackQuery objects.

See the tests/ directory for real-world examples and patterns.

Note on Bot and UpdateFactory

When testing code that uses UpdateFactory, remember that it now requires a Bot instance as the second argument:

use Aymericcucherousset\TelegramBot\Update\UpdateFactory;
use Aymericcucherousset\TelegramBot\Bot\Bot;

$bot = new Bot($client); // $client is your TelegramClientInterface implementation (can be a fake/mock)
$update = UpdateFactory::fromJson($json, $bot);
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours