PHP code example of uzdevid / yii2-telegram

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

    

uzdevid / yii2-telegram example snippets


$config = [
    '_bot_' => [
        'token' => '5390057974:AAFsR6ySk6CTPHs9neYkAXdKTO5cer1cdho'
    ]
];

$telegram = new Telegram($config);

$url = "https://example.com/telegrambot";

$telegram->bot->setWebHook($url);

$telegram->bot->deleteWebHook($url);

$text = "Hello world!!!";
$params = [
    'parse_mode'=> 'HTML'
];
$chat_id = 1234567;

$telegram->bot->sender->text($text, $params)->send($chat_id);

$result = $telegram->bot->sender->text($text, $params)->send($chat_id);
file_put_contents('test.json', json_encode($result, JSON_UNESCAPED_UNICODE));

$photo = '/img/elephant.jpg'
$text = "This is elephant photo";
$params = [
    'parse_mode'=> 'HTML'
];
$chat_id = 1234567;

$telegram->bot->sender->photo($photo, $params)->text($text)->send($chat_id);

$telegram->bot->sender->photo($photo, $params)->send($chat_id);

$video = '/img/avengers-final.mp4'
$text = "Avengers: Final";
$params = [
    'parse_mode'=> 'HTML'
];
$chat_id = 1234567;

$telegram->bot->sender->video($photo, $params)->text($text)->send($chat_id);

$telegram->bot->sender->video($photo, $params)->send($chat_id);

$sticker = "CAACAgIAAxkBAAEFRRhiz-WSsSh7GsHDlj8_csvlad9-2gACHQADO3EfIqmCmmAwV9EZKQQ";
$params = [
    'disable_notification'=> true
];
$chat_id = 1234567;

$telegram->bot->sender->sticker($sticker, $params)->send($chat_id);

$phone = '+998993261330';
$first_name = 'Diyorbek';
$last_name = 'Ibragimov';
$params = [
    'disable_notification'=> true
];
$chat_id = 1234567;

$telegram->bot->sender->contact($url, $first_name, $last_name)->send($chat_id);

$question = "Question";
$options = ['variant id-0', 'variant id-1', 'variant id-2'];
$correct_option_id = 1;
$params = ['type' => 'quiz'];
$chat_id = 1234567;

$telegram->bot->sender->poll($question, $options, $correct_option_id, $params)->send($chat_id);

$telegram->bot->sender
    ->text($text)
    ->createKeyboard([['text' => "Button"]])
    ->send($chat_id);

$telegram->bot->sender
    ->photo($photo)
    ->text($text)
    ->createInlineKeyboard([['text' => "URL button", 'url' => "https://devid.uz"]])
    ->send($chat_id);

$callback_data = json_encode(['command' => '/callback', 'id' => 12021]);
$telegram->bot->sender
    ->text($text)
    ->createInlineKeyboard([['text' => 'callback', 'callback_date' => $callback_data]])
    ->send($chat_id);

$telegram->bot->handler->onMessage('/start', function ($body) use ($telegram) {
    // Your code
    $telegram->bot->sender->text("Welcome")->send();
});

$telegram->bot->handler->onCommand('/callback', function ($body, $callback_data) use ($telegram) {
    // Your code
    $telegram->bot->sender->photo('/img/elephant.jpg')->send();
});

$telegram->bot->handler->onQuery('*', function ($query, $body) use ($telegram) {
    // Your code
    $title = "Mode: InlineQuery";
    $description = "Query: {$query}";
    $content = "Answer content";
    
    $telegram->bot->sender->inline
        ->answer([$telegram->bot->sender->inline->article($title, $description, $content)])
        ->send();
});