diff --git a/tg/bot.go b/tg/bot.go index bda1a61..210e02f 100644 --- a/tg/bot.go +++ b/tg/bot.go @@ -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( diff --git a/tg/context.go b/tg/context.go index c13ef60..5a5ffde 100644 --- a/tg/context.go +++ b/tg/context.go @@ -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 } diff --git a/tg/inline.go b/tg/inline.go index 7c6c1ed..2f4fa5b 100644 --- a/tg/inline.go +++ b/tg/inline.go @@ -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 diff --git a/tg/keyboard.go b/tg/keyboard.go index 9263edc..392cae3 100644 --- a/tg/keyboard.go +++ b/tg/keyboard.go @@ -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 } diff --git a/tg/message.go b/tg/message.go index 754bb37..3f706e7 100644 --- a/tg/message.go +++ b/tg/message.go @@ -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 diff --git a/tg/reply.go b/tg/reply.go index 2ea4183..64e95a0 100644 --- a/tg/reply.go +++ b/tg/reply.go @@ -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 } diff --git a/tg/send.go b/tg/send.go index 07b471c..c379b2e 100644 --- a/tg/send.go +++ b/tg/send.go @@ -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) } diff --git a/tg/session.go b/tg/session.go index 6f31ce4..5d5709a 100644 --- a/tg/session.go +++ b/tg/session.go @@ -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) -}