mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-14 16:26:57 +08:00
fix(telegram): use Markdown rendering + simplify language selection condition
- sendMarkdownMsg() helper: sends with ParseMode=Markdown, falls back to plain text - All formatted messages (langSelectionMsg, buildSetupGuide, helpMessage) now render bold text and code blocks correctly in Telegram - Simplify /start language check: isLangDefault(st) alone is sufficient (lang == 'en' && isLangDefault was redundant — GetLanguage returns 'en' when empty)
This commit is contained in:
@@ -122,9 +122,9 @@ func runBot(token string, cfg *config.Config, st *store.Store) bool {
|
|||||||
if lang != "" {
|
if lang != "" {
|
||||||
awaitingLang = false
|
awaitingLang = false
|
||||||
st.TelegramConfig().SetLanguage(lang) //nolint:errcheck
|
st.TelegramConfig().SetLanguage(lang) //nolint:errcheck
|
||||||
sendMsg(bot, chatID, buildSetupGuide(st, botUserID, cfg.APIServerPort, botToken, lang))
|
sendMarkdownMsg(bot, chatID, buildSetupGuide(st, botUserID, cfg.APIServerPort, botToken, lang))
|
||||||
} else {
|
} else {
|
||||||
sendMsg(bot, chatID, langSelectionMsg())
|
sendMarkdownMsg(bot, chatID, langSelectionMsg())
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -146,14 +146,13 @@ func runBot(token string, cfg *config.Config, st *store.Store) bool {
|
|||||||
} else {
|
} else {
|
||||||
agents.Reset(chatID)
|
agents.Reset(chatID)
|
||||||
}
|
}
|
||||||
// Show language selection if not chosen yet; otherwise go straight to guide.
|
// Show language selection if user has never chosen (Language == ""); go straight to guide otherwise.
|
||||||
lang := st.TelegramConfig().GetLanguage()
|
if isLangDefault(st) {
|
||||||
if lang == "en" && isLangDefault(st) {
|
|
||||||
// First time: ask language preference
|
|
||||||
awaitingLang = true
|
awaitingLang = true
|
||||||
sendMsg(bot, chatID, langSelectionMsg())
|
sendMarkdownMsg(bot, chatID, langSelectionMsg())
|
||||||
} else {
|
} else {
|
||||||
sendMsg(bot, chatID, buildSetupGuide(st, botUserID, cfg.APIServerPort, botToken, lang))
|
lang := st.TelegramConfig().GetLanguage()
|
||||||
|
sendMarkdownMsg(bot, chatID, buildSetupGuide(st, botUserID, cfg.APIServerPort, botToken, lang))
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -161,14 +160,14 @@ func runBot(token string, cfg *config.Config, st *store.Store) bool {
|
|||||||
// Handle /lang: change language at any time
|
// Handle /lang: change language at any time
|
||||||
if text == "/lang" {
|
if text == "/lang" {
|
||||||
awaitingLang = true
|
awaitingLang = true
|
||||||
sendMsg(bot, chatID, langSelectionMsg())
|
sendMarkdownMsg(bot, chatID, langSelectionMsg())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle /help
|
// Handle /help
|
||||||
if text == "/help" {
|
if text == "/help" {
|
||||||
lang := st.TelegramConfig().GetLanguage()
|
lang := st.TelegramConfig().GetLanguage()
|
||||||
sendMsg(bot, chatID, helpMessage(lang))
|
sendMarkdownMsg(bot, chatID, helpMessage(lang))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,13 +187,13 @@ func runBot(token string, cfg *config.Config, st *store.Store) bool {
|
|||||||
// Direct setup commands (no LLM needed): "configure deepseek sk-xxx" / "配置 deepseek sk-xxx"
|
// Direct setup commands (no LLM needed): "configure deepseek sk-xxx" / "配置 deepseek sk-xxx"
|
||||||
lang := st.TelegramConfig().GetLanguage()
|
lang := st.TelegramConfig().GetLanguage()
|
||||||
if reply, handled := tryHandleSetupCommand(text, cfg.APIServerPort, botToken, st, botUserID, lang); handled {
|
if reply, handled := tryHandleSetupCommand(text, cfg.APIServerPort, botToken, st, botUserID, lang); handled {
|
||||||
sendMsg(bot, chatID, reply)
|
sendMarkdownMsg(bot, chatID, reply)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Guard: if no AI model configured, show setup guide instead of failing.
|
// Guard: if no AI model configured, show setup guide instead of failing.
|
||||||
if newLLMClient(st, botUserID) == nil {
|
if newLLMClient(st, botUserID) == nil {
|
||||||
sendMsg(bot, chatID, buildSetupGuide(st, botUserID, cfg.APIServerPort, botToken, lang))
|
sendMarkdownMsg(bot, chatID, buildSetupGuide(st, botUserID, cfg.APIServerPort, botToken, lang))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,6 +258,17 @@ func sendMsg(bot *tgbotapi.BotAPI, chatID int64, text string) {
|
|||||||
bot.Send(msg) //nolint:errcheck
|
bot.Send(msg) //nolint:errcheck
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sendMarkdownMsg sends a message with Markdown formatting; falls back to plain text on parse error.
|
||||||
|
func sendMarkdownMsg(bot *tgbotapi.BotAPI, chatID int64, text string) {
|
||||||
|
msg := tgbotapi.NewMessage(chatID, text)
|
||||||
|
msg.ParseMode = "Markdown"
|
||||||
|
if _, err := bot.Send(msg); err != nil {
|
||||||
|
// Markdown rejected (e.g. unescaped special chars) — fall back to plain text.
|
||||||
|
plain := tgbotapi.NewMessage(chatID, text)
|
||||||
|
bot.Send(plain) //nolint:errcheck
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// newLLMClient builds an LLM client for the agent using the bound user's enabled model.
|
// newLLMClient builds an LLM client for the agent using the bound user's enabled model.
|
||||||
// Priority: bound user's DB model > environment variables.
|
// Priority: bound user's DB model > environment variables.
|
||||||
// Uses provider-specific constructors to ensure correct default base URLs and models.
|
// Uses provider-specific constructors to ensure correct default base URLs and models.
|
||||||
|
|||||||
Reference in New Issue
Block a user