alikm6/ php-telegram-bot
PHP Telegram Bot API
v1.4.74
2024-06-18 07:01 UTC
Requires
- php: ^7.4||^8.0
- ext-curl: *
- ext-json: *
- php-curl-class/php-curl-class: 9.*
README
这是一个简单的PHP包,它提供了一个易于使用的Telegram Bot API(https://core.telegram.org/bots)接口。它符合2024年5月28日的Telegram Bot API更新(版本7.4),并支持同时发送多个请求。
要求
要使用PHP Telegram Bot API,您需要以下内容:
- PHP 7.4或更高版本
- PHP中必须启用MultiCurl和exec
- Composer
安装
要安装此包,请使用Composer
composer require alikm6/php-telegram-bot:v1.4.74
如果在安装过程中遇到错误 它不符合您最低的稳定性要求
,请运行以下命令并再次尝试
composer config minimum-stability dev composer config prefer-stable true
使用方法
要使用此包,您首先需要创建一个Telegram对象,如下所示:
$tg = new TelegramBot\Telegram('your-bot-token');
解析更新
如果您为您的机器人设置了webhook,您可以使用以下代码以数组的形式接收来自Telegram发送到服务器的信息:
$update = $tg->parseUpdate();
为了更高的安全性,如果您只想处理来自Telegram服务器的请求并忽略其他请求,您可以在webhook文件的开始处使用以下代码。
TelegramBot\Telegram::limit_access_to_telegram_only();
在这种情况下,如果请求未从Telegram服务器发送,PHP脚本将停止,请求将不会被处理。
使用方法
您可以使用Telegram Bot API的任何方法,例如:
$m = $tg->sendMessage([ 'chat_id' => 1233456, 'text' => "Hello World" ]);
有关可用方法的更多信息,请参阅https://core.telegram.org/bots/api。
每个方法都可以接收一个可选的API请求选项数组
$options = [ 'send_error' => true, // If set to true, errors will be sent to the specified chat ID as a Telegram message. 'run_in_background' => false, // If set to true, requests will be processed in the background and responses will not be returned to the caller. 'return' => 'result_array', // The type of result to return from API requests. ];
return
选项可以有以下值:
result_array
:Telegram API响应的'result'键将以关联数组的形式返回。result_object
:Telegram API响应的'result'键将作为对象返回。response
:从Telegram API返回的原始响应将作为字符串返回。response_array
:从Telegram API返回的原始响应将作为关联数组返回。response_object
:从Telegram API返回的原始响应将作为对象返回。
如果没有设置$options
,则将应用默认选项。
同时发送请求
要同时发送多个请求,可以将请求数组传递给方法
$ms = $tg->sendMessage([ [ 'chat_id' => 1233456, 'text' => "Message 1" ], [ 'chat_id' => 654321, 'text' => "Message 2" ] ]);
许可证
此包根据MIT许可证授权。有关详细信息,请参阅LICENSE文件。