mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-20 23:25:35 +08:00
@@ -9,8 +9,6 @@ use OneBot\Driver\Event\WebSocket\WebSocketMessageEvent;
|
||||
use OneBot\V12\Object\Action;
|
||||
use OneBot\V12\Object\MessageSegment;
|
||||
use OneBot\V12\Object\OneBotEvent;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use ZM\Context\Trait\BotActionTrait;
|
||||
use ZM\Exception\OneBot12Exception;
|
||||
use ZM\Utils\MessageUtil;
|
||||
@@ -19,6 +17,8 @@ class BotContext implements ContextInterface
|
||||
{
|
||||
use BotActionTrait;
|
||||
|
||||
private static array $bots = [];
|
||||
|
||||
private static array $echo_id_list = [];
|
||||
|
||||
private array $self;
|
||||
@@ -30,6 +30,7 @@ class BotContext implements ContextInterface
|
||||
public function __construct(string $bot_id, string $platform, null|WebSocketMessageEvent|HttpRequestEvent $event = null)
|
||||
{
|
||||
$this->self = ['user_id' => $bot_id, 'platform' => $platform];
|
||||
self::$bots[$bot_id][$platform] = $this;
|
||||
$this->base_event = $event;
|
||||
}
|
||||
|
||||
@@ -41,11 +42,7 @@ class BotContext implements ContextInterface
|
||||
/**
|
||||
* 快速回复机器人消息文本
|
||||
*
|
||||
* @param array|MessageSegment|string|\Stringable $message 消息内容、消息段或消息段数组
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws OneBot12Exception
|
||||
* @throws \Throwable
|
||||
* @param array|MessageSegment|string|\Stringable $message 消息内容、消息段或消息段数组
|
||||
*/
|
||||
public function reply(\Stringable|MessageSegment|array|string $message)
|
||||
{
|
||||
@@ -76,13 +73,24 @@ class BotContext implements ContextInterface
|
||||
/**
|
||||
* 获取其他机器人的上下文操作对象
|
||||
*
|
||||
* @param string $bot_id 机器人的 self.user_id 对应的 ID
|
||||
* @param string $platform 机器人的 self.platform 对应的 platform
|
||||
* @return $this
|
||||
* @param string $bot_id 机器人的 self.user_id 对应的 ID
|
||||
* @param string $platform 机器人的 self.platform 对应的 platform
|
||||
* @throws OneBot12Exception
|
||||
*/
|
||||
public function getBot(string $bot_id, string $platform = ''): BotContext
|
||||
{
|
||||
return $this;
|
||||
if (isset(self::$bots[$bot_id])) {
|
||||
if ($platform === '') {
|
||||
$one = current(self::$bots[$bot_id]);
|
||||
if ($one instanceof BotContext) {
|
||||
return $one;
|
||||
}
|
||||
} elseif (isset(self::$bots[$bot_id][$platform])) {
|
||||
return self::$bots[$bot_id][$platform];
|
||||
}
|
||||
}
|
||||
// 到这里说明没找到对应的机器人,抛出异常
|
||||
throw new OneBot12Exception('Bot not found.');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,4 +130,9 @@ class BotContext implements ContextInterface
|
||||
{
|
||||
return self::$echo_id_list[$echo] ?? null;
|
||||
}
|
||||
|
||||
public function getSelf(): array
|
||||
{
|
||||
return $this->self;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,23 +45,23 @@ class HttpEventListener
|
||||
$result = HttpUtil::parseUri($event->getRequest(), $node, $params);
|
||||
switch ($result) {
|
||||
case ZM_ERR_NONE: // 解析到存在路由了
|
||||
$handler = new AnnotationHandler(Route::class);
|
||||
$route_handler = new AnnotationHandler(Route::class);
|
||||
$div = new Route($node['route']);
|
||||
$div->params = $params;
|
||||
$div->method = $node['method'];
|
||||
// TODO:这里有个bug,逻辑上 request_method 应该是个数组,而不是字符串,但是这里 $node['method'] 是字符串,所以这里只能用字符串来判断
|
||||
// $div->request_method = $node['request_method'];
|
||||
$div->class = $node['class'];
|
||||
$starttime = microtime(true);
|
||||
$handler->handle($div, null, $params, $event->getRequest(), $event);
|
||||
if (is_string($val = $handler->getReturnVal()) || ($val instanceof \Stringable)) {
|
||||
$route_handler->handle($div, null, $params, $event->getRequest(), $event);
|
||||
if (is_string($val = $route_handler->getReturnVal()) || ($val instanceof \Stringable)) {
|
||||
// 返回的内容是可以被字符串化的,就当作 Body 来返回,状态码 200
|
||||
$event->withResponse(HttpFactory::createResponse(200, null, [], Stream::create($val)));
|
||||
} elseif ($event->getResponse() === null) {
|
||||
// 过了一遍 Route,没有促成 Response,则返回 500(路由必须有返回才行)
|
||||
$event->withResponse(HttpFactory::createResponse(500));
|
||||
}
|
||||
logger()->warning('Used ' . round((microtime(true) - $starttime) * 1000, 3) . ' ms');
|
||||
break;
|
||||
case ZM_ERR_ROUTE_METHOD_NOT_ALLOWED:
|
||||
case ZM_ERR_ROUTE_METHOD_NOT_ALLOWED: // 路由检测到存在,但是方法不匹配,则返回 405,表示方法不受支持
|
||||
$event->withResponse(HttpUtil::handleHttpCodePage(405));
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user