tsvetkov/telegram_bot

此包的最新版本(2.6.1)没有可用的许可证信息。

PHP库,用于Telegram机器人API

2.6.1 2020-12-03 15:12 UTC

README

Tsvetkov的Telegram机器人

Latest Stable Version Total Downloads

安装

要安装此扩展,请使用Composer。运行以下命令之一:

php composer.phar require tsvetkov/telegram_bot

"tsvetkov/telegram_bot": "*"

将其添加到你的composer.json文件的require部分。

基本用法

初始化

use tsvetkov\telegram_bot\TelegramBot;

$bot = new TelegramBot($token); // You can get token of you bots from @BotFather

// With proxy
$requestOptions = [
    'proxy' => 'your_proxy_config',
];
$bot = new TelegramBot($token, $requestOptions);

发送消息

$bot->sendMessage($userId, 'Hello world!');

获取更新

$updates = $bot->getUpdates();

Webhooks

$data = json_decode(file_get_contents('php://input'));

$update = new \tsvetkov\telegram_bot\entities\update\Update($data);

如何发送带有文件的媒体组?

使用attach://file_name_in_post来发送

$media = [
    new \tsvetkov\telegram_bot\entities\inputMedia\InputMediaPhoto([
        'media' => 'attach://file-photo',
    ]),
    new \tsvetkov\telegram_bot\entities\inputMedia\InputMediaVideo([
        'media' => 'attach://file-video',
    ]),
];

别忘了将你的文件添加到请求中

$files = [
    'file-video' => "path_to_video_for_upload",
    'file-photo' => "path_to_photo_for_upload",
];

然后发送你的请求

$message = $bot->sendMediaGroup($chatId, $media, $files);