PHP code example of justify / telegram-bot-api

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

    

justify / telegram-bot-api example snippets


define('TELEGRAM_TOKEN', 'Your token');

$bot = new Justify\TelegramBotApi\Client(TELEGRAM_TOKEN);

$bot->sendMessage($chatId, 'Hello!');
$bot->sendPhoto($chatId, 'File id || URL || path to file', [
    'caption' => 'My photo'
]);

$response = $bot->sendRequest('getFile', [
    'file_id' => $fileId
]);

$bot->handler(function ($message) use ($bot) {
    // Actions ...
});

// Will react on "start" command
$bot->command('start', function ($message) use ($bot) {
    // Actions ...
});

// Will react on "Some text" text
$bot->text('Some text', function ($message) use ($bot) {
    // Actions ...
});

// Will react if text messages will match to regexp pattern
$bot->regexp('Password (\d+)', function ($message, $matches) use ($bot) {
    // Actions ...
});

// Will react if message will have "photo" type
$bot->type('photo', function ($message) use ($bot) {
    // Actions ...
});
$

$bot->sendPhoto($chatId, 'Pass file id or file url or path to file');

$photo = new Justify\TelegramBotApi\Types\InputMediaPhoto('Pass file id or file url or path to file', [
    'caption' => 'My photo'
]);
$video = new Justify\TelegramBotApi\Types\InputMediaVideo('Pass file id or file url or path to file', [
    'caption' => 'My video'
]);

$bot->sendMediaGroup($chatId, [$photo, $video]);

$keyboard = new Justify\TelegramBotApi\Types\ReplyKeyboardMarkup([
    'resize_keyboard' => true,
    'ont_time_keyboard' => true
]);

$keyboard->row('1', '2', '3')
    ->row('4', '5', '6')
    ->row('7', '8', '9')
    ->row('0');

$bot->sendMessage($chatId, 'Choose a number', ['reply_markup' => $keyboard]);


// Removing keyboard
$removeKeyboard = new Justify\TelegramBotApi\Types\ReplyKeyboardRemove();
$bot->sendMessage($chatId, 'Removing keyboard', ['reply_markup' => $removeKeyboard]);

$bot->polling();



define('TELEGRAM_TOKEN', 'Pass your token here');

$bot = new Justify\TelegramBotApi\Client(TELEGRAM_TOKEN);

$bot->command('start', function ($message) use ($bot) {
    $keyboard = new Justify\TelegramBotApi\Types\ReplyKeyboardMarkup([
        'resize_keyboard' => true
    ]);
    $keyboard->row('Photo', 'Audio')
        ->row('Document', 'Video')
        ->row('/stop');

    $bot->sendMessage($message->chat->id, 'Welcome!', ['reply_markup' => $keyboard]);
});

$bot->command('stop', function ($message) use ($bot) {
    $bot->sendMessage($message->chat->id, 'Completed', [
        'reply_markup' => new Justify\TelegramBotApi\Types\ReplyKeyboardRemove()
    ]);
});

$bot->text('Photo', function ($message) use ($bot) {
    $bot->sendPhoto($message->chat->id, 'File id || URL || path to file');
});

$bot->text('Audio', function ($message) use ($bot) {
    $bot->sendAudio($message->chat->id, 'File id || URL || path to file');
});

$bot->text('Document', function ($message) use ($bot) {
    $bot->sendDocument($message->chat->id, 'File id || URL || path to file');
});

$bot->text('Video', function ($message) use ($bot) {
    $bot->sendVideo($message->chat->id, 'File id || URL || path to file');
});

try {
    $bot->polling();
} catch (Justify\TelegramBotApi\Exception $e) {
    echo $e->getMessage();
}