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