mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
feat: add TELEGRAM_NOTIFICATION configuration switch
Add support for enabling/disabling Telegram notifications independently from bot token presence. Configuration respects priority: environment variable > settings.json > default (enabled for backward compatibility).
This commit is contained in:
@@ -12,6 +12,7 @@ import { getSettingsFile, readSettings, writeSettings } from './settings'
|
|||||||
|
|
||||||
export interface ServerSettings {
|
export interface ServerSettings {
|
||||||
telegramBotToken: string | null
|
telegramBotToken: string | null
|
||||||
|
telegramNotification: boolean
|
||||||
webappHost: string
|
webappHost: string
|
||||||
webappPort: number
|
webappPort: number
|
||||||
webappUrl: string
|
webappUrl: string
|
||||||
@@ -22,6 +23,7 @@ export interface ServerSettingsResult {
|
|||||||
settings: ServerSettings
|
settings: ServerSettings
|
||||||
sources: {
|
sources: {
|
||||||
telegramBotToken: 'env' | 'file' | 'default'
|
telegramBotToken: 'env' | 'file' | 'default'
|
||||||
|
telegramNotification: 'env' | 'file' | 'default'
|
||||||
webappHost: 'env' | 'file' | 'default'
|
webappHost: 'env' | 'file' | 'default'
|
||||||
webappPort: 'env' | 'file' | 'default'
|
webappPort: 'env' | 'file' | 'default'
|
||||||
webappUrl: 'env' | 'file' | 'default'
|
webappUrl: 'env' | 'file' | 'default'
|
||||||
@@ -84,6 +86,7 @@ export async function loadServerSettings(dataDir: string): Promise<ServerSetting
|
|||||||
let needsSave = false
|
let needsSave = false
|
||||||
const sources: ServerSettingsResult['sources'] = {
|
const sources: ServerSettingsResult['sources'] = {
|
||||||
telegramBotToken: 'default',
|
telegramBotToken: 'default',
|
||||||
|
telegramNotification: 'default',
|
||||||
webappHost: 'default',
|
webappHost: 'default',
|
||||||
webappPort: 'default',
|
webappPort: 'default',
|
||||||
webappUrl: 'default',
|
webappUrl: 'default',
|
||||||
@@ -104,6 +107,20 @@ export async function loadServerSettings(dataDir: string): Promise<ServerSetting
|
|||||||
sources.telegramBotToken = 'file'
|
sources.telegramBotToken = 'file'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// telegramNotification: env > file > true (default enabled for backward compatibility)
|
||||||
|
let telegramNotification = true
|
||||||
|
if (process.env.TELEGRAM_NOTIFICATION !== undefined) {
|
||||||
|
telegramNotification = process.env.TELEGRAM_NOTIFICATION === 'true'
|
||||||
|
sources.telegramNotification = 'env'
|
||||||
|
if (settings.telegramNotification === undefined) {
|
||||||
|
settings.telegramNotification = telegramNotification
|
||||||
|
needsSave = true
|
||||||
|
}
|
||||||
|
} else if (settings.telegramNotification !== undefined) {
|
||||||
|
telegramNotification = settings.telegramNotification
|
||||||
|
sources.telegramNotification = 'file'
|
||||||
|
}
|
||||||
|
|
||||||
// webappHost: env > file > 127.0.0.1
|
// webappHost: env > file > 127.0.0.1
|
||||||
let webappHost = '127.0.0.1'
|
let webappHost = '127.0.0.1'
|
||||||
if (process.env.WEBAPP_HOST) {
|
if (process.env.WEBAPP_HOST) {
|
||||||
@@ -174,6 +191,7 @@ export async function loadServerSettings(dataDir: string): Promise<ServerSetting
|
|||||||
return {
|
return {
|
||||||
settings: {
|
settings: {
|
||||||
telegramBotToken,
|
telegramBotToken,
|
||||||
|
telegramNotification,
|
||||||
webappHost,
|
webappHost,
|
||||||
webappPort,
|
webappPort,
|
||||||
webappUrl,
|
webappUrl,
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ export interface Settings {
|
|||||||
}
|
}
|
||||||
// Server configuration (persisted from environment variables)
|
// Server configuration (persisted from environment variables)
|
||||||
telegramBotToken?: string
|
telegramBotToken?: string
|
||||||
|
telegramNotification?: boolean
|
||||||
webappHost?: string
|
webappHost?: string
|
||||||
webappPort?: number
|
webappPort?: number
|
||||||
webappUrl?: string
|
webappUrl?: string
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
* Optional environment variables:
|
* Optional environment variables:
|
||||||
* - CLI_API_TOKEN: Shared secret for hapi CLI authentication (auto-generated if not set)
|
* - CLI_API_TOKEN: Shared secret for hapi CLI authentication (auto-generated if not set)
|
||||||
* - TELEGRAM_BOT_TOKEN: Telegram Bot API token from @BotFather
|
* - TELEGRAM_BOT_TOKEN: Telegram Bot API token from @BotFather
|
||||||
|
* - TELEGRAM_NOTIFICATION: Enable/disable Telegram notifications (default: true)
|
||||||
* - WEBAPP_PORT: Port for Mini App HTTP server (default: 3006)
|
* - WEBAPP_PORT: Port for Mini App HTTP server (default: 3006)
|
||||||
* - WEBAPP_URL: Public URL for Telegram Mini App
|
* - WEBAPP_URL: Public URL for Telegram Mini App
|
||||||
* - CORS_ORIGINS: Comma-separated CORS origins
|
* - CORS_ORIGINS: Comma-separated CORS origins
|
||||||
@@ -27,6 +28,7 @@ export type ConfigSource = 'env' | 'file' | 'default'
|
|||||||
|
|
||||||
export interface ConfigSources {
|
export interface ConfigSources {
|
||||||
telegramBotToken: ConfigSource
|
telegramBotToken: ConfigSource
|
||||||
|
telegramNotification: ConfigSource
|
||||||
webappHost: ConfigSource
|
webappHost: ConfigSource
|
||||||
webappPort: ConfigSource
|
webappPort: ConfigSource
|
||||||
webappUrl: ConfigSource
|
webappUrl: ConfigSource
|
||||||
@@ -41,6 +43,9 @@ class Configuration {
|
|||||||
/** Telegram bot enabled status (token present) */
|
/** Telegram bot enabled status (token present) */
|
||||||
public readonly telegramEnabled: boolean
|
public readonly telegramEnabled: boolean
|
||||||
|
|
||||||
|
/** Telegram notifications enabled */
|
||||||
|
public readonly telegramNotification: boolean
|
||||||
|
|
||||||
/** CLI auth token (shared secret) */
|
/** CLI auth token (shared secret) */
|
||||||
public cliApiToken: string
|
public cliApiToken: string
|
||||||
|
|
||||||
@@ -88,6 +93,7 @@ class Configuration {
|
|||||||
// Apply server settings
|
// Apply server settings
|
||||||
this.telegramBotToken = serverSettings.telegramBotToken
|
this.telegramBotToken = serverSettings.telegramBotToken
|
||||||
this.telegramEnabled = Boolean(this.telegramBotToken)
|
this.telegramEnabled = Boolean(this.telegramBotToken)
|
||||||
|
this.telegramNotification = serverSettings.telegramNotification
|
||||||
this.webappHost = serverSettings.webappHost
|
this.webappHost = serverSettings.webappHost
|
||||||
this.webappPort = serverSettings.webappPort
|
this.webappPort = serverSettings.webappPort
|
||||||
this.miniAppUrl = serverSettings.webappUrl
|
this.miniAppUrl = serverSettings.webappUrl
|
||||||
|
|||||||
+6
-1
@@ -77,6 +77,8 @@ async function main() {
|
|||||||
} else {
|
} else {
|
||||||
const tokenSource = formatSource(config.sources.telegramBotToken)
|
const tokenSource = formatSource(config.sources.telegramBotToken)
|
||||||
console.log(`[Server] Telegram: enabled (${tokenSource})`)
|
console.log(`[Server] Telegram: enabled (${tokenSource})`)
|
||||||
|
const notificationSource = formatSource(config.sources.telegramNotification)
|
||||||
|
console.log(`[Server] Telegram notifications: ${config.telegramNotification ? 'enabled' : 'disabled'} (${notificationSource})`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const store = new Store(config.dbPath)
|
const store = new Store(config.dbPath)
|
||||||
@@ -111,7 +113,10 @@ async function main() {
|
|||||||
miniAppUrl: config.miniAppUrl,
|
miniAppUrl: config.miniAppUrl,
|
||||||
store
|
store
|
||||||
})
|
})
|
||||||
notificationChannels.push(happyBot)
|
// Only add to notification channels if notifications are enabled
|
||||||
|
if (config.telegramNotification) {
|
||||||
|
notificationChannels.push(happyBot)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
notificationHub = new NotificationHub(syncEngine, notificationChannels)
|
notificationHub = new NotificationHub(syncEngine, notificationChannels)
|
||||||
|
|||||||
Reference in New Issue
Block a user