choccybiccy / telegrambot
构建更优秀的Telegram机器人的库
Requires
- guzzlehttp/guzzle: ^6.0
Requires (Dev)
- phpunit/phpunit: ^4.7
This package is auto-updated.
Last update: 2024-08-23 13:20:53 UTC
README
构建更优秀的Telegram机器人。
简介
这是一个PHP库,可以帮助您轻松构建自己的Telegram机器人及其命令。
安装
composer require choccybiccy/telegrambot
用法
创建命令
假设您的项目结构如下,您的 src/
文件夹被命名空间为 App
public/
index.php
src/
Controller/
Entity/
Telegram/
Command/
让我们创建一个名为 ExampleCommand.php
的文件,位于 src/Telegram/Command/
。这个命令将简单地回显发出命令的人传递给命令的任何参数。用户将发出以下命令
/example Hello World
现在让我们创建这个文件
<?php
namespace App;
use Choccybiccy\Telegram\Command;
use Choccybiccy\Telegram\Entity\Message;
class ExampleCommand extends Command
{
/**
* Execute the command
*
* @param string $argument The arguments passed to the command
* @param Message $message The message object issuing the command
*/
protected function execute($argument, Message $message)
{
}
}
如您所见,参数和Message对象被传递到execute($argument, Message $message)
方法中,这为我们提供了所需的所有信息,包括发出的命令、谁发出了命令以及在哪里发出的。
通过抽象Command
类中提供的\Choccybiccy\Telegram\ApiClient
,我们可以向私人或群聊发送消息。我们只需要私人或群聊的聊天ID。
<?php
namespace App;
use Choccybiccy\Telegram\Command;
use Choccybiccy\Telegram\Entity\Message;
class ExampleCommand extends Command
{
/**
* Execute the command
*
* @param string $argument The arguments passed to the command
* @param Message $message The message object issuing the command
*/
protected function execute($argument, Message $message)
{
$chat = $message->chat;
$chatId = $chat->id;
}
}
现在我们有了聊天ID,让我们向聊天发送一个简单的消息,这个消息就是我们的命令参数。在我们的例子中,用户发出的命令是/example Hello World
,所以我们的参数将是Hello World
。让我们发送消息。
<?php
namespace App;
use Choccybiccy\Telegram\ApiClient;
use Choccybiccy\Telegram\Command;
use Choccybiccy\Telegram\Entity\Message;
class ExampleCommand extends Command
{
/**
* Execute the command
*
* @param string $argument The arguments passed to the command
* @param Message $message The message object issuing the command
*/
protected function execute($argument, Message $message)
{
$chat = $message->chat;
$chatId = $chat->id;
/** @var ApiClient $client */
$client = $this->getApiClient();
/** @var Message $outgoingMessage */
$outgoingMessage = $client->sendMessage($chatId, $argument);
}
}
sendMessage
方法的返回值是我们刚刚发送的消息的Message
对象。
命令处理器
我们的所有命令都注册在CommandHandler
中。它负责将所有命令组合在一起,所以我们只需要提供一个Update
对象,然后让CommandHandler
决定执行哪个命令。
我们将使用非常简单且轻量级的Flight路由器作为构建我们的机器人web服务的快速示例。在我们的示例应用程序结构中,我们将设置我们的public/index.php
以添加我们的命令。
<?php
namespace App;
require_once "../vendor/autoload.php";
use Choccybiccy\Telegram\ApiClient;
use Choccybiccy\Telegram\CommandHandler;
use Flight;
use Symfony\Component\HttpFoundation\Request;
$apiClient = new ApiClient("<bot-authorisation-token>");
$request = Request::createFromGlobals();
Flight::route("/webhook", function () use ($request, $apiClient) {
$commands = new CommandHandler($apiClient);
});
我们的CommandHandler
已准备好注册命令。现在我们将添加我们的命令,并将命令触发器作为命令构造函数的一部分传递。在这个例子中,我们的命令将在有人输入/example
时触发,所以我们将example
传递给命令构造函数。然后,我们将从Telegram API发送到我们等待的webhook的请求体构造一个Telegram Update
对象。
<?php
namespace App;
require_once "../vendor/autoload.php";
use App\Telegram\Command\ExampleCommand;
use Choccybiccy\Telegram\ApiClient;
use Choccybiccy\Telegram\CommandHandler;
use Choccybiccy\Telegram\Entity\Update;
use Flight;
use Symfony\Component\HttpFoundation\Request;
$apiClient = new ApiClient("<bot-authorisation-token>");
$request = Request::createFromGlobals();
Flight::route("/webhook", function () use ($request, $apiClient) {
$commands = new CommandHandler($apiClient);
$commands->register(new ExampleCommand("example"));
/** @var Update $update */
$update = $apiClient->entityFromBody($request->getContent(), new Update());
$commands->run($update);
});
就这样。当Telegram API击中我们的webservice在/hook
时,我们将请求体转换为Update
对象,并将其传递给我们的CommandHandler
。处理器然后获取用户发送的消息文本,并将其与注册在其内的命令匹配。当它匹配到/example
时,我们的ExampleCommand
命令被执行。我们的示例命令简单地向私人或群聊回复了/example
命令的参数。
与Bot API交互
本库支持Telegram Bot API中概述的所有可用方法。这里有一些示例
sendMessage
$apiClient = new ApiClient("<authorisation token>");
$message = $apiClient->sendMessage($chatId, "My example message");
sendPhoto
$photo = new \Choccybiccy\Telegram\Entity\InputFile(file_get_contents("photo.jpg"));
$apiClient = new ApiClient("<authorisation token>");
$message = $apiClient->sendPhoto($chatId, $photo, "Cool photo, huh?");
进一步阅读
有关Telegram API的更多信息,请查看以下资源