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 ====================
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);