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
// SessionId represents both for chat IDs.
func (bot *Bot) Send(
sid SessionId, v Sendable, args ...any,
sid SessionId, v Sendable,
) (*Message, error) {
ctx, ok := bot.contexts[sid]
if !ok {
@ -65,6 +65,17 @@ func (bot *Bot) Send(
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
// including private, group and channel.
func (bot *Bot) GetSession(

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -10,11 +10,9 @@ const (
ChannelSessionScope
)
type ChatId int64
// Represents unique value to identify chats.
// In fact is simply ID of the chat.
type SessionId = ChatId
type SessionId int64
// Convert the SessionId to Telegram API's type.
func (si SessionId) ToApi() int64 {
@ -50,23 +48,3 @@ func (sm SessionMap) Add(sid SessionId, scope SessionScope) *Session {
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)
}