Make bot token also per-company with global fallback
Both bot_token and chat_id are now per-company. If a company doesn't set its own bot_token, falls back to the global one. Removed parenthetical hints from labels, all admins can now configure their own Telegram bot. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
52
api.php
52
api.php
@@ -905,17 +905,20 @@ class ImapClient {
|
||||
// ==================== TICKETS HELPER ====================
|
||||
|
||||
function sendTelegramAlert(string $companyId, array $ticket): void {
|
||||
// Bot token on globaali, chat_id on yrityskohtainen
|
||||
$config = dbLoadConfig();
|
||||
$botToken = $config['telegram_bot_token'] ?? '';
|
||||
if (!$botToken) return;
|
||||
|
||||
// Hae yrityskohtainen chat_id integraation configista
|
||||
// Hae yrityskohtainen Telegram-konfiguraatio
|
||||
$integ = dbGetIntegration($companyId, 'telegram');
|
||||
if (!$integ || !$integ['enabled']) return;
|
||||
$chatId = $integ['config']['chat_id'] ?? '';
|
||||
if (!$chatId) return;
|
||||
|
||||
// Bot token: yrityskohtainen tai globaali fallback
|
||||
$botToken = $integ['config']['bot_token'] ?? '';
|
||||
if (!$botToken) {
|
||||
$config = dbLoadConfig();
|
||||
$botToken = $config['telegram_bot_token'] ?? '';
|
||||
}
|
||||
if (!$botToken) return;
|
||||
|
||||
// Hae yrityksen nimi
|
||||
$company = dbGetCompany($companyId);
|
||||
$companyName = $company['nimi'] ?? $companyId;
|
||||
@@ -1377,14 +1380,14 @@ switch ($action) {
|
||||
requireAuth();
|
||||
$companyId = requireCompany();
|
||||
$globalConf = dbLoadConfig();
|
||||
// Telegram chat_id on yrityskohtainen
|
||||
// Telegram: yrityskohtainen config (bot_token + chat_id), globaali fallback
|
||||
$teleInteg = dbGetIntegration($companyId, 'telegram');
|
||||
$teleChatId = ($teleInteg && $teleInteg['config']) ? ($teleInteg['config']['chat_id'] ?? '') : '';
|
||||
$teleConf = ($teleInteg && $teleInteg['config']) ? $teleInteg['config'] : [];
|
||||
echo json_encode([
|
||||
'api_key' => dbGetCompanyApiKey($companyId),
|
||||
'cors_origins' => dbGetCompanyCorsOrigins($companyId),
|
||||
'telegram_bot_token' => $globalConf['telegram_bot_token'] ?? '',
|
||||
'telegram_chat_id' => $teleChatId,
|
||||
'telegram_bot_token' => $teleConf['bot_token'] ?? ($globalConf['telegram_bot_token'] ?? ''),
|
||||
'telegram_chat_id' => $teleConf['chat_id'] ?? '',
|
||||
]);
|
||||
break;
|
||||
|
||||
@@ -1400,16 +1403,17 @@ switch ($action) {
|
||||
$origins = array_filter(array_map('trim', explode("\n", $input['cors_origins'])));
|
||||
dbSetCompanyCorsOrigins($companyId, array_values($origins));
|
||||
}
|
||||
// Telegram Bot Token: globaali (vain superadmin voi muuttaa)
|
||||
if (isset($input['telegram_bot_token']) && ($_SESSION['role'] ?? '') === 'superadmin') {
|
||||
dbSaveConfig(['telegram_bot_token' => trim($input['telegram_bot_token'])]);
|
||||
}
|
||||
// Telegram Chat ID: yrityskohtainen (tallennetaan integrations-tauluun)
|
||||
if (isset($input['telegram_chat_id'])) {
|
||||
// Telegram-asetukset: bot_token ja chat_id yrityskohtaisia
|
||||
if (isset($input['telegram_bot_token']) || isset($input['telegram_chat_id'])) {
|
||||
$teleInteg = dbGetIntegration($companyId, 'telegram');
|
||||
$teleConfig = ($teleInteg && $teleInteg['config']) ? $teleInteg['config'] : [];
|
||||
$teleConfig['chat_id'] = trim($input['telegram_chat_id']);
|
||||
$teleEnabled = $teleInteg ? $teleInteg['enabled'] : false;
|
||||
if (isset($input['telegram_bot_token'])) {
|
||||
$teleConfig['bot_token'] = trim($input['telegram_bot_token']);
|
||||
}
|
||||
if (isset($input['telegram_chat_id'])) {
|
||||
$teleConfig['chat_id'] = trim($input['telegram_chat_id']);
|
||||
}
|
||||
dbSaveIntegration($companyId, 'telegram', $teleEnabled, $teleConfig);
|
||||
}
|
||||
dbAddLog($companyId, currentUser(), 'config_update', '', '', 'Päivitti asetuksia');
|
||||
@@ -1423,14 +1427,18 @@ switch ($action) {
|
||||
requireAdmin();
|
||||
$companyId = requireCompany();
|
||||
if ($method !== 'POST') break;
|
||||
$config = dbLoadConfig();
|
||||
$botToken = $config['telegram_bot_token'] ?? '';
|
||||
// Chat ID yrityskohtainen
|
||||
// Bot token + chat_id: yrityskohtainen, fallback globaaliin
|
||||
$teleInteg = dbGetIntegration($companyId, 'telegram');
|
||||
$chatId = ($teleInteg && $teleInteg['config']) ? ($teleInteg['config']['chat_id'] ?? '') : '';
|
||||
$teleConf = ($teleInteg && $teleInteg['config']) ? $teleInteg['config'] : [];
|
||||
$botToken = $teleConf['bot_token'] ?? '';
|
||||
if (!$botToken) {
|
||||
$globalConf = dbLoadConfig();
|
||||
$botToken = $globalConf['telegram_bot_token'] ?? '';
|
||||
}
|
||||
$chatId = $teleConf['chat_id'] ?? '';
|
||||
if (!$botToken || !$chatId) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'Telegram Bot Token (globaali) ja Chat ID (yrityskohtainen) vaaditaan']);
|
||||
echo json_encode(['error' => 'Bot Token ja Chat ID vaaditaan']);
|
||||
break;
|
||||
}
|
||||
$company = dbGetCompany($companyId);
|
||||
|
||||
@@ -1571,14 +1571,14 @@
|
||||
<!-- Telegram-asetukset -->
|
||||
<div class="table-card" id="settings-telegram-card" style="padding:1.5rem;margin-top:1rem;display:none;">
|
||||
<h3 style="color:#0f3460;margin-bottom:0.5rem;border-bottom:2px solid #f0f2f5;padding-bottom:0.5rem;">Telegram-hälytykset</h3>
|
||||
<p style="color:#666;font-size:0.85rem;margin-bottom:1rem;">URGENT-prioriteetin tiketit lähettävät hälytyksen Telegram-kanavalle. Chat ID on yrityskohtainen — eri yritykset voivat käyttää eri kanavia.</p>
|
||||
<p style="color:#666;font-size:0.85rem;margin-bottom:1rem;">URGENT-prioriteetin tiketit lähettävät hälytyksen Telegram-kanavalle.</p>
|
||||
<div class="form-grid" style="max-width:500px;">
|
||||
<div class="form-group full-width" id="telegram-token-group">
|
||||
<label>Bot Token <span style="color:#888;font-weight:normal;font-size:0.8rem;">(globaali, kaikille yrityksille sama)</span></label>
|
||||
<label>Bot Token</label>
|
||||
<input type="text" id="settings-telegram-token" placeholder="123456:ABC-DEF..." style="font-family:monospace;">
|
||||
</div>
|
||||
<div class="form-group full-width">
|
||||
<label>Chat ID <span style="color:#888;font-weight:normal;font-size:0.8rem;">(yrityskohtainen kanava/ryhmä)</span></label>
|
||||
<label>Chat ID</label>
|
||||
<input type="text" id="settings-telegram-chat" placeholder="-1001234567890" style="font-family:monospace;">
|
||||
</div>
|
||||
<div class="form-group full-width" style="display:flex;gap:0.5rem;">
|
||||
|
||||
@@ -2735,12 +2735,9 @@ async function loadSettings() {
|
||||
const key = config.api_key || 'AVAIN';
|
||||
document.getElementById('api-example-url').textContent = `api.php?action=saatavuus&key=${key}&osoite=Esimerkkikatu+1&postinumero=00100&kaupunki=Helsinki`;
|
||||
|
||||
// Telegram-asetukset
|
||||
// Telegram-asetukset (yrityskohtaiset)
|
||||
document.getElementById('settings-telegram-token').value = config.telegram_bot_token || '';
|
||||
document.getElementById('settings-telegram-chat').value = config.telegram_chat_id || '';
|
||||
// Bot Token vain superadminille
|
||||
const tokenGroup = document.getElementById('telegram-token-group');
|
||||
if (tokenGroup) tokenGroup.style.display = currentUser?.role === 'superadmin' ? '' : 'none';
|
||||
} catch (e) { console.error(e); }
|
||||
|
||||
// Lataa saatavuuskyselyt
|
||||
|
||||
Reference in New Issue
Block a user