devrabie/php-telegram-bot-plus

A community-driven and updated fork of the popular php-telegram-bot/core library, with support for the latest Telegram Bot API features.

v1.0.6 2025-07-16 11:50 UTC

README

Latest Version on Packagist Software License Issues

A community-driven and updated fork of the popular php-telegram-bot/core library, with support for the latest Telegram Bot API features.

πŸ“– Overview

php-telegram-bot-plus is an enhanced and actively maintained fork of the great longman/php-telegram-bot library. This project was created to continue its development after the original project stalled, ensuring full support for the latest Telegram Bot API features and providing ongoing bug fixes and improvements for the community.

Our goal is to be a drop-in replacement for existing users, while providing powerful new features for new developers.

✨ Why choose php-telegram-bot-plus?

  • πŸš€ Up-to-date: Full support for the latest versions of the Telegram Bot API.
  • πŸ› οΈ Actively Maintained: Bugs from the original version are actively being fixed.
  • 🧩 New Features: Adding powerful new features that were not previously available.
  • 🀝 Community-Driven: We welcome contributions from developers to make the library better for everyone.
  • πŸ”’ Compatible: We maintain backward compatibility as much as possible to make upgrading easy.

πŸ“¦ Installation

composer require devrabie/php-telegram-bot-plus

πŸš€ Using the Redis Helper

This library provides a simple helper to integrate a Redis client, allowing you to easily use Redis for your custom data persistence needs (e.g., storing user states, settings, caching). The library itself remains stateless.

1. Enable Redis

In your main bot file (e.g., hook.php or your script that handles updates):

<?php

require_once __DIR__ . '/vendor/autoload.php';

$bot_api_key  = 'YOUR_BOT_API_KEY';
$bot_username = 'YOUR_BOT_USERNAME';

$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);

// Initialize the Redis client and make it available to all commands
// Default connection: 127.0.0.1:6379
$telegram->enableRedis();

// Or with custom connection parameters:
// $telegram->enableRedis([
//    'host'   => 'your-redis-host',
//    'port'   => 6379,
//    // 'password' => 'your-redis-password'
// ]);

// Handle updates
$telegram->handle();

2. Use Redis in Your Commands

You can access the shared Redis client instance from any command class using automatic dependency injection. Simply add a redis property to your command class and it will be automatically populated:

<?php

namespace Longman\TelegramBot\Commands\UserCommands;

use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Request;

class SettingsCommand extends UserCommand
{
    protected $name = 'settings';
    protected $description = 'Manage user settings using Redis';
    protected $usage = '/settings';
    protected $version = '1.0.0';

    /**
     * @var \Redis
     */
    protected $redis;

    public function execute()
    {
        $message = $this->getMessage();
        $chat_id = $message->getChat()->getId();

        if ($this->redis) {
            $settings_key = 'bot:settings:' . $chat_id;

            // Example: Use Redis to store custom settings for a chat
            $this->redis->hset($settings_key, 'language', 'en');
            $lang = $this->redis->hget($settings_key, 'language');

            $text = 'Language set to: ' . $lang . ' (using Redis!)';
        } else {
            $text = 'Redis is not enabled.';
        }

        return Request::sendMessage([
            'chat_id' => $chat_id,
            'text'    => $text,
        ]);
    }
}

You can also access the Redis instance statically from anywhere in your project:

$redis = \Longman\TelegramBot\Telegram::getRedis();

πŸ™ Acknowledgments A huge thanks to the original developers of longman/php-telegram-bot for their incredible work that formed the foundation of this project.