luzrain/telegram-bot-api

Telegram Bot API 的 PHP 封装

v3.7.0 2024-08-26 15:30 UTC

This package is auto-updated.

Last update: 2024-08-26 15:34:34 UTC


README

Bot Api 7.9 PHP >=8.2 Tests Status

这是一个轻量级的面向对象的 PHP 封装,用于 Telegram Bot API,支持所有 Telegram 方法和类型。有关所有可用方法和其参数,请参阅 Telegram Bot API 文档页面。

安装

$ composer require luzrain/telegram-bot-api

Bot API

Telegram Bot 客户端没有与 Guzzle 或其他发送 HTTP 信息的库硬耦合。相反,它使用 PSR-18 客户端抽象。这将为您选择要使用的 PSR-7 实现HTTP 客户端 提供灵活性。

使用 Guzzle 客户端初始化 BotApi 的示例

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Luzrain\TelegramBotApi\BotApi;

$httpFactory = new HttpFactory();
$httpClient = new Client(['http_errors' => false]);

$bot = new BotApi(
    requestFactory: $httpFactory,
    streamFactory: $httpFactory,
    client: $httpClient,
    token: 'API_TOKEN',
);

发送消息

use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotApi\Type;

/**
 * @var Type\Message $response
 */
$response = $bot->call(new Method\SendMessage(
    chatId: 123456789,
    text: 'Example text',
));

带回复键盘发送消息

use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotApi\Type;

$replyKeyboard = new Type\ReplyKeyboardMarkup(
    oneTimeKeyboard: true,
    resizeKeyboard: true,
    keyboard: Type\KeyboardButtonArrayBuilder::create()
        ->addButton(new Type\KeyboardButton(text: 'Button 1'))
        ->addButton(new Type\KeyboardButton(text: 'Button 2'))
        ->addBreak()
        ->addButton(new Type\KeyboardButton(text: 'Web App', webApp: new Type\WebAppInfo('https://github.com/')))
        ->addButton(new Type\KeyboardButton(text: 'Create Poll', requestPoll: new Type\KeyboardButtonPollType())),
);

// For keyboard remove
// $replyKeyboard = new Type\ReplyKeyboardRemove();

/**
 * @var Type\Message $response
 */
$response = $bot->call(new Method\SendMessage(
    chatId: 123456789,
    text: 'Example text',
    replyMarkup: $replyKeyboard,
));

带内联键盘发送消息

use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotApi\Type;

$inlineKeyboard = new Type\InlineKeyboardMarkup(
    inlineKeyboard: Type\InlineKeyboardButtonArrayBuilder::create()
        ->addButton(new Type\InlineKeyboardButton(text: 'Url button', url: 'https://google.com'))
        ->addButton(new Type\InlineKeyboardButton(text: 'Callback button', callbackData: 'callback_data'))
        ->addBreak()
        ->addButton(new Type\InlineKeyboardButton(text: 'Iinline query', switchInlineQueryCurrentChat: 'test')),
);

/**
 * @var Type\Message $response
 */
$response = $bot->call(new Method\SendMessage(
    chatId: 123456789,
    text: 'Example text',
    replyMarkup: $inlineKeyboard ,
));

发送照片/视频/文档

use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotApi\Type;

/**
 * Upload image from local filesystem
 * @var Type\Message $response
 */
$response = $bot->call(new Method\SendPhoto(
    chatId: 123456789,
    photo: new Type\InputFile('/home/user/img/15311661465960.jpg'),
));

/**
 * Send image from the Internet
 * @var Type\Message $response
 */
$response = $bot->call(new Method\SendPhoto(
    chatId: 123456789,
    photo: 'https://avatars3.githubusercontent.com/u/9335727',
));

/**
 * Upload Document
 * @var Type\Message $response
 */
$response = $bot->call(new Method\SendDocument(
    chatId: 123456789,
    document: new Type\InputFile('/home/user/files/file.zip'),
    thumbnail: new Type\InputFile('/home/user/img/thumb.jpg'),
    caption: 'Test file',
));

/**
 * You can also use these methods:
 * SendPhoto, SendAudio, SendDocument, SendVideo, SendAnimation, SendVoice, SendVideoNote
 */

发送媒体组

use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotApi\Type;

/**
 * @var Type\Message[] $response
 */
$response = $bot->call(new Method\SendMediaGroup(
    chatId: 123456789,
    media: [
        new Type\InputMediaPhoto(
            media: new Type\InputFile('/home/user/img/15311661465960.jpg'),
            caption: 'Test media 1',
        ),
        new Type\InputMediaPhoto(
            media: new Type\InputFile('/home/user/img/16176321866250.png'),
            caption: 'Test media 2',
        ),
    ],
));

客户端 API

Webhook 客户端

use Luzrain\TelegramBotApi\ClientApi;
use Luzrain\TelegramBotApi\Event;
use Luzrain\TelegramBotApi\Method;
use Luzrain\TelegramBotApi\Type;

$client = new ClientApi();

// Handle any type of update
$client->on(new Event\Update(function(Type\Update $update) {
    // Any update received
}));

// Handle /ping command
$client->on(new Event\Command('/ping', function(Type\Message $message) {
    /**
     * You can return any Method object from here, and it will be sent as an answer to the webhook.
     * Be aware that your cannot send methods with uploading local files from here, use BotApi instead.
     */
    return new Method\SendMessage(
        chatId: $message->chat->id,
        text: 'pong!',
    );
}));

// Handle text messages
$client->on(new Event\Message(function(Type\Message $message) {
    return new Method\SendMessage(
        chatId: $message->chat->id,
        text: 'Your message: ' . $message->text,
    );
}));

$client->run();