no message

This commit is contained in:
kuaifan 2025-07-29 16:22:37 +08:00
parent cdc27004bf
commit fe9d23a0ff
3 changed files with 47 additions and 34 deletions

View File

@ -12,7 +12,6 @@ use App\Tasks\PushTask;
use App\Exceptions\ApiException;
use App\Observers\ProjectTaskObserver;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use League\HTMLToMarkdown\HtmlConverter;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
@ -1958,8 +1957,7 @@ class ProjectTask extends AbstractModel
if ($this->content) {
$taskDesc = $this->content?->getContentInfo();
if ($taskDesc) {
$converter = new HtmlConverter(['strip_tags' => true]);
$descContent = Base::cutStr($converter->convert($taskDesc['content']), 2000);
$descContent = Base::cutStr(Base::html2markdown($taskDesc['content'], ['strip_tags' => true]), 2000);
$contexts[] = <<<EOF
任务描述:
```md

View File

@ -3049,12 +3049,13 @@ class Base
/**
* html MD(markdown)
* @param $html
* @param array $options
* @return mixed|string
*/
public static function html2markdown($html)
public static function html2markdown($html, $options = [])
{
try {
$converter = new HtmlConverter();
$converter = new HtmlConverter($options);
return $converter->convert($html);
} catch (\Exception) {
return $html;

View File

@ -599,21 +599,25 @@ class BotReceiveMsgTask extends AbstractTask
*/
private function extractMessageContent(WebSocketDialogMsg $msg)
{
$result = '';
$reserves = [];
switch ($msg->type) {
case "file":
// 提取文件消息
$msgData = Base::json2array($msg->getRawOriginal('msg'));
return $this->convertMentionFormat("path", $msgData['path'], $msgData['name']);
$result = $this->convertMentionFormat("path", $msgData['path'], $msgData['name'], $reserves);
break;
case "text":
// 提取文本消息
$original = $msg->msg['text'] ?: '';
if (empty($original)) {
$result = $msg->msg['text'] ?: '';
if (empty($result)) {
return '';
}
// 提取快捷键
if (preg_match("/<span[^>]*?data-quick-key=([\"'])([^\"']+?)\\1[^>]*?>(.*?)<\/span>/is", $original, $match)) {
if (preg_match("/<span[^>]*?data-quick-key=([\"'])([^\"']+?)\\1[^>]*?>(.*?)<\/span>/is", $result, $match)) {
$command = $match[2] ?? '';
$command = preg_replace("/^%3A\.?/", ":", $command);
$command = trim($command);
@ -623,41 +627,49 @@ class BotReceiveMsgTask extends AbstractTask
}
// 提及任务、文件、报告
$original = preg_replace_callback_array([
$result = preg_replace_callback_array([
// 用户
"/<span class=\"mention user\" data-id=\"(\d+)\">(.*?)<\/span>/" => function () {
return "";
},
// 任务
"/<span class=\"mention task\" data-id=\"(\d+)\">(.*?)<\/span>/" => function ($match) {
return $this->convertMentionFormat("task", $match[1], $match[2]);
"/<span class=\"mention task\" data-id=\"(\d+)\">#?(.*?)<\/span>/" => function ($match) use (&$reserves) {
return $this->convertMentionFormat("task", $match[1], $match[2], $reserves);
},
// 文件
"/<a class=\"mention file\" href=\"([^\"']+?)\"[^>]*?>(.*?)<\/a>/" => function ($match) {
"/<a class=\"mention file\" href=\"([^\"']+?)\"[^>]*?>~?(.*?)<\/a>/" => function ($match) use (&$reserves) {
if (preg_match("/single\/file\/(.*?)$/", $match[1], $subMatch)) {
return $this->convertMentionFormat("file", $subMatch[1], $match[2]);
return $this->convertMentionFormat("file", $subMatch[1], $match[2], $reserves);
}
return "";
},
// 报告
"/<a class=\"mention report\" href=\"([^\"']+?)\"[^>]*?>(.*?)<\/a>/" => function ($match) {
"/<a class=\"mention report\" href=\"([^\"']+?)\"[^>]*?>%?(.*?)<\/a>/" => function ($match) use (&$reserves) {
if (preg_match("/single\/report\/detail\/(.*?)$/", $match[1], $subMatch)) {
return $this->convertMentionFormat("report", $subMatch[1], $match[2]);
return $this->convertMentionFormat("report", $subMatch[1], $match[2], $reserves);
}
return "";
},
], $original);
], $result);
// 转成 markdown 并返回
return Base::html2markdown($original);
// 转成 markdown
$result = Base::html2markdown($result);
break;
default:
// 其他类型消息不处理
return '';
}
// 处理 reserves
foreach ($reserves as $rand => $mention) {
$result = str_replace($rand, $mention, $result);
}
return $result;
}
/**
@ -669,11 +681,13 @@ class BotReceiveMsgTask extends AbstractTask
* @param string $name 提及对象的显示名称
* @return string 格式化后的提及字符串
*/
private function convertMentionFormat($type, $key, $name)
private function convertMentionFormat($type, $key, $name, &$reserves)
{
$key = preg_replace('/[#\[\]]/', '', $key);
$name = preg_replace('/[#\[\]]/', '', $name);
return "[{$type}#{$key}#{$name}]";
$key = str_replace(['#', '-->'], '', $key);
$name = str_replace(['#', '-->'], '', $name);
$rand = Base::generatePassword(12);
$reserves[$rand] = "<!--{$type}#{$key}#{$name}-->";
return $rand;
}
/**
@ -687,7 +701,7 @@ class BotReceiveMsgTask extends AbstractTask
private function convertMentionForAI($original)
{
$array = [];
$original = preg_replace_callback('/\[([^#\[\]]+)#([^#\[\]]+)#([^#\[\]]*)\]/', function ($match) use (&$array) {
$original = preg_replace_callback('/<!--(.*?)#(.*?)#(.*?)-->/', function ($match) use (&$array) {
// 初始化 tag 内容
$pathTag = null;
$pathName = null;
@ -748,7 +762,7 @@ class BotReceiveMsgTask extends AbstractTask
}
$pathTag = "report_content";
$pathName = addslashes($match[3]) . " (ID:{$reportInfo->id})";
$pathContent = $reportInfo->content;
$pathContent = Base::html2markdown($reportInfo->content);
break;
}