PHP code example of natilosir / telegram-bot-sdk

1. Go to this page and download the library: Download natilosir/telegram-bot-sdk 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/ */

    

natilosir / telegram-bot-sdk example snippets



use natilosir\bot\Log;

// Initialize the logger (done automatically)
AdvancedLogger::getInstance();

// Log messages using the Log class
Log::info('Application started');
Log::debug('User data', ['user_id' => 123, 'name' => 'John']);
Log::error('Database connection failed', ['error_code' => 500]);

// Log using the global lg function
lg('Processing request', 'INFO', ['request_id' => 'abc123']);
lg(['data' => ['key' => 'value']], 'DEBUG', ['context' => 'additional info']);

// Dump and die for debugging
dd(['variable' => 'test']);

use natilosir\bot\Route;

Route::add(['/start', '๐Ÿ  Home', 'Cancel'], [StartController::class, 'hello']);

Route::add(['/start', 'Back'], 'start')

use natilosir\bot\Route;
use Controllers\StartController;

// Define a route with multiple input patterns
Route::add(['start', '/start', '๐Ÿ  Home', 'Cancel'], [StartController::class, 'hello']);

// Define a route with a state
Route::add('/profile', [ProfileController::class, 'show'])->state('profile_state');

// Define a default route for unmatched inputs
Route::def([DefaultController::class, 'handle']);

    Route::add('/start', [StartController::class, 'hello']);
    Route::add(['help', '/help'], [HelpController::class, 'show']);
    

    Route::add('/profile', [ProfileController::class, 'show'])->state('profile_state');
    

    Route::def([DefaultController::class, 'handle']);
    

namespace Controllers;

class StartController {
    public function hello(Request $request) {
        // Handle the request
        return "Welcome to the bot!";
    }
}

use natilosir\bot\State;
use Controllers\ProfileController;

// Define a state with multiple input patterns
State::add(['profile', '/profile', '๐Ÿ‘ค Profile'], [ProfileState::class, 'show']);


use natilosir\bot\Request;

class StartController {
    public function hello(Request $request) {
        // Access common properties
        $chatID = $request->chatID;
        $userID = $request->fromID;
        $text   = $request->text;

        // Check if the input is a command
        if ($request->isCommand()) {
            echo "Command received: " . $request->getCommand();
        }

        // Get the input (text, callback data, or query)
        $input = $request->getInput();

        // Convert request to array for processing
        $data = $request->toArray();
    }
}

    $input = $request->getInput();
    

    $type = $request->getUpdateType();
    

    if ($request->isCommand()) {
        echo "This is a command!";
    }
    

    $command = $request->getCommand();
    

    $data = $request->toArray();
    

    $json = $request->toJson();
    

    $raw = $request->getRawData();
    


use natilosir\bot\Http;

class StartController {
    public function hello() {
        // Quick POST request
        $response = Http::post('https://api.example.com/endpoint', [
            'key' => 'value',
        ]);

        if ($response->successful()) {
            echo "Response: " . json_encode($response->body());
        } else {
            echo "Request failed with status: " . $response->status();
        }

        // GET request with query parameters
        $response = Http::get('https://api.example.com/search', [
            'q'    => 'example',
            'page' => 1,
        ]);

        // Check response
        if ($response->successful()) {
            $data = $response->json();
            echo "Data: " . json_encode($data);
        }
    }
}

    $response = Http::post('https://api.example.com/submit', ['key' => 'value']);
    

    $response = Http::get('https://api.example.com/search', ['q' => 'test']);
    

    $response = Http::put('https://api.example.com/update', ['id' => 1, 'value' => 'new']);
    

    $response = Http::patch('https://api.example.com/patch', ['key' => 'updated']);
    

    $response = Http::delete('https://api.example.com/delete', ['id' => 1]);
    

    $http = Http::new()->withHeaders(['Content-Type' => 'application/json']);
    

    $http = Http::new()->withOptions([CURLOPT_TIMEOUT => 30]);
    

    $response = $http->send('POST', '/submit', ['form_params' => ['key' => 'value']]);
    

    $status = $response->status();
    

    $body = $response->body();
    

    $json = $response->json();
    

    if ($response->successful()) {
        echo "Request succeeded!";
    }
    

    if ($response->failed()) {
        echo "Request failed!";
    }
    

// Define chat ID and message ID
$chatID     = 123456789;
$message_id = 42;

// Use the deleteMessage method
Bot::deleteMessage($chatID, $message_id);

// Define chat IDs and message ID
$chatID       = 123456789;
$from_chat_id = 987654321;
$message_id   = 42;

// Use the forwardMessage method
Bot::forwardMessage($chatID, $from_chat_id, $message_id);

// Define the chat ID and message text
$chatID      = 123456789;
$messageText = "Hello! How can I assist you today?";

// Send a message to the chat
Bot::sendMessage($chatID, $messageText);


namespace natilosir\bot;

// Define chat IDs and message ID
$chatID       = 123456789;
$from_chat_id = 987654321;
$message_id   = 42;

// Use the copyMessage method
Bot::copyMessage($chatID, $from_chat_id, $message_id);

$response = Bot::row([
    Bot::column('โ“ What is this bot? What is it used for?', 'option_1'),
])
->row([
    Bot::column('๐Ÿ”— How do I connect to a random stranger?', 'option_2'),
])
->row([
    Bot::column('๐Ÿ’Œ How do I connect to a specific contact?', 'option_3'),
]);

$text = "๐Ÿ”Ž Just tap the desired button below๐Ÿ‘‡๐Ÿป";
$response = Bot::inline($chatID, $text, $message_id);

$response = Bot::row([
    Bot::column('Account Information'),
])
->row([
    Bot::column('Help'),
    Bot::column('Contact Us'),
]);

if ($text == 'Back') {
    $text = 'You have returned to the main menu.\n\nPlease select one of the options below.';
} else {
    $text = 'Please select an option from the menu below.';
}

$response = Bot::keyboard($chatID, $text, $message_id);

Bot::row([
    Bot::column('๐Ÿ”“ Unblock', 'unblock' . $message_id_from_callback),
    Bot::column('โœ Reply', 'reply' . $message_id_from_callback),
])
->row([
    Bot::column('๐Ÿšซ Report User', 'report' . $message_id_from_callback),
]);

Bot::alert($query_id, '๐Ÿšซ Blocked!');
Bot::inline($chatID, null, $message_id, 'edit');

Bot::answerCallbackQuery($query_id, "Your action was successful!", true);

$query_id     = '1234567890';
$message_text = 'This is an alert message!';
Bot::alert($query_id, $message_text, true);

$chatID = 123456789;
$action = 'typing';
Bot::sendChatAction($chatID, $action);
bash
composer atilosir/bot/install.php
bash
editor.php?file=app/Controllers/StartController.php