phenogram/scraper

用于抓取Telegram机器人API文档页面并将其转换为PHP类的实用程序。

1.0.0 2024-09-01 11:54 UTC

This package is auto-updated.

Last update: 2024-09-22 13:27:54 UTC


README

PHP二进制文件,用于抓取Telegram机器人API文档页面并将其转换为PHP类,以在Phenogram底层绑定中使用

分支动机

推广特性

主要原因是我喜欢PHP 8的推广特性,因此在这个仓库中,占位符生成方式如下。

旧版

class BotCommand implements TypeInterface
{
    /** @var string Text of the command; 1-32 characters. Can contain only lowercase English letters, digits and underscores. */
    public string $command;

    /** @var string Description of the command; 1-256 characters. */
    public string $description;
}

新版本

/**
 * This object represents a bot command.
 */
class BotCommand implements TypeInterface
{
    /**
     * @param string $command     Text of the command; 1-32 characters. Can contain only lowercase English letters, digits and underscores.
     * @param string $description description of the command; 1-256 characters
     */
    public function __construct(
        public string $command,
        public string $description,
    ) {
    }
}

将API类型从使用抽象方法的Trait更改为使用新客户端接口的类

特质很好,但我认为在这种情况下使用它并不合适。我认为我的方法提供了更多的灵活性。

旧版

...
Trait API
{
    abstract public function sendRequest(string $method, array $args): mixed;
...

新版本

// TelegramBotApi.php
// ...
class TelegramBotApi
{
	public function __construct(
		protected TelegramBotApiClientInterface $client,
	) {
	}
// TelegramBotApiClientInterface.php
// ...
interface TelegramBotApiClientInterface
{
	function sendRequest(string $method, array $args): mixed;
}

杂项

  1. 更改类/文件名
  2. 为类型类添加注释
  3. 按必需到可选的顺序排序类参数
  4. 代码风格

安装

使用composer安装库

  $ composer require phenogram/scraper

从命令行使用

安装后,您可以使用CLI与库交互。

对于基本帮助和命令列表

  $ vendor/bin/tgscraper help

JSON

以人类可读的JSON提取最新架构

  $ vendor/bin/tgscraper app:export-schema --readable botapi.json

或者,如果您想要Postman兼容的JSON(感谢davtur19

  $ vendor/bin/tgscraper app:export-schema --postman botapi_postman.json

YAML

以YAML格式提取最新架构

  $ vendor/bin/tgscraper app:export-schema --yaml botapi.yaml

OpenAPI

以JSON格式提取最新的OpenAPI架构

  $ vendor/bin/tgscraper app:export-schema --openapi botapi_openapi.json

或者,如果您更喜欢YAML

  $ vendor/bin/tgscraper app:export-schema --openapi --yaml botapi_openapi.yaml

占位符

注意:由于Telegram可能会随时更改页面格式,因此**不要**依赖此库自动生成的占位符,**始终**检查代码!

TGScraper还可以生成您可以在库中使用的类占位符。一个示例实现可以在Sysbot Telegram模块中找到。

使用Phenogram\Telegram作为命名空间前缀,在out/目录中创建占位符

  $ vendor/bin/tgscraper app:create-stubs --namespace-prefix "Phenogram\Telegram" out

所有版本

如果您想为每个机器人API版本生成所有架构和占位符,可以做到!

以下是如何将所有内容导出到out/目录的示例,架构以人类可读格式显示,并将占位符的命名空间前缀设置为Phenogram\Telegram

  $ vendor/bin/tgscraper app:dump-schemas -r --namespace-prefix "Phenogram\Telegram" out

自定义格式

如果您对TGScraper生成的自定义格式感兴趣,您可以在这里找到其架构。