Fixed the starting problem.

This commit is contained in:
Andrey Parhomenko 2023-08-19 13:25:47 +03:00
parent 9a1202d777
commit cdc1a737b7

View file

@ -146,35 +146,41 @@ func (bot *Bot) handlePrivate(updates chan *Update) {
var sid SessionId var sid SessionId
for u := range updates { for u := range updates {
sid = SessionId(u.FromChat().ID) sid = SessionId(u.FromChat().ID)
var sessionOk, chnOk bool
// Create new session if the one does not exist // Create new session if the one does not exist
// for this user. // for this user.
if _, sessionOk = bot.sessions[sid]; !sessionOk {
bot.sessions.Add(sid)
}
_, chnOk = chans[sid]
// Making the bot ignore anything except "start" // Making the bot ignore anything except "start"
// before the session started // before the session started
if u.Message.IsCommand() && !sessionOk { session, sessionOk := bot.sessions[sid]
cmdName := CommandName(u.Message.Command()) chn, chnOk := chans[sid]
if cmdName == "start" { if sessionOk {
session := bot.sessions[sid] if !chnOk {
ctx := &context{ ctx := &context{
Bot: bot, Bot: bot,
Session: session, Session: session,
updates: make(chan *Update), updates: make(chan *Update),
} }
chn := make(chan *Update)
// Starting the new goroutine if chans[sid] = chn
// there is no one. go ctx.handleUpdateChan(chn)
if !chnOk { }
} else {
if u.Message != nil && u.Message.Command() == "start" {
if !sessionOk {
bot.sessions.Add(sid)
}
lsession := bot.sessions[sid]
ctx := &context{
Bot: bot,
Session: lsession,
updates: make(chan *Update),
}
chn := make(chan *Update) chn := make(chan *Update)
chans[sid] = chn chans[sid] = chn
go ctx.handleUpdateChan(chn) go ctx.handleUpdateChan(chn)
} }
} }
}
chn, ok := chans[sid] chn, ok := chans[sid]
// The bot MUST get the "start" command. // The bot MUST get the "start" command.
// It will do nothing otherwise. // It will do nothing otherwise.