uzdevid/php-telegram-bot

Telegram messenjerida bot yaratish uchun PHP kutubxonasi

1.0.1 2024-07-08 09:03 UTC

This package is auto-updated.

Last update: 2024-12-04 09:04:27 UTC


README

PHP-da oson hamda mukammal Telegram bot yaratish uchun kerakli kutubxona.

O'rnatish

Ushbu kengaytmani o'rnatishning eng afzal usuli - composer orqali.

O'rnatish uchun quyidagi buyruqni ishga tushiring:

composer require "uzdevid/php-telegram-bot"

Botning asosiy obyektini yaratish

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);

Matnli habar yuborish

use uzdevid\telegram\bot\message\messages\methods\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

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\messages\methods\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.

Tugmali habar yuborish

use uzdevid\telegram\bot\message\messages\methods\SendMessage;
use uzdevid\telegram\bot\message\messages\keyboard\reply\ReplyButton;
use uzdevid\telegram\bot\message\messages\keyboard\reply\ReplyKeyboardMarkup;
use uzdevid\telegram\bot\message\messages\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\messages\methods\SendMessage;
use uzdevid\telegram\bot\message\messages\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.

Botdan habarlarni qabul qilish va qayta ishlash

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\updates\MessageUpdate;
use uzdevid\telegram\bot\handler\MessageUpdateInterface;
use uzdevid\telegram\bot\objects\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.