Changed interface Sendable to make it work without context.

This commit is contained in:
Andrey Parhomenko 2023-10-10 12:42:05 +03:00
parent 1f1b15d8a2
commit daeede9315
8 changed files with 30 additions and 35 deletions

View file

@ -52,7 +52,7 @@ func (bot *Bot) Debug(debug bool) *Bot {
// Can be used for both group and private sessions because // Can be used for both group and private sessions because
// SessionId represents both for chat IDs. // SessionId represents both for chat IDs.
func (bot *Bot) Send( func (bot *Bot) Send(
sid SessionId, v Sendable, args ...any, sid SessionId, v Sendable,
) (*Message, error) { ) (*Message, error) {
ctx, ok := bot.contexts[sid] ctx, ok := bot.contexts[sid]
if !ok { if !ok {
@ -65,6 +65,17 @@ func (bot *Bot) Send(
return c.Bot.Send(c.Session.Id, v) return c.Bot.Send(c.Session.Id, v)
} }
// Send to the session specified its ID raw chattable from the tgbotapi.
func (bot *Bot) SendRaw(
sid SessionId, v tgbotapi.Chattable,
) (*Message, error) {
msg, err := bot.Api.Send(v)
if err != nil {
return nil, err
}
return &msg, nil
}
// Get session by its ID. Can be used for any scope // Get session by its ID. Can be used for any scope
// including private, group and channel. // including private, group and channel.
func (bot *Bot) GetSession( func (bot *Bot) GetSession(

View file

@ -84,7 +84,7 @@ func (c *Context) Skip(u *Update) {
// Sends to the Sendable object. // Sends to the Sendable object.
func (c *Context) Send(v Sendable) (*Message, error) { func (c *Context) Send(v Sendable) (*Message, error) {
config := v.SendConfig(c) config := v.SendConfig(c.Session.Id, c.Bot)
if config.Error != nil { if config.Error != nil {
return nil, config.Error return nil, config.Error
} }

View file

@ -31,10 +31,9 @@ type InlineCompo struct {
// Implementing the Sendable interface. // Implementing the Sendable interface.
func (compo *InlineCompo) SendConfig( func (compo *InlineCompo) SendConfig(
c *Context, sid SessionId, bot *Bot,
) (*SendConfig) { ) (*SendConfig) {
sendConfig := compo.MessageCompo.SendConfig(sid, bot)
sendConfig := compo.MessageCompo.SendConfig(c)
sendConfig.Message.ReplyMarkup = compo.Inline.ToApi() sendConfig.Message.ReplyMarkup = compo.Inline.ToApi()
return sendConfig return sendConfig

View file

@ -16,7 +16,11 @@ type Keyboard struct {
// Returns the new keyboard with specified rows. // Returns the new keyboard with specified rows.
func NewKeyboard(rows ...ButtonRow) *Keyboard { func NewKeyboard(rows ...ButtonRow) *Keyboard {
ret := &Keyboard{} ret := &Keyboard{}
ret.Rows = rows for _, row := range rows {
if row != nil && len(row) > 0 {
ret.Rows = append(ret.Rows, row)
}
}
return ret return ret
} }
@ -33,7 +37,10 @@ func (kbd *Keyboard) Row(btns ...*Button) *Keyboard {
} }
retBtns = append(retBtns, btn) retBtns = append(retBtns, btn)
} }
// Add only if there is something to add.
if len(retBtns) > 0 {
kbd.Rows = append(kbd.Rows, retBtns) kbd.Rows = append(kbd.Rows, retBtns)
}
return kbd return kbd
} }

View file

@ -78,7 +78,7 @@ func (msg *MessageCompo) Reply(reply *Reply) *ReplyCompo {
} }
func (config *MessageCompo) SendConfig( func (config *MessageCompo) SendConfig(
c *Context, sid SessionId, bot *Bot,
) (*SendConfig) { ) (*SendConfig) {
var ( var (
ret SendConfig ret SendConfig
@ -93,7 +93,7 @@ func (config *MessageCompo) SendConfig(
//text = strings.ReplaceAll(text, "-", "\\-") //text = strings.ReplaceAll(text, "-", "\\-")
msg := tgbotapi.NewMessage(c.Session.Id.ToApi(), text) msg := tgbotapi.NewMessage(sid.ToApi(), text)
ret.Message = &msg ret.Message = &msg
ret.Message.ParseMode = config.ParseMode ret.Message.ParseMode = config.ParseMode

View file

@ -58,9 +58,9 @@ type ReplyCompo struct {
// Implementing the sendable interface. // Implementing the sendable interface.
func (compo *ReplyCompo) SendConfig( func (compo *ReplyCompo) SendConfig(
c *Context, sid SessionId, bot *Bot,
) (*SendConfig) { ) (*SendConfig) {
sendConfig := compo.MessageCompo.SendConfig(c) sendConfig := compo.MessageCompo.SendConfig(sid, bot)
sendConfig.Message.ReplyMarkup = compo.Reply.ToApi() sendConfig.Message.ReplyMarkup = compo.Reply.ToApi()
return sendConfig return sendConfig
} }

View file

@ -10,7 +10,7 @@ type MessageId int64
// way to define what message will be // way to define what message will be
// sent to the side of a user. // sent to the side of a user.
type Sendable interface { type Sendable interface {
SendConfig(*Context) (*SendConfig) SendConfig(SessionId, *Bot) (*SendConfig)
SetMessage(*Message) SetMessage(*Message)
} }

View file

@ -10,11 +10,9 @@ const (
ChannelSessionScope ChannelSessionScope
) )
type ChatId int64
// Represents unique value to identify chats. // Represents unique value to identify chats.
// In fact is simply ID of the chat. // In fact is simply ID of the chat.
type SessionId = ChatId type SessionId int64
// Convert the SessionId to Telegram API's type. // Convert the SessionId to Telegram API's type.
func (si SessionId) ToApi() int64 { func (si SessionId) ToApi() int64 {
@ -50,23 +48,3 @@ func (sm SessionMap) Add(sid SessionId, scope SessionScope) *Session {
return ret return ret
} }
// Session information for a group.
type GroupSession struct {
Id SessionId
// Information for each user in the group.
Data any
}
// Returns new empty group session with specified group and user IDs.
func NewGroupSession(id SessionId) *GroupSession {
return &GroupSession{
Id: id,
}
}
// Map for every group the bot is in.
type GroupSessionMap map[SessionId]*GroupSession
func (sm GroupSessionMap) Add(sid SessionId) {
sm[sid] = NewGroupSession(sid)
}