Skip to content

Commit ecb9376

Browse files
Refactor: Remove internal persistence, add Redis helper, and update README
- Removed MySQL-based internal data persistence (DB, Conversation, ConversationDB). - Made the library stateless by removing all internal storage logic. - Updated Telegram.php version to 1.0.4. - Added a Redis helper (enableRedis, getRedis) using predis/predis for optional shared Redis client access in commands. - Updated README.md with instructions and examples for the new Redis helper.
1 parent 5aa3c6f commit ecb9376

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
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.

0 commit comments

Comments
 (0)