dratejinn / telegrambot
一个Telegram机器人API
v2.0.0
2024-07-04 15:02 UTC
Requires
- php: ^7.4 || ^8.0
- ext-curl: *
- psr/log: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- monolog/monolog: ^3.0
- robmorgan/phinx: ^0.8.1
Suggests
- monolog/monolog: Useful for logging purposes
- robmorgan/phinx: Easy database migrations and an applicable migration exists for existing objects
README
用于创建Telegram机器人的PHP 7.0+ API。更多详细信息,请查看 Telegram机器人API
基本API
机器人API的基础可以在 src/API 中找到。结构尽可能地反映了Telegram API。
基本示例
所有可发送的对象都有一个 ->call(\Telegram\API\Bot $bot)
方法,可以用来将对象发送到Telegram API。一些示例可以在 Examples/Examples.php 中找到
发送聊天消息
<?php require_once 'vendor/autoload.php'; use Telegram\API\Bot; use Telegram\API\Method\SendMessage; $bot = new Bot('bottoken here'); $sendMessage = new SendMessage; $sendMessage->chatId = 1234566; $sendMessage->text = 'Hello world!'; $sendMessage->call($bot);
发送图片
<?php require_once 'vendor/autoload.php'; use Telegram\API\Bot; use Telegram\API\Method\SendPhoto; $bot = new Bot('bottoken here'); $sendPhoto = new SendPhoto; $sendPhoto->chatId = 1234566; $sendPhoto->photo = new InputFile('pathToFile'); $sendPhoto->call($bot);
创建机器人
创建机器人非常简单。Telegram接受两种类型的机器人。一种是通过getUpdates API调用持续请求更新的机器人,另一种是使用WebHooks。两种选项都可用。
一个简单的机器人 机器人
<?php declare(strict_types = 1); namespace Examples\Bot; use Telegram\Bot\ABot as TBot; class ExampleBot extends TBot { const TOKEN = ''; //place your token here (optionally) protected $_handlers = [ self::UPDATE_TYPE_MESSAGE => Handlers\MessageHandler::class ]; public function __construct(string $token = NULL) { if ($token === NULL) { $token = self::TOKEN; } parent::__construct($token); } }
消息处理器
<?php declare(strict_types = 1); namespace Examples\Bot\Handlers; use Telegram\Bot\Handler\AMessageHandler; class MessageHandler extends AMessageHandler { protected $_commandHandlers = [ Command\ExampleCommandHandler::class, ]; public function handleText(string $text) { $this->sendTextMessage('You typed: ' . $text); } }
命令处理器
<?php declare(strict_types = 1); namespace Examples\Bot\Handlers\Command; use Telegram\Bot\Handler\ACommandHandler; class ExampleCommandHandler extends ACommandHandler { public function handleCommand(string $command) { $this->sendTextMessage('You fired the command: ' . $command); $this->sendTextMessage('Provided with arguments: ' . implode(' ', $this->getArguments())); } public static function GetRespondsTo() : array { return [ 'test', 'example' ]; } }
通过持续轮询运行机器人
<?php declare(strict_types = 1); namespace Examples\Bot; use Telegram\API; use Telegram\API\ConfigManager; use Telegram\Storage\PDO\ConnectionDetails\ConnectionDetailsMySQL; use Telegram\Storage\PDO\MySQLHandler; require_once __DIR__ . '/../vendor/autoload.php'; if (file_exists(__DIR__ . '/userCreds.php')) { require_once __DIR__ . '/userCreds.php'; $userCreds = mysqlUserCreds(); } else { $userCreds = NULL; } define('DEVELOPMENT_MODE', TRUE); API\ConfigManager::AddConfigFromINIFile(__DIR__ . '/Log.ini', 'Log'); ConfigManager::AddConfigFromINIFile(__DIR__ . '/../Tokens.ini', 'token'); $tokens = ConfigManager::GetConfig('token'); $token = $tokens['devbot']['token']; $bot = new ExampleBot($token); if ($userCreds) { $connectionDetails = new ConnectionDetailsMySQL; $mysqlHandler = new MySQLHandler($connectionDetails, $userCreds, 'telegrambot'); $bot->setStorageHandler($mysqlHandler); } $bot->run();
使用WebHooks运行机器人
<?php use Examples\Bot\ExampleBot; use Telegram\API\Type\Update; require_once __DIR__ . '/../vendor/autoload.php'; $input = file_get_contents('php://input'); if ($input) { $bot = new ExampleBot('tokenHere'); $update = json_decode($input); $updateObj = new Update($update); $bot->handleUpdate($update); }