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:
2026-03-13 19:08:58 +02:00
parent 20a2b78782
commit acd3591544
3 changed files with 34 additions and 29 deletions

52
api.php
View File

@@ -905,17 +905,20 @@ class ImapClient {
// ==================== TICKETS HELPER ==================== // ==================== TICKETS HELPER ====================
function sendTelegramAlert(string $companyId, array $ticket): void { function sendTelegramAlert(string $companyId, array $ticket): void {
// Bot token on globaali, chat_id on yrityskohtainen // Hae yrityskohtainen Telegram-konfiguraatio
$config = dbLoadConfig();
$botToken = $config['telegram_bot_token'] ?? '';
if (!$botToken) return;
// Hae yrityskohtainen chat_id integraation configista
$integ = dbGetIntegration($companyId, 'telegram'); $integ = dbGetIntegration($companyId, 'telegram');
if (!$integ || !$integ['enabled']) return; if (!$integ || !$integ['enabled']) return;
$chatId = $integ['config']['chat_id'] ?? ''; $chatId = $integ['config']['chat_id'] ?? '';
if (!$chatId) return; 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 // Hae yrityksen nimi
$company = dbGetCompany($companyId); $company = dbGetCompany($companyId);
$companyName = $company['nimi'] ?? $companyId; $companyName = $company['nimi'] ?? $companyId;
@@ -1377,14 +1380,14 @@ switch ($action) {
requireAuth(); requireAuth();
$companyId = requireCompany(); $companyId = requireCompany();
$globalConf = dbLoadConfig(); $globalConf = dbLoadConfig();
// Telegram chat_id on yrityskohtainen // Telegram: yrityskohtainen config (bot_token + chat_id), globaali fallback
$teleInteg = dbGetIntegration($companyId, 'telegram'); $teleInteg = dbGetIntegration($companyId, 'telegram');
$teleChatId = ($teleInteg && $teleInteg['config']) ? ($teleInteg['config']['chat_id'] ?? '') : ''; $teleConf = ($teleInteg && $teleInteg['config']) ? $teleInteg['config'] : [];
echo json_encode([ echo json_encode([
'api_key' => dbGetCompanyApiKey($companyId), 'api_key' => dbGetCompanyApiKey($companyId),
'cors_origins' => dbGetCompanyCorsOrigins($companyId), 'cors_origins' => dbGetCompanyCorsOrigins($companyId),
'telegram_bot_token' => $globalConf['telegram_bot_token'] ?? '', 'telegram_bot_token' => $teleConf['bot_token'] ?? ($globalConf['telegram_bot_token'] ?? ''),
'telegram_chat_id' => $teleChatId, 'telegram_chat_id' => $teleConf['chat_id'] ?? '',
]); ]);
break; break;
@@ -1400,16 +1403,17 @@ switch ($action) {
$origins = array_filter(array_map('trim', explode("\n", $input['cors_origins']))); $origins = array_filter(array_map('trim', explode("\n", $input['cors_origins'])));
dbSetCompanyCorsOrigins($companyId, array_values($origins)); dbSetCompanyCorsOrigins($companyId, array_values($origins));
} }
// Telegram Bot Token: globaali (vain superadmin voi muuttaa) // Telegram-asetukset: bot_token ja chat_id yrityskohtaisia
if (isset($input['telegram_bot_token']) && ($_SESSION['role'] ?? '') === 'superadmin') { if (isset($input['telegram_bot_token']) || isset($input['telegram_chat_id'])) {
dbSaveConfig(['telegram_bot_token' => trim($input['telegram_bot_token'])]);
}
// Telegram Chat ID: yrityskohtainen (tallennetaan integrations-tauluun)
if (isset($input['telegram_chat_id'])) {
$teleInteg = dbGetIntegration($companyId, 'telegram'); $teleInteg = dbGetIntegration($companyId, 'telegram');
$teleConfig = ($teleInteg && $teleInteg['config']) ? $teleInteg['config'] : []; $teleConfig = ($teleInteg && $teleInteg['config']) ? $teleInteg['config'] : [];
$teleConfig['chat_id'] = trim($input['telegram_chat_id']);
$teleEnabled = $teleInteg ? $teleInteg['enabled'] : false; $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); dbSaveIntegration($companyId, 'telegram', $teleEnabled, $teleConfig);
} }
dbAddLog($companyId, currentUser(), 'config_update', '', '', 'Päivitti asetuksia'); dbAddLog($companyId, currentUser(), 'config_update', '', '', 'Päivitti asetuksia');
@@ -1423,14 +1427,18 @@ switch ($action) {
requireAdmin(); requireAdmin();
$companyId = requireCompany(); $companyId = requireCompany();
if ($method !== 'POST') break; if ($method !== 'POST') break;
$config = dbLoadConfig(); // Bot token + chat_id: yrityskohtainen, fallback globaaliin
$botToken = $config['telegram_bot_token'] ?? '';
// Chat ID yrityskohtainen
$teleInteg = dbGetIntegration($companyId, 'telegram'); $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) { if (!$botToken || !$chatId) {
http_response_code(400); 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; break;
} }
$company = dbGetCompany($companyId); $company = dbGetCompany($companyId);

View File

@@ -1571,14 +1571,14 @@
<!-- Telegram-asetukset --> <!-- Telegram-asetukset -->
<div class="table-card" id="settings-telegram-card" style="padding:1.5rem;margin-top:1rem;display:none;"> <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> <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-grid" style="max-width:500px;">
<div class="form-group full-width" id="telegram-token-group"> <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;"> <input type="text" id="settings-telegram-token" placeholder="123456:ABC-DEF..." style="font-family:monospace;">
</div> </div>
<div class="form-group full-width"> <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;"> <input type="text" id="settings-telegram-chat" placeholder="-1001234567890" style="font-family:monospace;">
</div> </div>
<div class="form-group full-width" style="display:flex;gap:0.5rem;"> <div class="form-group full-width" style="display:flex;gap:0.5rem;">

View File

@@ -2735,12 +2735,9 @@ async function loadSettings() {
const key = config.api_key || 'AVAIN'; 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`; 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-token').value = config.telegram_bot_token || '';
document.getElementById('settings-telegram-chat').value = config.telegram_chat_id || ''; 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); } } catch (e) { console.error(e); }
// Lataa saatavuuskyselyt // Lataa saatavuuskyselyt