telegram-bot-api / for-bundle
Telegram Bot API 的 PHP 封装
Requires
- php: >=5.5.0
- ext-curl: *
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-22 23:19:44 UTC
README
一个无需依赖的扩展原生 PHP 封装,用于 Telegram Bot API。支持所有方法和响应类型。
机器人:开发者指南
机器人是专门为自动处理消息而设计的 Telegram 账户。用户可以通过在私聊或群聊中发送命令消息与机器人交互。
您可以通过向 bot API 发送 HTTPS 请求来控制您的机器人。
Bot API 是为希望为 Telegram 开发机器人的开发者创建的基于 HTTP 的接口。有关如何创建和设置机器人的信息,请参阅 机器人介绍 和 机器人常见问题解答。
安装
通过 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)。有关更多信息,请参阅 许可证文件。