Skip to content

Commit bf149ac

Browse files
authored
Merge pull request #6 from devrabie/refactor/stateless-redis
Refactor/stateless redis
2 parents fc28e3e + ecb9376 commit bf149ac

File tree

7 files changed

+131
-822
lines changed

7 files changed

+131
-822
lines changed

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,89 @@ Our goal is to be a drop-in replacement for existing users, while providing powe
2828
composer require devrabie/php-telegram-bot-plus
2929
```
3030

31+
## 🚀 Using the Redis Helper
32+
33+
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.
34+
35+
### 1. Enable Redis
36+
37+
In your main bot file (e.g., `hook.php` or your script that handles updates):
38+
39+
```php
40+
<?php
41+
42+
require_once __DIR__ . '/vendor/autoload.php';
43+
44+
$bot_api_key = 'YOUR_BOT_API_KEY';
45+
$bot_username = 'YOUR_BOT_USERNAME';
46+
47+
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
48+
49+
// Initialize the Redis client and make it available to all commands
50+
// Default connection: tcp://127.0.0.1:6379
51+
$telegram->enableRedis();
52+
53+
// Or with custom connection parameters:
54+
// $telegram->enableRedis([
55+
// 'scheme' => 'tcp',
56+
// 'host' => 'your-redis-host',
57+
// 'port' => 6379,
58+
// // 'password' => 'your-redis-password'
59+
// ]);
60+
61+
// Handle updates
62+
$telegram->handle();
63+
```
64+
65+
### 2. Use Redis in Your Commands
66+
67+
You can access the shared Redis client instance from any command class using `getRedis()`:
68+
69+
```php
70+
<?php
71+
72+
namespace Longman\TelegramBot\Commands\UserCommands;
73+
74+
use Longman\TelegramBot\Commands\UserCommand;
75+
use Longman\TelegramBot\Request;
76+
77+
class SettingsCommand extends UserCommand
78+
{
79+
protected $name = 'settings';
80+
protected $description = 'Manage user settings using Redis';
81+
protected $usage = '/settings';
82+
protected $version = '1.0.0';
83+
84+
public function execute()
85+
{
86+
$message = $this->getMessage();
87+
$chat_id = $message->getChat()->getId();
88+
89+
// Get the shared Redis client instance.
90+
/** @var \Predis\Client|null $redis */
91+
$redis = $this->getTelegram()->getRedis();
92+
93+
if ($redis) {
94+
$settings_key = 'bot:settings:' . $chat_id;
95+
96+
// Example: Use Redis to store custom settings for a chat
97+
$redis->hset($settings_key, 'language', 'en');
98+
$lang = $redis->hget($settings_key, 'language');
99+
100+
$text = 'Language set to: ' . $lang . ' (using Redis!)';
101+
} else {
102+
$text = 'Redis is not enabled.';
103+
}
104+
105+
return Request::sendMessage([
106+
'chat_id' => $chat_id,
107+
'text' => $text,
108+
]);
109+
}
110+
}
111+
```
112+
113+
---
114+
31115
🙏 Acknowledgments
32116
A huge thanks to the original developers of longman/php-telegram-bot for their incredible work that formed the foundation of this project.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
"ext-curl": "*",
4141
"ext-json": "*",
4242
"ext-mbstring": "*",
43-
"psr/log": "^1.1|^2.0|^3.0",
44-
"guzzlehttp/guzzle": "^6.0|^7.0"
43+
"guzzlehttp/guzzle": "^6.0|^7.0",
44+
"predis/predis": "^2.0",
45+
"psr/log": "^1.1|^2.0|^3.0"
4546
},
4647
"require-dev": {
4748
"phpunit/phpunit": "^9.5",

src/Commands/SystemCommands/GenericmessageCommand.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,12 @@ class GenericmessageCommand extends SystemCommand
3535
/**
3636
* @var string
3737
*/
38-
protected $version = '1.2.0';
38+
protected $version = '1.2.1'; // Updated version
3939

4040
/**
4141
* @var bool
4242
*/
43-
protected $need_mysql = true;
44-
45-
/**
46-
* Execution if MySQL is required but not available
47-
*
48-
* @return ServerResponse
49-
* @throws TelegramException
50-
*/
51-
public function executeNoDb(): ServerResponse
52-
{
53-
// Try to execute any deprecated system commands.
54-
if (self::$execute_deprecated && $deprecated_system_command_response = $this->executeDeprecatedSystemCommand()) {
55-
return $deprecated_system_command_response;
56-
}
57-
58-
return Request::emptyResponse();
59-
}
43+
protected $need_mysql = false; // MySQL is no longer used
6044

6145
/**
6246
* Execute command
@@ -66,11 +50,7 @@ public function executeNoDb(): ServerResponse
6650
*/
6751
public function execute(): ServerResponse
6852
{
69-
// Try to continue any active conversation.
70-
if ($active_conversation_response = $this->executeActiveConversation()) {
71-
return $active_conversation_response;
72-
}
73-
53+
// Conversation logic removed.
7454
// Try to execute any deprecated system commands.
7555
if (self::$execute_deprecated && $deprecated_system_command_response = $this->executeDeprecatedSystemCommand()) {
7656
return $deprecated_system_command_response;

src/Conversation.php

Lines changed: 0 additions & 216 deletions
This file was deleted.

0 commit comments

Comments
 (0)