|
@@ -27,11 +27,23 @@ class CozeServices
|
|
|
|
|
|
|
|
protected $botId;
|
|
protected $botId;
|
|
|
|
|
|
|
|
|
|
+ protected $timeout;
|
|
|
|
|
+
|
|
|
|
|
+ protected $fallbackReply;
|
|
|
|
|
+
|
|
|
public function __construct()
|
|
public function __construct()
|
|
|
{
|
|
{
|
|
|
- $this->apiBase = rtrim((string)$this->env('coze.api_base', 'COZE_API_BASE', 'https://api.coze.cn'), '/');
|
|
|
|
|
- $this->apiToken = (string)$this->env('coze.api_token', 'COZE_API_TOKEN', '');
|
|
|
|
|
- $this->botId = (string)$this->env('coze.bot_id', 'COZE_BOT_ID', '7670455976805826612');
|
|
|
|
|
|
|
+ $this->apiBase = rtrim((string)$this->env(['coze.coze_api_base', 'coze.api_base'], 'COZE_API_BASE', 'https://api.coze.cn'), '/');
|
|
|
|
|
+ $this->apiToken = (string)$this->env(['coze.coze_access_token', 'coze.access_token', 'coze.api_token'], 'COZE_ACCESS_TOKEN', '');
|
|
|
|
|
+ $this->botId = (string)$this->env(['coze.coze_bot_id', 'coze.bot_id'], 'COZE_BOT_ID', '');
|
|
|
|
|
+ $this->timeout = max(1, (int)$this->env(['coze.coze_timeout', 'coze.timeout'], 'COZE_TIMEOUT', '15'));
|
|
|
|
|
+ $this->fallbackReply = trim((string)$this->env(['coze.coze_fallback_reply', 'coze.fallback_reply'], 'COZE_FALLBACK_REPLY', ''));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function isEnabled(): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ $value = Env::get('coze.coze_enable', Env::get('coze.enable', getenv('COZE_ENABLE') ?: false));
|
|
|
|
|
+ return filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -40,7 +52,7 @@ class CozeServices
|
|
|
public function chatText(string $userId, string $content): string
|
|
public function chatText(string $userId, string $content): string
|
|
|
{
|
|
{
|
|
|
$content = trim($content);
|
|
$content = trim($content);
|
|
|
- if ($content === '' || $this->apiToken === '' || $this->botId === '') {
|
|
|
|
|
|
|
+ if (!self::isEnabled() || $content === '' || $this->apiToken === '' || $this->botId === '') {
|
|
|
return '';
|
|
return '';
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -62,14 +74,15 @@ class CozeServices
|
|
|
'Authorization: Bearer ' . $this->apiToken,
|
|
'Authorization: Bearer ' . $this->apiToken,
|
|
|
'Content-Type: application/json',
|
|
'Content-Type: application/json',
|
|
|
'Accept: text/event-stream',
|
|
'Accept: text/event-stream',
|
|
|
- ], 20);
|
|
|
|
|
|
|
+ ], $this->timeout);
|
|
|
|
|
|
|
|
if ($response === false || $response === '') {
|
|
if ($response === false || $response === '') {
|
|
|
Log::error('Coze 小程序客服调用失败:' . json_encode(HttpService::getStatus(), JSON_UNESCAPED_UNICODE) . ' ' . HttpService::getCurlError());
|
|
Log::error('Coze 小程序客服调用失败:' . json_encode(HttpService::getStatus(), JSON_UNESCAPED_UNICODE) . ' ' . HttpService::getCurlError());
|
|
|
- return '';
|
|
|
|
|
|
|
+ return $this->fallbackReply;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return $this->parseStreamAnswer($response);
|
|
|
|
|
|
|
+ $answer = $this->parseStreamAnswer($response);
|
|
|
|
|
+ return $answer !== '' ? $answer : $this->fallbackReply;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
protected function parseStreamAnswer(string $response): string
|
|
protected function parseStreamAnswer(string $response): string
|
|
@@ -124,11 +137,13 @@ class CozeServices
|
|
|
return trim($answer);
|
|
return trim($answer);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected function env(string $key, string $serverKey, string $default = ''): string
|
|
|
|
|
|
|
+ protected function env(array $keys, string $serverKey, string $default = ''): string
|
|
|
{
|
|
{
|
|
|
- $value = Env::get($key, '');
|
|
|
|
|
- if ($value !== '') {
|
|
|
|
|
- return (string)$value;
|
|
|
|
|
|
|
+ foreach ($keys as $key) {
|
|
|
|
|
+ $value = Env::get($key, '');
|
|
|
|
|
+ if ($value !== '') {
|
|
|
|
|
+ return (string)$value;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$serverValue = getenv($serverKey);
|
|
$serverValue = getenv($serverKey);
|