PHP-da oson hamda mukammal Telegram bot yaratish uchun kerakli kutubxona.
Ushbu kengaytmani o'rnatishning eng afzal usuli - composer orqali.
O'rnatish uchun quyidagi buyruqni ishga tushiring:
composer require "uzdevid/php-telegram-bot"
Bot sozlamalari
$config = [
'token' => '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11', // Bot tokeni
];
Bot tokenini @BotFather dan olishingiz mumkin
Agar bot doyimiy faqat bitta telegram akkaunt bilan ishlaydigan bo'lsa chat_id yoki username parametrini ham kiritishingiz mumkin. Shu holda habar yuborayotganda ushbu parametrlarni kiritishingiz shart bo'lmaydi
Bot obyektini yaratish
$bot = new \uzdevid\telegram\bot\Bot($config);
use uzdevid\telegram\bot\Message\Message\Method\SendMessage;
$bot->sender()
->chatId(123456789) // yoki ->username('@uzdevid')
->method(new SendMessage('Salom. Bu matnli habar'))
->send();
chatId() - habar yuboriladigan chat id sini qabul qabul qiladi.
sender() - so'rov yuborishga mo'ljallangan.
method() - yuboriladigan so'rovni qabul qabul qiladi.
send() - so'rovni yuboradi.
Rasmli habar yuborish ham huddi matnli habar yuborish kabi amalga oshiriladi. Faqatgi farqi rasmni yuborish uchun method() metodiga SendPhoto obyektini uzatish kerak.
use uzdevid\telegram\bot\Message\Message\Method\SendPhoto;
$sendPhoto = new SendPhoto('https://uzdevid.com/img/logo.png');
$sendPhoto->caption('Bu rasmli habar')->hasSpoiler();
$bot->sender()
->chatId(123456789)
->method($sendPhoto)
->send();
SendPhoto - rasmli habar yuborish uchun kerakli klass.
caption() - rasmli habar yuboriladigan matn.
hasSpoiler() - rasmni spoiler ko'rinishida yuborish.
use uzdevid\telegram\bot\Message\Message\Method\SendMessage;
use uzdevid\telegram\bot\Message\Message\Keyboard\reply\ReplyButton;
use uzdevid\telegram\bot\Message\Message\Keyboard\reply\ReplyKeyboardMarkup;
use uzdevid\telegram\bot\Message\Message\Keyboard\reply\ReplyRow;
$replyMarkup = new ReplyKeyboardMarkup();
$replyMarkup->resizeKeyboard()->addRow(
(new ReplyRow())
->addButton(new ReplyButton('Tugma 1'))
->addButton((new ReplyButton('Joylashuv uchun tugma'))->requestLocation())
->addButton((new ReplyButton('Telefon raqam uchun tugma'))->requestContact())
);
$sendMessage = new SendMessage('Bu tugmali habar');
$sendMessage->addReplyMarkup($replyMarkup);
$bot->sender()
->chatId(123456789)
->method($sendMessage)
->send();
Tugmali habar yuborilganidan so'ng undan keyingi habardan tugmalarni o'chirish uchun yangi habarni shunday ko'rinishda yuborish kerak
use uzdevid\telegram\bot\Message\Message\Method\SendMessage;
use uzdevid\telegram\bot\Message\Message\Keyboard\reply\ReplyKeyboardRemove;
$sendMessage = new SendMessage('Bu oddiy habar');
$sendMessage->addReplyMarkup(new ReplyKeyboardRemove());
ReplyKeyboardMarkup - tugma yaratish uchun kerakli klass.
resizeKeyboard() - tugmalarni o'lchamini o'zgartirish mumkinligini yoqish.
addRow() - tugmalarga yangi qator yaratadi.
ReplyRow - tugmalar uchun yangi qator yaratish uchun kerakli klass.
addButton() - tugma qo'shish.
ReplyButton tugma yaratish uchun kerakli klass.
addReplyMarkup() - tugmalarni habarga biriktirib yuborish uchun kerakli metod.
index.php
namespace App;
$requestBody = json_decode(file_get_contents('php://input'), true);
$bot->handler($requestBody)
->on(ChatStart::class)
$requestBody - telegramdan kelgan so'rov.
handler() - so'rovlarni qabul qiluvchi va qayta ishlovchi metod.
on() - qabul qilinadigan so'rovlarni qo'shish uchun kerakli metod.
ChatStart.php
namespace App\Handlers;
use uzdevid\telegram\bot\Handler\update\Message\MessageUpdate;use uzdevid\telegram\bot\Handler\update\Message\MessageUpdateInterface;use uzdevid\telegram\bot\Type\Message;
class ChatStart extends MessageUpdate implements MessageUpdateInterface {
public static function canHandle(Bot $bot, Message $message): bool {
return $message->text == '/start';
}
public function handle(Bot $bot, Message $message): void {
$bot->sender()
->chatId($message->chat->id)
->method(new SendMessage('Salom. Botga xush kelibsiz'))
->send();
}
}
canHandle() - so'rovni qabul qilinadimi yoki yo'qmi tekshiradi.
handle() - so'rovni qabul qilgandan so'ng ishlovchi metod.