Skip to content

Refactor/stateless redis #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,89 @@ Our goal is to be a drop-in replacement for existing users, while providing powe
composer require devrabie/php-telegram-bot-plus
```

## 🚀 Using the Redis Helper

This library provides a simple helper to integrate a [Predis](https://github.com/predis/predis) 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
<?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: tcp://127.0.0.1:6379
$telegram->enableRedis();

// Or with custom connection parameters:
// $telegram->enableRedis([
// 'scheme' => 'tcp',
// '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 `getRedis()`:

```php
<?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';

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

// Get the shared Redis client instance.
/** @var \Predis\Client|null $redis */
$redis = $this->getTelegram()->getRedis();

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

// Example: Use Redis to store custom settings for a chat
$redis->hset($settings_key, 'language', 'en');
$lang = $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,
]);
}
}
```

---

🙏 Acknowledgments
A huge thanks to the original developers of longman/php-telegram-bot for their incredible work that formed the foundation of this project.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"psr/log": "^1.1|^2.0|^3.0",
"guzzlehttp/guzzle": "^6.0|^7.0"
"guzzlehttp/guzzle": "^6.0|^7.0",
"predis/predis": "^2.0",
"psr/log": "^1.1|^2.0|^3.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
26 changes: 3 additions & 23 deletions src/Commands/SystemCommands/GenericmessageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,12 @@ class GenericmessageCommand extends SystemCommand
/**
* @var string
*/
protected $version = '1.2.0';
protected $version = '1.2.1'; // Updated version

/**
* @var bool
*/
protected $need_mysql = true;

/**
* Execution if MySQL is required but not available
*
* @return ServerResponse
* @throws TelegramException
*/
public function executeNoDb(): ServerResponse
{
// Try to execute any deprecated system commands.
if (self::$execute_deprecated && $deprecated_system_command_response = $this->executeDeprecatedSystemCommand()) {
return $deprecated_system_command_response;
}

return Request::emptyResponse();
}
protected $need_mysql = false; // MySQL is no longer used

/**
* Execute command
Expand All @@ -66,11 +50,7 @@ public function executeNoDb(): ServerResponse
*/
public function execute(): ServerResponse
{
// Try to continue any active conversation.
if ($active_conversation_response = $this->executeActiveConversation()) {
return $active_conversation_response;
}

// Conversation logic removed.
// Try to execute any deprecated system commands.
if (self::$execute_deprecated && $deprecated_system_command_response = $this->executeDeprecatedSystemCommand()) {
return $deprecated_system_command_response;
Expand Down
216 changes: 0 additions & 216 deletions src/Conversation.php

This file was deleted.

Loading