mirror of
https://github.com/hrfee/jfa-go.git
synced 2026-01-18 16:47:42 +01:00
telegram: add "always use default lang", /lang default
For #451. Setting in integrations > chat bots > telegram makes jfa-go ignore the telegram users clients lanugage setting and always use whatever is set as the default language in jfa-go. Also added /lang default, to un-set a preferred language. the start message also now shows up on typing /help or !help too.
This commit is contained in:
@@ -1056,6 +1056,12 @@ sections:
|
||||
value: en-us
|
||||
description: Default telegram message language. Visit weblate if you'd like to
|
||||
translate.
|
||||
- setting: ignore_client_language
|
||||
name: Always use default language
|
||||
depends_true: enabled
|
||||
type: bool
|
||||
value: false
|
||||
description: When disabled, jfa-go will check the telegram user's language and use it if possible. Enable to ignore it and use your configured default language always.
|
||||
- section: matrix
|
||||
meta:
|
||||
name: Matrix
|
||||
|
||||
@@ -945,9 +945,9 @@
|
||||
<button class="button ~neutral @low center inside-input rounded-s-none settings-search-clear" aria-label="{{ .strings.clearSearch }}" text="{{ .strings.clearSearch }}"><i class="ri-close-line"></i></button>
|
||||
</div>
|
||||
<div id="settings-loader" class="flex flex-row flex-wrap gap-2">
|
||||
<span class="button ~neutral @low justify-center grow flex flex-row gap-2" id="setting-about"><span class="flex">{{ .strings.aboutProgram }} <i class="ri-information-line"></i></span></span>
|
||||
<a class="button ~urge dark:~d_info @low justify-center grow flex flex-row gap-2" target="_blank" href="https://wiki.jfa-go.com"><span class="flex">{{ .strings.wiki }} <i class="ri-book-shelf-line"></i></a>
|
||||
<span class="button ~neutral @low justify-center grow flex flex-row gap-2" id="setting-profiles"><span class="flex">{{ .strings.userProfiles }} <i class="ri-user-line"></i></span></span>
|
||||
<span class="button ~neutral @low justify-center grow flex flex-row gap-2" id="setting-about"><span class="flex flex-row gap-2">{{ .strings.aboutProgram }} <i class="ri-information-line"></i></span></span>
|
||||
<a class="button ~urge dark:~d_info @low justify-center grow flex flex-row gap-2" target="_blank" href="https://wiki.jfa-go.com"><span class="flex flex-row gap-2">{{ .strings.wiki }} <i class="ri-book-shelf-line"></i></a>
|
||||
<span class="button ~neutral @low justify-center grow flex flex-row gap-2" id="setting-profiles"><span class="flex flex-row gap-2">{{ .strings.userProfiles }} <i class="ri-user-line"></i></span></span>
|
||||
</div>
|
||||
<div class="flex md:flex flex-col gap-2 overflow-y-scroll" id="settings-sidebar-items"></div>
|
||||
</div>
|
||||
|
||||
64
telegram.go
64
telegram.go
@@ -59,15 +59,16 @@ type VerifToken struct {
|
||||
}
|
||||
|
||||
type TelegramDaemon struct {
|
||||
Stopped bool
|
||||
ShutdownChannel chan string
|
||||
bot *tg.BotAPI
|
||||
username string
|
||||
tokens map[string]VerifToken // Map of pins to tokens.
|
||||
verifiedTokens map[string]TelegramVerifiedToken // Map of token pins to the responsible ChatID+Username.
|
||||
languages map[int64]string // Store of languages for chatIDs. Added to on first interaction, and loaded from app.storage.telegram on start.
|
||||
link string
|
||||
app *appContext
|
||||
Stopped bool
|
||||
ShutdownChannel chan string
|
||||
bot *tg.BotAPI
|
||||
username string
|
||||
tokens map[string]VerifToken // Map of pins to tokens.
|
||||
verifiedTokens map[string]TelegramVerifiedToken // Map of token pins to the responsible ChatID+Username.
|
||||
languages map[int64]string // Store of languages for chatIDs. Added to on first interaction, and loaded from app.storage.telegram on start.
|
||||
ignoreClientLanguage bool // Whether to ignore the sender's client language and just use the preconfigured default language or not.
|
||||
link string
|
||||
app *appContext
|
||||
}
|
||||
|
||||
func newTelegramDaemon(app *appContext) (*TelegramDaemon, error) {
|
||||
@@ -80,14 +81,15 @@ func newTelegramDaemon(app *appContext) (*TelegramDaemon, error) {
|
||||
return nil, err
|
||||
}
|
||||
td := &TelegramDaemon{
|
||||
ShutdownChannel: make(chan string),
|
||||
bot: bot,
|
||||
username: bot.Self.UserName,
|
||||
tokens: map[string]VerifToken{},
|
||||
verifiedTokens: map[string]TelegramVerifiedToken{},
|
||||
languages: map[int64]string{},
|
||||
link: "https://t.me/" + bot.Self.UserName,
|
||||
app: app,
|
||||
ShutdownChannel: make(chan string),
|
||||
bot: bot,
|
||||
username: bot.Self.UserName,
|
||||
tokens: map[string]VerifToken{},
|
||||
verifiedTokens: map[string]TelegramVerifiedToken{},
|
||||
languages: map[int64]string{},
|
||||
ignoreClientLanguage: app.config.Section("telegram").Key("ignore_client_language").MustBool(false),
|
||||
link: "https://t.me/" + bot.Self.UserName,
|
||||
app: app,
|
||||
}
|
||||
for _, user := range app.storage.GetTelegram() {
|
||||
if user.Lang != "" {
|
||||
@@ -156,12 +158,14 @@ func (t *TelegramDaemon) run() {
|
||||
lang := t.app.storage.lang.chosenTelegramLang
|
||||
storedLang, ok := t.languages[upd.Message.Chat.ID]
|
||||
if !ok {
|
||||
found := false
|
||||
for code := range t.app.storage.lang.Telegram {
|
||||
if code[:2] == upd.Message.From.LanguageCode {
|
||||
lang = code
|
||||
found = true
|
||||
break
|
||||
found := t.ignoreClientLanguage
|
||||
if !found {
|
||||
for code := range t.app.storage.lang.Telegram {
|
||||
if code[:2] == upd.Message.From.LanguageCode {
|
||||
lang = code
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if found {
|
||||
@@ -172,6 +176,8 @@ func (t *TelegramDaemon) run() {
|
||||
}
|
||||
switch msg := sects[0]; msg {
|
||||
case "/start":
|
||||
case "/help":
|
||||
case "!help":
|
||||
t.commandStart(&upd, sects, lang)
|
||||
continue
|
||||
case "/lang":
|
||||
@@ -243,6 +249,7 @@ func (t *TelegramDaemon) commandStart(upd *tg.Update, sects []string, lang strin
|
||||
func (t *TelegramDaemon) commandLang(upd *tg.Update, sects []string, lang string) {
|
||||
if len(sects) == 1 {
|
||||
list := "/lang `<lang>`\n"
|
||||
list += "`default`: " + t.app.storage.lang.Telegram[t.app.storage.lang.chosenTelegramLang].Meta.Name + "\n"
|
||||
for code := range t.app.storage.lang.Telegram {
|
||||
list += fmt.Sprintf("`%s`: %s\n", code, t.app.storage.lang.Telegram[code].Meta.Name)
|
||||
}
|
||||
@@ -252,6 +259,17 @@ func (t *TelegramDaemon) commandLang(upd *tg.Update, sects []string, lang string
|
||||
}
|
||||
return
|
||||
}
|
||||
if sects[1] == "default" {
|
||||
delete(t.languages, upd.Message.Chat.ID)
|
||||
for _, user := range t.app.storage.GetTelegram() {
|
||||
if user.ChatID == upd.Message.Chat.ID {
|
||||
user.Lang = ""
|
||||
t.app.storage.SetTelegramKey(user.JellyfinID, user)
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
if _, ok := t.app.storage.lang.Telegram[sects[1]]; ok {
|
||||
t.languages[upd.Message.Chat.ID] = sects[1]
|
||||
for _, user := range t.app.storage.GetTelegram() {
|
||||
|
||||
Reference in New Issue
Block a user