PHP code example of lukasss93 / telegrambot-php

1. Go to this page and download the library: Download lukasss93/telegrambot-php 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/ */

    

lukasss93 / telegrambot-php example snippets


use TelegramBot\TelegramBot;

$bot = new TelegramBot($token);
$update=$bot->getWebhookUpdate();
$bot->sendMessage([
    'chat_id' => $update->message->chat->id,
    'text' => 'Hello world!'
]);

use TelegramBot\TelegramBot;

$bot = new TelegramBot($token);
$update=$bot->getWebhookUpdate();
$text=$update->message->text;

// Load a local file to upload. If is already on Telegram's Servers just pass the resource id
$img = curl_file_create('test.png','image/png');
$bot->sendPhoto([
    'chat_id' => $chat_id, 
    'photo' => $img
]);

$file = $bot->getFile($file_id);
$bot->downloadFile($file->file_path, "./my_downloaded_file_on_local_server.png");

$updates=$bot->getUpdated();
for ($i = 0; $i < count($updates); $i++) {
    //single update
    $update=$updates[$i];
    if($update->message->getCommand()=='/start'){
        //working!
    }
}

    //$update->message->text => '/test@ExampleBot my args'
    $command=$update->message->getCommand();
    //$command => '/text'
    

    //$update->message->text => '/test@ExampleBot my args'
    $args_array=$update->message->getArgs();
    //$args_array[0] => 'my'
    //$args_array[1] => 'args'
    $args_string=$update->message->getArgs(true);
    //$args_string => 'my args'
    

$buttons = [ 
    //First row
    [
        $bot->buildKeyboardButton("Button 1"),
        $bot->buildKeyboardButton("Button 2")
    ], 
    //Second row 
    [
        $bot->buildKeyboardButton("Button 3"),
        $bot->buildKeyboardButton("Button 4"),
        $bot->buildKeyboardButton("Button 5")], 
    //Third row
    [
        $bot->buildKeyboardButton("Button 6")]
    ];
    
$bot->sendMessage([
    'chat_id' => $chat_id, 
    'reply_markup' => $bot->buildKeyBoard($buttons), 
    'text' => 'This is a Keyboard Test'
]);

$buttons = [ 
    //First row
    [
        $bot->buildInlineKeyBoardButton("Button 1", $url="http://link1.com"), 
        $bot->buildInlineKeyBoardButton("Button 2", $url="http://link2.com")
    ], 
    //Second row 
    [
        $bot->buildInlineKeyBoardButton("Button 3", $url="http://link3.com"),
        $bot->buildInlineKeyBoardButton("Button 4", $url="http://link4.com"),
        $bot->buildInlineKeyBoardButton("Button 5", $url="http://link5.com")
    ], 
    //Third row
    [
        $bot->buildInlineKeyBoardButton("Button 6", $url="http://link6.com")
    ]
];

$bot->sendMessage([
    'chat_id' => $chat_id, 
    'reply_markup' => $bot->buildInlineKeyBoard($buttons), 
    'text' => 'This is a Keyboard Test'
]);

$bot->buildKeyBoard(array $options, $onetime=true, $resize=true, $selective=true);

$bot->buildInlineKeyBoard(array $inline_keyboard);

$bot->buildInlineKeyBoardButton($text, $url, $callback_data, $switch_inline_query);

$bot->buildKeyBoardButton($text, $url, $request_contact, $request_location);

$bot->buildKeyBoardHide($selective=true);

$bot->buildForceReply($selective=true);