|
|
<section id="telegram-notifications" class="mt-4" aria-labelledby="telegram-notifications-title">
|
|
|
|
|
|
<div class="py-3 d-flex align-items-md-center flex-md-row flex-column">
|
|
|
<div class="flex-grow-1">
|
|
|
<h5 id="telegram-notifications-title" class="fs-20 fw-semibold mb-1">Telegram 工单提醒</h5>
|
|
|
<p class="text-muted mb-0">配置工单机器人、独立通信域名和管理员私聊接收账号。用户仍只通过网站查看与回复工单。</p>
|
|
|
</div>
|
|
|
<div class="text-md-end mt-3 mt-md-0 admin-config-actions">
|
|
|
<span class="admin-config-status {if $telegram_enabled && $telegram_configured}is-success{else}is-loading{/if}" id="telegramPageStatus">
|
|
|
{if $telegram_enabled && $telegram_configured}通知服务已就绪{elseif $telegram_enabled}配置尚未完成{else}通知服务未启用{/if}
|
|
|
</span>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
{if $telegram_config.configuration_error|default:'' != ''}
|
|
|
<div class="alert alert-danger" role="alert">
|
|
|
<i class="mdi mdi-alert-circle-outline me-1"></i>{$telegram_config.configuration_error|escape:'html'}
|
|
|
</div>
|
|
|
{/if}
|
|
|
|
|
|
<div class="row g-3 mb-3">
|
|
|
<div class="col-xl-3 col-md-6"><div class="card admin-ticket-stat h-100"><div class="card-body">
|
|
|
<span class="admin-ticket-stat-icon {if !$telegram_enabled}is-closed{/if}"><i class="mdi mdi-bell-ring-outline"></i></span>
|
|
|
<div><p class="text-muted mb-1">通知开关</p><h5 class="mb-0">{if $telegram_enabled}已启用{else}未启用{/if}</h5></div>
|
|
|
</div></div></div>
|
|
|
<div class="col-xl-3 col-md-6"><div class="card admin-ticket-stat h-100"><div class="card-body">
|
|
|
<span class="admin-ticket-stat-icon {if !$telegram_config.bot_token_configured}is-open{/if}"><i class="mdi mdi-robot-outline"></i></span>
|
|
|
<div><p class="text-muted mb-1">Bot Token</p><h5 class="mb-0">{if $telegram_config.bot_token_configured}已加密保存{else}尚未配置{/if}</h5></div>
|
|
|
</div></div></div>
|
|
|
<div class="col-xl-3 col-md-6"><div class="card admin-ticket-stat h-100"><div class="card-body">
|
|
|
<span class="admin-ticket-stat-icon {if !$telegram_config.webhook_secret_configured}is-open{/if}"><i class="mdi mdi-shield-key-outline"></i></span>
|
|
|
<div><p class="text-muted mb-1">Webhook 密钥</p><h5 class="mb-0">{if $telegram_config.webhook_secret_configured}已安全生成{else}尚未生成{/if}</h5></div>
|
|
|
</div></div></div>
|
|
|
<div class="col-xl-3 col-md-6"><div class="card admin-ticket-stat h-100"><div class="card-body">
|
|
|
<span class="admin-ticket-stat-icon {if !$telegram_bound}is-closed{/if}"><i class="mdi mdi-account-check-outline"></i></span>
|
|
|
<div><p class="text-muted mb-1">接收管理员</p><h5 class="mb-0">{if $telegram_bound}管理员 #{$telegram_admin_id|escape:'html'}{else}尚未绑定{/if}</h5></div>
|
|
|
</div></div></div>
|
|
|
</div>
|
|
|
|
|
|
<div class="row g-3">
|
|
|
<section class="col-xl-7">
|
|
|
<div class="card h-100"><div class="card-body">
|
|
|
<div class="d-flex align-items-start justify-content-between gap-3 mb-3">
|
|
|
<div><p class="admin-eyebrow mb-1">Bot Configuration</p><h5 class="card-title mb-1">机器人与通信地址</h5><p class="text-muted mb-0">配置保存到数据库;敏感项加密存储且永不回显。</p></div>
|
|
|
<span class="admin-ticket-stat-icon"><i class="mdi mdi-telegram"></i></span>
|
|
|
</div>
|
|
|
<form id="telegramConfigForm" autocomplete="off">
|
|
|
<div class="form-check form-switch mb-3">
|
|
|
<input class="form-check-input" id="telegramEnabled" name="telegram_enabled" type="checkbox" value="true" {if $telegram_enabled}checked{/if} {if !$admin_can_manage|default:false}disabled{/if}>
|
|
|
<label class="form-check-label fw-semibold" for="telegramEnabled">启用 Telegram 工单提醒</label>
|
|
|
<div class="form-text">关闭后不会接收 Webhook,也不会发送工单通知;已保存配置和管理员绑定会保留。</div>
|
|
|
</div>
|
|
|
<div class="mb-3">
|
|
|
<label class="form-label" for="telegramWebhookBaseUrl">独立 Telegram 通信域名</label>
|
|
|
<input class="form-control" id="telegramWebhookBaseUrl" name="telegram_webhook_base_url" type="url" value="{$telegram_config.webhook_base_url|default:''|escape:'html'}" maxlength="255" inputmode="url" placeholder="https://tg.example.com" {if !$admin_can_manage|default:false}disabled{/if}>
|
|
|
<div class="form-text">只填写 HTTPS 根地址,不含路径、查询参数或结尾斜杠。该域名需通过 DNS、TLS 与 Web 服务器指向本应用。</div>
|
|
|
</div>
|
|
|
<div class="mb-3">
|
|
|
<label class="form-label" for="telegramBotToken">Telegram Bot Token</label>
|
|
|
<input class="form-control admin-mono" id="telegramBotToken" name="telegram_bot_token" type="password" maxlength="256" autocomplete="new-password" spellcheck="false" placeholder="{if $telegram_config.bot_token_configured}已配置,留空保留现有 Token{else}请输入 BotFather 提供的 Token{/if}" {if !$admin_can_manage|default:false}disabled{/if}>
|
|
|
<div class="form-text">Token 只在本次保存请求中发送。更换 Token 会自动解除旧管理员绑定,避免向错误机器人继续投递。</div>
|
|
|
</div>
|
|
|
<div class="form-check mb-3">
|
|
|
<input class="form-check-input" id="telegramRotateSecret" name="telegram_rotate_webhook_secret" type="checkbox" value="true" {if !$admin_can_manage|default:false}disabled{/if}>
|
|
|
<label class="form-check-label" for="telegramRotateSecret">轮换 Webhook 请求密钥</label>
|
|
|
<div class="form-text">首次保存会自动生成高强度密钥。主动轮换后旧 Webhook 请求立即失效,需要重新注册 Webhook。</div>
|
|
|
</div>
|
|
|
<div class="d-flex align-items-center flex-wrap gap-2">
|
|
|
<button class="btn btn-primary" id="telegramConfigSave" type="submit" {if !$admin_can_manage|default:false}disabled{/if}><i class="mdi mdi-content-save-outline me-1"></i>保存 Telegram 配置</button>
|
|
|
<span class="text-muted small">保存成功后再执行右侧的 Webhook 注册。</span>
|
|
|
</div>
|
|
|
</form>
|
|
|
</div></div>
|
|
|
</section>
|
|
|
|
|
|
<section class="col-xl-5">
|
|
|
<div class="card mb-3"><div class="card-body">
|
|
|
<div class="d-flex align-items-start justify-content-between gap-3 mb-3">
|
|
|
<div><p class="admin-eyebrow mb-1">Webhook</p><h5 class="card-title mb-1">注册 Telegram 回调</h5><p class="text-muted mb-0">把当前独立通信地址注册到 Telegram。</p></div>
|
|
|
<span class="admin-ticket-stat-icon is-open"><i class="mdi mdi-webhook"></i></span>
|
|
|
</div>
|
|
|
<label class="form-label">当前回调地址</label>
|
|
|
<div class="border rounded-2 bg-light px-3 py-2 mb-3 admin-mono text-break">{$telegram_config.webhook_url|default:'尚未配置通信域名'|escape:'html'}</div>
|
|
|
<button class="btn btn-light" id="telegramWebhook" type="button" {if !$telegram_enabled || !$telegram_configured || !$admin_can_manage|default:false}disabled{/if}><i class="mdi mdi-cloud-sync-outline me-1"></i>注册当前 Webhook</button>
|
|
|
</div></div>
|
|
|
|
|
|
<div class="card"><div class="card-body">
|
|
|
<div class="d-flex align-items-start justify-content-between gap-3 mb-3">
|
|
|
<div><p class="admin-eyebrow mb-1">Private Binding</p><h5 class="card-title mb-1">管理员私聊绑定</h5><p class="text-muted mb-0">只接受管理员本人的 Telegram 私聊,群组和转发消息无效。</p></div>
|
|
|
<span class="admin-ticket-stat-icon"><i class="mdi mdi-account-lock-outline"></i></span>
|
|
|
</div>
|
|
|
<div class="d-flex flex-wrap gap-2">
|
|
|
<button class="btn btn-primary" id="telegramBind" type="button" {if !$telegram_enabled || !$telegram_configured || !$admin_can_manage|default:false}disabled{/if}><i class="mdi mdi-link-variant-plus me-1"></i>生成绑定命令</button>
|
|
|
<button class="btn btn-outline-danger" id="telegramUnbind" type="button" {if !$telegram_bound || !$admin_can_manage|default:false}disabled{/if}><i class="mdi mdi-link-variant-off me-1"></i>解除绑定</button>
|
|
|
</div>
|
|
|
<div class="alert alert-info mt-3 mb-0" id="telegramBinding" hidden>
|
|
|
<div class="d-flex align-items-center justify-content-between gap-2 flex-wrap"><code class="text-break" id="telegramCommand"></code><button class="btn btn-sm btn-light" id="telegramCopyCommand" type="button"><i class="mdi mdi-content-copy me-1"></i>复制</button></div>
|
|
|
<p class="mb-0 mt-2" id="telegramExpiry"></p>
|
|
|
<p class="mb-0">请在机器人私聊中发送,成功后刷新本页确认绑定状态;不要公开这条一次性命令。</p>
|
|
|
</div>
|
|
|
</div></div>
|
|
|
</section>
|
|
|
</div>
|
|
|
|
|
|
<div class="mt-3 d-flex justify-content-end"><span class="admin-config-status" id="telegramStatus" role="status" aria-live="polite">等待操作</span></div>
|
|
|
</section>
|
|
|
|
|
|
{literal}
|
|
|
<script>
|
|
|
(function () {
|
|
|
'use strict';
|
|
|
var busy = false;
|
|
|
var timer = null;
|
|
|
var form = document.getElementById('telegramConfigForm');
|
|
|
var save = document.getElementById('telegramConfigSave');
|
|
|
var bind = document.getElementById('telegramBind');
|
|
|
var unbind = document.getElementById('telegramUnbind');
|
|
|
var webhook = document.getElementById('telegramWebhook');
|
|
|
var copy = document.getElementById('telegramCopyCommand');
|
|
|
var status = document.getElementById('telegramStatus');
|
|
|
var binding = document.getElementById('telegramBinding');
|
|
|
var command = document.getElementById('telegramCommand');
|
|
|
var expiry = document.getElementById('telegramExpiry');
|
|
|
var controls = [save, bind, unbind, webhook].filter(Boolean);
|
|
|
|
|
|
function showStatus(message, state) {
|
|
|
status.textContent = message;
|
|
|
status.className = 'admin-config-status' + (state ? ' is-' + state : '');
|
|
|
}
|
|
|
function encodeForm(source) {
|
|
|
var body = new URLSearchParams();
|
|
|
new FormData(source).forEach(function (value, key) { body.append(key, String(value)); });
|
|
|
return body.toString();
|
|
|
}
|
|
|
async function request(url, body) {
|
|
|
if (busy) return null;
|
|
|
busy = true;
|
|
|
var original = controls.map(function (button) { return button.disabled; });
|
|
|
controls.forEach(function (button) { button.disabled = true; });
|
|
|
showStatus('正在处理…', 'loading');
|
|
|
try {
|
|
|
var response = await fetch(url, {
|
|
|
method: 'POST', credentials: 'same-origin',
|
|
|
headers: {'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With': 'XMLHttpRequest', 'X-Admin-CSRF-Token': window.ADMIN_CSRF_TOKEN || ''},
|
|
|
body: body || ''
|
|
|
});
|
|
|
var text = await response.text();
|
|
|
var data;
|
|
|
try { data = JSON.parse(text); }
|
|
|
catch (error) { throw new Error('服务器未返回有效结果(HTTP ' + response.status + ')'); }
|
|
|
if (!response.ok || Number(data.ret) !== 1) throw new Error(data.msg || data.error || '操作失败');
|
|
|
showStatus(data.msg || '操作完成', 'success');
|
|
|
return data;
|
|
|
} catch (error) {
|
|
|
showStatus(error.message || '请求失败', 'error');
|
|
|
return null;
|
|
|
} finally {
|
|
|
busy = false;
|
|
|
controls.forEach(function (button, index) { button.disabled = original[index]; });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
form.addEventListener('submit', async function (event) {
|
|
|
event.preventDefault();
|
|
|
var data = await request('/admin/telegram/config', encodeForm(form));
|
|
|
if (!data) return;
|
|
|
document.getElementById('telegramBotToken').value = '';
|
|
|
document.getElementById('telegramRotateSecret').checked = false;
|
|
|
window.setTimeout(function () {
|
|
|
window.location.hash = 'telegram-notifications';
|
|
|
window.location.reload();
|
|
|
}, 700);
|
|
|
});
|
|
|
bind.addEventListener('click', async function () {
|
|
|
var data = await request('/admin/telegram/bind', '');
|
|
|
if (!data) return;
|
|
|
var code = String(data.code || '');
|
|
|
var seconds = Number(data.expires_in);
|
|
|
if (!/^[A-Za-z0-9_-]{8,128}$/.test(code) || !Number.isFinite(seconds) || seconds <= 0) { showStatus('绑定命令返回无效,请重试。', 'error'); return; }
|
|
|
if (timer) clearInterval(timer);
|
|
|
command.textContent = '/start ' + code;
|
|
|
binding.hidden = false;
|
|
|
var deadline = Date.now() + seconds * 1000;
|
|
|
function updateExpiry() {
|
|
|
var left = Math.max(0, Math.ceil((deadline - Date.now()) / 1000));
|
|
|
expiry.textContent = left ? '绑定命令将在 ' + left + ' 秒后失效。' : '命令已过期,请重新生成。';
|
|
|
if (!left) { command.textContent = ''; clearInterval(timer); timer = null; }
|
|
|
}
|
|
|
updateExpiry(); timer = window.setInterval(updateExpiry, 1000);
|
|
|
});
|
|
|
unbind.addEventListener('click', async function () {
|
|
|
if (!window.confirm('解除后将不再向当前 Telegram 账号发送工单提醒,确定解除?')) return;
|
|
|
if (await request('/admin/telegram/unbind', '')) {
|
|
|
window.location.hash = 'telegram-notifications';
|
|
|
window.location.reload();
|
|
|
}
|
|
|
});
|
|
|
webhook.addEventListener('click', async function () {
|
|
|
if (!window.confirm('确定把页面显示的回调地址注册到当前 Telegram Bot?')) return;
|
|
|
await request('/admin/telegram/webhook', '');
|
|
|
});
|
|
|
copy.addEventListener('click', async function () {
|
|
|
var value = command.textContent || '';
|
|
|
if (!value) return;
|
|
|
try { await navigator.clipboard.writeText(value); showStatus('绑定命令已复制。', 'success'); }
|
|
|
catch (error) { showStatus('自动复制失败,请手动选择绑定命令。', 'error'); }
|
|
|
});
|
|
|
})();
|
|
|
</script>
|
|
|
{/literal}
|