PHP code example of devrabie / php-telegram-bot-plus

1. Go to this page and download the library: Download devrabie/php-telegram-bot-plus library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devrabie / php-telegram-bot-plus example snippets




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();



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,
        ]);
    }
}