opensourcewebsite-org/php-telegram-sdk

PHP语言的Telegram Bot API库

v2.3.22 2021-03-09 07:49 UTC

README

License PHP Version

机器人:开发者介绍

机器人是特殊Telegram账户,用于自动处理消息。用户可以通过在私聊或群聊中发送命令消息与机器人交互。

您可以通过向 Telegram Bot API 发送HTTPS请求来控制您的机器人。

Bot API是一个基于HTTP的接口,专为希望为Telegram构建机器人的开发者创建。要了解如何创建和设置机器人,请参阅 机器人介绍机器人常见问题解答

适用于 Telegram Bot API 的PHP Telegram SDK。

此库正在积极开发中,应被视为测试版质量。请确保您已在测试网络上进行了广泛的测试,并在您的代码的其他地方添加了合理性检查。

此存储库是 OpenSourceWebsite 组织 的一部分。本项目及其所有参与者均受 行为准则 的约束。

入门

查看发行说明以了解重大更改.

请阅读 Telegram Bot API 文档

贡献

请阅读我们的 贡献指南

安装

安装此扩展的首选方法是使用 composer

运行以下命令:

composer require opensourcewebsite-org/php-telegram-sdk

"opensourcewebsite-org/php-telegram-sdk": "*"

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

使用方法

API包装器

发送消息

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$bot->sendMessage($chatId, $messageText);

发送文档

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$document = new \CURLFile('document.txt');

$bot->sendDocument($chatId, $document);

发送带回复键盘的消息

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$keyboard = new \TelegramBot\Api\Types\ReplyKeyboardMarkup(array(array("one", "two", "three")), true); // true for one-time keyboard

$bot->sendMessage($chatId, $messageText, null, false, null, $keyboard);

发送带内联键盘的消息

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$keyboard = new \TelegramBot\Api\Types\Inline\InlineKeyboardMarkup(
            [
                [
                    ['text' => 'link', 'url' => 'https://core.telegram.org']
                ]
            ]
        );

$bot->sendMessage($chatId, $messageText, null, false, null, $keyboard);

发送媒体组

$bot = new \TelegramBot\Api\BotApi('YOUR_BOT_API_TOKEN');

$media = new \TelegramBot\Api\Types\InputMedia\ArrayOfInputMedia();
$media->addItem(new TelegramBot\Api\Types\InputMedia\InputMediaPhoto('https://avatars3.githubusercontent.com/u/9335727'));
$media->addItem(new TelegramBot\Api\Types\InputMedia\InputMediaPhoto('https://avatars3.githubusercontent.com/u/9335727'));
// Same for video
// $media->addItem(new TelegramBot\Api\Types\InputMedia\InputMediaVideo('http://clips.vorwaerts-gmbh.de/VfE_html5.mp4'));
$bot->sendMediaGroup($chatId, $media);

客户端

require_once "vendor/autoload.php";

try {
    $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN');

    //Handle /ping command
    $bot->command('ping', function ($message) use ($bot) {
        $bot->sendMessage($message->getChat()->getId(), 'pong!');
    });

    //Handle text messages
    $bot->on(function (\TelegramBot\Api\Types\Update $update) use ($bot) {
        $message = $update->getMessage();
        $id = $message->getChat()->getId();
        $bot->sendMessage($id, 'Your message: ' . $message->getText());
    }, function () {
        return true;
    });

    $bot->run();

} catch (\TelegramBot\Api\Exception $e) {
    $e->getMessage();
}

测试

$ composer test

反馈

要请求新功能、提交错误报告、给我们反馈、开始设计讨论或提出改进此代码的想法,请随时 打开一个问题创建一个拉取请求

请将所有安全问题发送到 security@opensourcewebsite.org

许可证

本项目是开源的,并免费提供,遵循 MIT许可证