feat(config): support custom telegram webhook url

This commit is contained in:
xboard
2026-03-11 02:50:33 +08:00
parent 562064712d
commit b340652b4d
2 changed files with 43 additions and 6 deletions
@@ -71,17 +71,22 @@ class ConfigController extends Controller
public function setTelegramWebhook(Request $request) public function setTelegramWebhook(Request $request)
{ {
$app_url = admin_setting('app_url'); $hookUrl = $this->resolveTelegramWebhookUrl();
if (blank($app_url)) if (blank($hookUrl)) {
return $this->fail([422, '请先设置站点网址']); return $this->fail([422, 'Telegram Webhook地址未配置']);
$hookUrl = $app_url . '/api/v1/guest/telegram/webhook?' . http_build_query([ }
$hookUrl .= '?' . http_build_query([
'access_token' => md5(admin_setting('telegram_bot_token', $request->input('telegram_bot_token'))) 'access_token' => md5(admin_setting('telegram_bot_token', $request->input('telegram_bot_token')))
]); ]);
$telegramService = new TelegramService($request->input('telegram_bot_token')); $telegramService = new TelegramService($request->input('telegram_bot_token'));
$telegramService->getMe(); $telegramService->getMe();
$telegramService->setWebhook($hookUrl); $telegramService->setWebhook(url: $hookUrl);
$telegramService->registerBotCommands(); $telegramService->registerBotCommands();
return $this->success(true); return $this->success([
'success' => true,
'webhook_url' => $hookUrl,
'webhook_base_url' => $this->getTelegramWebhookBaseUrl(),
]);
} }
public function fetch(Request $request) public function fetch(Request $request)
@@ -171,6 +176,7 @@ class ConfigController extends Controller
'telegram' => [ 'telegram' => [
'telegram_bot_enable' => (bool) admin_setting('telegram_bot_enable', 0), 'telegram_bot_enable' => (bool) admin_setting('telegram_bot_enable', 0),
'telegram_bot_token' => admin_setting('telegram_bot_token'), 'telegram_bot_token' => admin_setting('telegram_bot_token'),
'telegram_webhook_url' => admin_setting('telegram_webhook_url'),
'telegram_discuss_link' => admin_setting('telegram_discuss_link') 'telegram_discuss_link' => admin_setting('telegram_discuss_link')
], ],
'app' => [ 'app' => [
@@ -313,4 +319,33 @@ class ConfigController extends Controller
return ''; return '';
} }
private function getTelegramWebhookBaseUrl(): ?string
{
$customUrl = trim((string) admin_setting('telegram_webhook_url', ''));
if ($customUrl !== '') {
return rtrim($customUrl, '/');
}
$appUrl = trim((string) admin_setting('app_url', ''));
if ($appUrl !== '') {
return rtrim($appUrl, '/');
}
return null;
}
private function resolveTelegramWebhookUrl(): ?string
{
$baseUrl = $this->getTelegramWebhookBaseUrl();
if (!$baseUrl) {
return null;
}
if (str_contains($baseUrl, '/api/v1/guest/telegram/webhook')) {
return $baseUrl;
}
return $baseUrl . '/api/v1/guest/telegram/webhook';
}
} }
+2
View File
@@ -68,6 +68,7 @@ class ConfigSave extends FormRequest
// telegram // telegram
'telegram_bot_enable' => '', 'telegram_bot_enable' => '',
'telegram_bot_token' => '', 'telegram_bot_token' => '',
'telegram_webhook_url' => 'nullable|url',
'telegram_discuss_id' => '', 'telegram_discuss_id' => '',
'telegram_channel_id' => '', 'telegram_channel_id' => '',
'telegram_discuss_link' => 'nullable|url', 'telegram_discuss_link' => 'nullable|url',
@@ -128,6 +129,7 @@ class ConfigSave extends FormRequest
'subscribe_url.url' => '订阅URL格式不正确,必须携带http(s)://', 'subscribe_url.url' => '订阅URL格式不正确,必须携带http(s)://',
'server_token.min' => '通讯密钥长度必须大于16位', 'server_token.min' => '通讯密钥长度必须大于16位',
'tos_url.url' => '服务条款URL格式不正确,必须携带http(s)://', 'tos_url.url' => '服务条款URL格式不正确,必须携带http(s)://',
'telegram_webhook_url.url' => 'Telegram Webhook地址格式不正确,必须携带http(s)://',
'telegram_discuss_link.url' => 'Telegram群组地址必须为URL格式,必须携带http(s)://', 'telegram_discuss_link.url' => 'Telegram群组地址必须为URL格式,必须携带http(s)://',
'logo.url' => 'LOGO URL格式不正确,必须携带https(s)://', 'logo.url' => 'LOGO URL格式不正确,必须携带https(s)://',
'secure_path.min' => '后台路径长度最小为8位', 'secure_path.min' => '后台路径长度最小为8位',