Bale Bot API 的 PHP 封装器

dev-main 2024-05-28 11:56 UTC

This package is auto-updated.

Last update: 2024-09-28 12:48:31 UTC


README

Latest Version on Packagist Software License Total Downloads

一个扩展的本地 PHP 封装器,用于无需要求的 Telegram Bot API,支持所有方法和响应类型。

机器人:开发者介绍

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

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

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

安装

通过 Composer

$ composer require telegram-bot/api

用法

请参阅示例 DevAnswerBot(俄语)。

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');
    // or initialize with botan.io tracker api key
    // $bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY');
    

    //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();
}

Botan SDK(不再支持)

Botan 是一个基于 Yandex.Appmetrica 的 Telegram 机器人分析系统。在此文档中,您可以找到如何设置 Yandex.Appmetrica 账户以及 Botan SDK 的使用示例。

创建账户

  • http://appmetrica.yandex.com/ 注册
  • 注册后,您将提示创建应用程序。请使用 @YourBotName 作为名称。
  • 从设置页面保存 API 密钥,您将使用它作为 Botan API 调用的令牌。
  • 下载您的语言的库,并按以下说明使用。不要忘记插入您的令牌!

由于我们刚刚开始,您可能会发现一些现有的 AppMetriсa 报告无法正确适用于 Telegram 机器人,例如地理位置、性别、年龄、库、设备、流量来源和网络部分。我们将在以后完善这些。

SDK 使用

独立

$tracker = new \TelegramBot\Api\Botan('YOUR_BOTAN_TRACKER_API_KEY');

$tracker->track($message, $eventName);

API 封装器

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

$bot->track($message, $eventName);

您可以使用 'getUpdates()' 方法,所有传入的消息都将自动跟踪为 Message-事件。

客户端

$bot = new \TelegramBot\Api\Client('YOUR_BOT_API_TOKEN', 'YOUR_BOTAN_TRACKER_API_KEY');

所有注册的命令都将自动跟踪为命令名称

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

测试

$ composer test

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

如果您发现任何安全相关的问题,请通过电子邮件 mail@igusev.ru 而不是使用问题跟踪器。

鸣谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件