The preferred way to install this extension is through composer.
Either run
composer require uzdevid/yii2-telegram "1.2.0"
or add
"uzdevid/yii2-telegram": "1.2.0"
to the require section of your composer.json
file.
Create telegram
extension with config params
$config = [
'_bot_' => [
'token' => '5390057974:AAFsR6ySk6CTPHs9neYkAXdKTO5cer1cdho'
]
];
$telegram = new Telegram($config);
$url
- url to Your handler
$url = "https://example.com/telegrambot";
$telegram->bot->setWebHook($url);
$telegram->bot->deleteWebHook($url);
Send text [docs]
$text
- [string
|required
] - message text.$params
- [array
|optional
] - additional params: parse_mode, entities, disable_web_page_preview and other. Read the telegram bot docs...$chat_id
- [integer
|required
] - telegram user chat id.
$text = "Hello world!!!";
$params = [
'parse_mode'=> 'HTML'
];
$chat_id = 1234567;
$telegram->bot->sender->text($text, $params)->send($chat_id);
Note: All send requests return a response from telegram
Example
$result = $telegram->bot->sender->text($text, $params)->send($chat_id);
file_put_contents('test.json', json_encode($result, JSON_UNESCAPED_UNICODE));
Send photo [docs]
$photo
- [string
|required
] - path to photo.$params
- [array
|optional
] - additional params: parse_mode, caption_entities, disable_notification and other. Read the telegram bot docs...$text
- [string
|optional
] - caption for photo.$chat_id
- [integer
|required
] - telegram user chat id.
$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);
or you can send a photo without a caption
$telegram->bot->sender->photo($photo, $params)->send($chat_id);
Send video [docs]
$video
- [string
|required
] - path to video.$params
- [array
|optional
] - additional params: duration, width, height and other. Read the telegram bot docs...$text
- [string
|optional
] - caption for video.$chat_id
- [integer
|required
] - telegram user 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);
or you can send a video without a caption
$telegram->bot->sender->video($photo, $params)->send($chat_id);
Send sticker [docs]
$sticker
[string
|required
] - sticker id.$params
- [array
|optional
] - additional params: disable_notification, protect_content, reply_to_message_id and other. Read the telegram bot docs...$chat_id
- [integer
|required
] - telegram user chat id.
$sticker = "CAACAgIAAxkBAAEFRRhiz-WSsSh7GsHDlj8_csvlad9-2gACHQADO3EfIqmCmmAwV9EZKQQ";
$params = [
'disable_notification'=> true
];
$chat_id = 1234567;
$telegram->bot->sender->sticker($sticker, $params)->send($chat_id);
Send contact [docs]
$phone
[string
|required
] - phone number.$first_name
[string
|required
] - first name.$last_name
[string
|optional
] - last name.$params
- [array
|optional
] - additional params: vcard, disable_notification, protect_content and other. Read the telegram bot docs...$chat_id
- [integer
|required
] - telegram user 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);
Send poll [docs]
$question
- [string
|required
] - Question.$options
- [array
|required
] - Options.$correct_option_id
- [integer
|optional
] - Correct option id, Required for polls in quiz mode.$params
- [array
|optional
] - additional params: type, allows_multiple_answers, explanation and other. Read the telegram bot docs...
$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);
Keyboard
$telegram->bot->sender
->text($text)
->createKeyboard([['text' => "Button"]])
->send($chat_id);
URL inline keyboard
$telegram->bot->sender
->photo($photo)
->text($text)
->createInlineKeyboard([['text' => "URL button", 'url' => "https://devid.uz"]])
->send($chat_id);
callback inline keyboard
$callback_data = json_encode(['command' => '/callback', 'id' => 12021]);
$telegram->bot->sender
->text($text)
->createInlineKeyboard([['text' => 'callback', 'callback_date' => $callback_data]])
->send($chat_id);
Note: When processing requests, there is no need to specify a chat id when sending a response to a request.
Processing /start
request
$telegram->bot->handler->onMessage('/start', function ($body) use ($telegram) {
// Your code
$telegram->bot->sender->text("Welcome")->send();
});
Processing callback query
$telegram->bot->handler->onCommand('/callback', function ($body, $callback_data) use ($telegram) {
// Your code
$telegram->bot->sender->photo('/img/elephant.jpg')->send();
});
Processing all (*) inline query
$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();
});