@ -10,13 +10,20 @@ import (
tg "github.com/go-telegram-bot-api/telegram-bot-api"
tg "github.com/go-telegram-bot-api/telegram-bot-api"
)
)
type VerifiedToken struct {
Token string
ChatID int64
Username string
}
type TelegramDaemon struct {
type TelegramDaemon struct {
Stopped bool
Stopped bool
ShutdownChannel chan string
ShutdownChannel chan string
bot * tg . BotAPI
bot * tg . BotAPI
username string
username string
tokens [ ] string
tokens [ ] string
verifiedTokens [ ] string
verifiedTokens [ ] VerifiedToken
languages map [ int64 ] string // Store of languages for chatIDs. Added to on first interaction, and loaded from app.storage.telegram on start.
link string
link string
app * appContext
app * appContext
}
}
@ -30,16 +37,23 @@ func newTelegramDaemon(app *appContext) (*TelegramDaemon, error) {
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
return & TelegramDaemon {
td := & TelegramDaemon {
Stopped : false ,
Stopped : false ,
ShutdownChannel : make ( chan string ) ,
ShutdownChannel : make ( chan string ) ,
bot : bot ,
bot : bot ,
username : bot . Self . UserName ,
username : bot . Self . UserName ,
tokens : [ ] string { } ,
tokens : [ ] string { } ,
verifiedTokens : [ ] string { } ,
verifiedTokens : [ ] VerifiedToken { } ,
languages : map [ int64 ] string { } ,
link : "https://t.me/" + bot . Self . UserName ,
link : "https://t.me/" + bot . Self . UserName ,
app : app ,
app : app ,
} , nil
}
for _ , user := range app . storage . telegram {
if user . Lang != "" {
td . languages [ user . ChatID ] = user . Lang
}
}
return td , nil
}
}
var runes = [ ] rune ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
var runes = [ ] rune ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
@ -80,28 +94,21 @@ func (t *TelegramDaemon) run() {
continue
continue
}
}
lang := t . app . storage . lang . chosenTelegramLang
lang := t . app . storage . lang . chosenTelegramLang
user, ok := t . app . storage . telegram [ upd . Message . Chat . ID ]
storedLang, ok := t . languages [ upd . Message . Chat . ID ]
if ! ok {
if ! ok {
user := TelegramUser {
found := false
Username : upd . Message . Chat . UserName ,
ChatID : upd . Message . Chat . ID ,
Lang : "" ,
}
t . app . storage . telegram [ upd . Message . Chat . ID ] = user
err := t . app . storage . storeTelegramUsers ( )
if err != nil {
t . app . err . Printf ( "Failed to store Telegram users: %v" , err )
}
}
if user . Lang != "" {
lang = user . Lang
} else {
for code := range t . app . storage . lang . Telegram {
for code := range t . app . storage . lang . Telegram {
if code [ : 2 ] == upd . Message . From . LanguageCode {
if code [ : 2 ] == upd . Message . From . LanguageCode {
lang = code
lang = code
found = true
break
break
}
}
}
}
if found {
t . languages [ upd . Message . Chat . ID ] = lang
}
} else {
lang = storedLang
}
}
switch msg := sects [ 0 ] ; msg {
switch msg := sects [ 0 ] ; msg {
case "/start" :
case "/start" :
@ -125,11 +132,17 @@ func (t *TelegramDaemon) run() {
continue
continue
}
}
if _ , ok := t . app . storage . lang . Telegram [ sects [ 1 ] ] ; ok {
if _ , ok := t . app . storage . lang . Telegram [ sects [ 1 ] ] ; ok {
user . Lang = sects [ 1 ]
t . languages [ upd . Message . Chat . ID ] = sects [ 1 ]
t . app . storage . telegram [ upd . Message . Chat . ID ] = user
for jfID , user := range t . app . storage . telegram {
err := t . app . storage . storeTelegramUsers ( )
if user . ChatID == upd . Message . Chat . ID {
if err != nil {
user . Lang = sects [ 1 ]
t . app . err . Printf ( "Failed to store Telegram users: %v" , err )
t . app . storage . telegram [ jfID ] = user
err := t . app . storage . storeTelegramUsers ( )
if err != nil {
t . app . err . Printf ( "Failed to store Telegram users: %v" , err )
}
break
}
}
}
}
}
continue
continue
@ -148,11 +161,15 @@ func (t *TelegramDaemon) run() {
}
}
continue
continue
}
}
err := t . QuoteReply ( & upd , t . app . storage . lang . Telegram [ lang ] . Strings . get ( " s uccess") )
err := t . QuoteReply ( & upd , t . app . storage . lang . Telegram [ lang ] . Strings . get ( " pinS uccess") )
if err != nil {
if err != nil {
t . app . err . Printf ( "Telegram: Failed to send message to \"%s\": %v" , upd . Message . From . UserName , err )
t . app . err . Printf ( "Telegram: Failed to send message to \"%s\": %v" , upd . Message . From . UserName , err )
}
}
t . verifiedTokens = append ( t . verifiedTokens , upd . Message . Text )
t . verifiedTokens = append ( t . verifiedTokens , VerifiedToken {
Token : upd . Message . Text ,
ChatID : upd . Message . Chat . ID ,
Username : upd . Message . Chat . UserName ,
} )
t . tokens [ len ( t . tokens ) - 1 ] , t . tokens [ tokenIndex ] = t . tokens [ tokenIndex ] , t . tokens [ len ( t . tokens ) - 1 ]
t . tokens [ len ( t . tokens ) - 1 ] , t . tokens [ tokenIndex ] = t . tokens [ tokenIndex ] , t . tokens [ len ( t . tokens ) - 1 ]
t . tokens = t . tokens [ : len ( t . tokens ) - 1 ]
t . tokens = t . tokens [ : len ( t . tokens ) - 1 ]
}
}