mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-24 00:55:35 +08:00
add match reply mode
This commit is contained in:
@@ -31,6 +31,11 @@ const ZM_ERR_METHOD_NOT_FOUND = 1; // 找不到方法
|
|||||||
const ZM_ERR_ROUTE_NOT_FOUND = 2; // 找不到路由
|
const ZM_ERR_ROUTE_NOT_FOUND = 2; // 找不到路由
|
||||||
const ZM_ERR_ROUTE_METHOD_NOT_ALLOWED = 3; // 路由方法不允许
|
const ZM_ERR_ROUTE_METHOD_NOT_ALLOWED = 3; // 路由方法不允许
|
||||||
|
|
||||||
|
/** 定义 BotContext 下 reply 回复的模式 */
|
||||||
|
const ZM_REPLY_NONE = 0; // 默认回复,不带任何东西
|
||||||
|
const ZM_REPLY_MENTION = 1; // 回复时 @ 该用户
|
||||||
|
const ZM_REPLY_QUOTE = 2; // 回复时引用该消息
|
||||||
|
|
||||||
const LOAD_MODE_VENDOR = 0; // 从 vendor 加载
|
const LOAD_MODE_VENDOR = 0; // 从 vendor 加载
|
||||||
const LOAD_MODE_SRC = 1; // 从 src 加载
|
const LOAD_MODE_SRC = 1; // 从 src 加载
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace ZM\Context;
|
namespace ZM\Context;
|
||||||
|
|
||||||
|
use DI\DependencyException;
|
||||||
|
use DI\NotFoundException;
|
||||||
use OneBot\Driver\Event\Http\HttpRequestEvent;
|
use OneBot\Driver\Event\Http\HttpRequestEvent;
|
||||||
use OneBot\Driver\Event\WebSocket\WebSocketMessageEvent;
|
use OneBot\Driver\Event\WebSocket\WebSocketMessageEvent;
|
||||||
use OneBot\V12\Object\Action;
|
|
||||||
use OneBot\V12\Object\MessageSegment;
|
use OneBot\V12\Object\MessageSegment;
|
||||||
use OneBot\V12\Object\OneBotEvent;
|
use OneBot\V12\Object\OneBotEvent;
|
||||||
use ZM\Context\Trait\BotActionTrait;
|
use ZM\Context\Trait\BotActionTrait;
|
||||||
@@ -17,14 +18,16 @@ class BotContext implements ContextInterface
|
|||||||
{
|
{
|
||||||
use BotActionTrait;
|
use BotActionTrait;
|
||||||
|
|
||||||
|
/** @var array<string, array<string, BotContext>> 记录机器人的上下文列表 */
|
||||||
private static array $bots = [];
|
private static array $bots = [];
|
||||||
|
|
||||||
private static array $echo_id_list = [];
|
/** @var string[] 记录当前上下文绑定的机器人 */
|
||||||
|
|
||||||
private array $self;
|
private array $self;
|
||||||
|
|
||||||
|
/** @var array 如果是 BotCommand 匹配的上下文,这里会存放匹配到的参数 */
|
||||||
private array $params = [];
|
private array $params = [];
|
||||||
|
|
||||||
|
/** @var bool 用于标记当前上下文会话是否已经调用过 reply() 方法 */
|
||||||
private bool $replied = false;
|
private bool $replied = false;
|
||||||
|
|
||||||
public function __construct(string $bot_id, string $platform, null|WebSocketMessageEvent|HttpRequestEvent $event = null)
|
public function __construct(string $bot_id, string $platform, null|WebSocketMessageEvent|HttpRequestEvent $event = null)
|
||||||
@@ -34,6 +37,12 @@ class BotContext implements ContextInterface
|
|||||||
$this->base_event = $event;
|
$this->base_event = $event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取机器人事件对象
|
||||||
|
*
|
||||||
|
* @throws DependencyException
|
||||||
|
* @throws NotFoundException
|
||||||
|
*/
|
||||||
public function getEvent(): OneBotEvent
|
public function getEvent(): OneBotEvent
|
||||||
{
|
{
|
||||||
return container()->get('bot.event');
|
return container()->get('bot.event');
|
||||||
@@ -42,9 +51,10 @@ class BotContext implements ContextInterface
|
|||||||
/**
|
/**
|
||||||
* 快速回复机器人消息文本
|
* 快速回复机器人消息文本
|
||||||
*
|
*
|
||||||
* @param array|MessageSegment|string|\Stringable $message 消息内容、消息段或消息段数组
|
* @param array|MessageSegment|string|\Stringable $message 消息内容、消息段或消息段数组
|
||||||
|
* @param int $reply_mode 回复消息模式,默认为空,可选 ZM_REPLY_MENTION(at 用户)、ZM_REPLY_QUOTE(引用消息)
|
||||||
*/
|
*/
|
||||||
public function reply(\Stringable|MessageSegment|array|string $message)
|
public function reply(\Stringable|MessageSegment|array|string $message, int $reply_mode = ZM_REPLY_NONE)
|
||||||
{
|
{
|
||||||
if (container()->has('bot.event')) {
|
if (container()->has('bot.event')) {
|
||||||
// 这里直接使用当前上下文的事件里面的参数,不再重新挨个获取怎么发消息的参数
|
// 这里直接使用当前上下文的事件里面的参数,不再重新挨个获取怎么发消息的参数
|
||||||
@@ -52,11 +62,13 @@ class BotContext implements ContextInterface
|
|||||||
$event = container()->get('bot.event');
|
$event = container()->get('bot.event');
|
||||||
|
|
||||||
// reply 的条件是必须 type=message
|
// reply 的条件是必须 type=message
|
||||||
if ($event->getType() !== 'message') {
|
if ($event->type !== 'message') {
|
||||||
throw new OneBot12Exception('bot()->reply() can only be used in message event.');
|
throw new OneBot12Exception('bot()->reply() can only be used in message event.');
|
||||||
}
|
}
|
||||||
$msg = (is_string($message) ? [new MessageSegment('text', ['text' => $message])] : ($message instanceof MessageSegment ? [$message] : $message));
|
$msg = (is_string($message) ? [new MessageSegment('text', ['text' => $message])] : ($message instanceof MessageSegment ? [$message] : $message));
|
||||||
$this->replied = true;
|
$this->replied = true;
|
||||||
|
// 判断规则
|
||||||
|
$this->matchReplyMode($reply_mode, $msg, $event);
|
||||||
return $this->sendMessage($msg, $event->detail_type, $event->jsonSerialize());
|
return $this->sendMessage($msg, $event->detail_type, $event->jsonSerialize());
|
||||||
}
|
}
|
||||||
throw new OneBot12Exception('bot()->reply() can only be used in message event.');
|
throw new OneBot12Exception('bot()->reply() can only be used in message event.');
|
||||||
@@ -126,13 +138,25 @@ class BotContext implements ContextInterface
|
|||||||
return $this->params;
|
return $this->params;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getEchoAction(mixed $echo): ?Action
|
|
||||||
{
|
|
||||||
return self::$echo_id_list[$echo] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getSelf(): array
|
public function getSelf(): array
|
||||||
{
|
{
|
||||||
return $this->self;
|
return $this->self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 匹配更改 reply 的特殊模式
|
||||||
|
*
|
||||||
|
* @param int $reply_mode 回复模式
|
||||||
|
* @param array $message_segments 消息段的引用
|
||||||
|
* @param OneBotEvent $event 事件对象
|
||||||
|
*/
|
||||||
|
private function matchReplyMode(int $reply_mode, array &$message_segments, OneBotEvent $event)
|
||||||
|
{
|
||||||
|
if (($reply_mode & ZM_REPLY_QUOTE) === ZM_REPLY_QUOTE) {
|
||||||
|
array_unshift($message_segments, new MessageSegment('reply', ['message_id' => $event->getMessageId(), 'user_id' => $event->getUserId()]));
|
||||||
|
}
|
||||||
|
if (($reply_mode & ZM_REPLY_MENTION) === ZM_REPLY_MENTION) {
|
||||||
|
array_unshift($message_segments, new MessageSegment('mention', ['user_id' => $event->getUserId()]));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user