Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxyw committed Sep 3, 2022
1 parent 0024596 commit 88e0d4b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
21 changes: 20 additions & 1 deletion src/OneBot/V12/Object/Event/OneBotEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ abstract class OneBotEvent implements JsonSerializable, IteratorAggregate
/** @var array 扩展数据 */
private array $extended_data = [];

/** @var string 扩展字段前缀 */
private string $extended_prefix = '';

/**
* @param string $type 事件类型
* @param string $detail_type 事件详细类型
Expand Down Expand Up @@ -138,6 +141,18 @@ public function unsetExtendedDatum(string $key): self
return $this;
}

/**
* 设置扩展字段前缀
*
* @param string $prefix 前缀,可以是机器人平台名称、平台名称缩写、实现名称、实现名称缩写或其他自定义字符串,格式必须符合 `[_a-z]+`
* @return $this
*/
public function setExtendedPrefix(string $prefix): self
{
$this->extended_prefix = $prefix;
return $this;
}

public function jsonSerialize(): array
{
$data = [];
Expand All @@ -147,7 +162,11 @@ public function jsonSerialize(): array
}
$data[$k] = $v;
if ($k === 'detail_type') {
$data[$k] = empty($this->extended_data) ? $this->detail_type : "{$this->impl}.{$this->detail_type}";
if (empty($this->extended_prefix) || empty($this->getExtendedData())) {
$data[$k] = $this->detail_type;
} else {
$data[$k] = "{$this->extended_prefix}.{$this->detail_type}";
}
}
}
return $data;
Expand Down
31 changes: 23 additions & 8 deletions src/OneBot/V12/OneBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OneBot\V12;

use InvalidArgumentException;
use OneBot\Driver\Driver;
use OneBot\Driver\Event\DriverInitEvent;
use OneBot\Driver\Event\Http\HttpRequestEvent;
Expand Down Expand Up @@ -36,25 +37,25 @@ class OneBot
use Singleton;

/** @var ConfigInterface 配置实例 */
private $config;
private ConfigInterface $config;

/** @var string 实现名称 */
private $implement_name;
private string $implement_name;

/** @var string 实现平台 */
private $platform;
private string $platform;

/** @var string 机器人 ID */
private $self_id;
private string $self_id;

/** @var Driver 驱动实例 */
private $driver;
private Driver $driver;

/** @var null|ActionHandlerBase 动作处理器 */
private $base_action_handler;
private ?ActionHandlerBase $base_action_handler;

/** @var array 动作处理回调们 */
private $action_handlers = [];
private array $action_handlers = [];

/**
* 创建一个 OneBot 实例
Expand All @@ -65,6 +66,8 @@ public function __construct(ConfigInterface $config)
throw new RuntimeException('只能有一个OneBot实例!');
}

$this->validateConfig($config);

$this->config = $config;
$this->implement_name = $config->get('name');
$this->self_id = $config->get('self_id');
Expand Down Expand Up @@ -246,7 +249,8 @@ public function dispatchEvent(OneBotEvent $event): void
}
$socket->post(json_encode($event->jsonSerialize()), $this->getRequestHeaders(), function (ResponseInterface $response) {
// TODO:编写 HTTP Webhook 响应的处理逻辑
}, function (RequestInterface $request) {});
}, function (RequestInterface $request) {
});
}
$frame_str = FrameFactory::createTextFrame(json_encode($event->jsonSerialize())); // 创建文本帧
foreach ($this->driver->getWSServerSockets() as $socket) {
Expand Down Expand Up @@ -306,4 +310,15 @@ protected function addOneBotEvent()
break;
}
}

protected function validateConfig(ConfigInterface $config): void
{
if (!preg_match('/[a-z][\-a-z0-9]*(\.[\-a-z0-9]+)*/', $config->get('platform'))) {
throw new InvalidArgumentException('配置的平台名称不合法,请参阅文档');
}

if (!preg_match('/[a-z][\-a-z0-9]*(\.[\-a-z0-9]+)*/', $config->get('name'))) {
throw new InvalidArgumentException('配置的实现名称不合法,请参阅文档');
}
}
}

0 comments on commit 88e0d4b

Please sign in to comment.