2023-08-10 15:49:25 +03:00
|
|
|
package tx
|
2023-07-09 01:28:59 +03:00
|
|
|
|
2023-07-12 14:20:52 +03:00
|
|
|
import (
|
2023-07-12 14:59:07 +03:00
|
|
|
"fmt"
|
2023-08-10 15:49:25 +03:00
|
|
|
|
|
|
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2023-07-12 14:20:52 +03:00
|
|
|
)
|
|
|
|
|
2023-08-18 13:46:10 +03:00
|
|
|
type context struct {
|
2023-07-12 14:06:05 +03:00
|
|
|
*Session
|
2023-08-18 13:46:10 +03:00
|
|
|
*Bot
|
2023-08-12 14:35:33 +03:00
|
|
|
updates chan *Update
|
|
|
|
// Is true if currently reading the Update.
|
|
|
|
readingUpdate bool
|
2023-08-15 16:02:14 +03:00
|
|
|
|
|
|
|
curScreen, prevScreen *Screen
|
2023-08-12 14:35:33 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 13:46:10 +03:00
|
|
|
// The type represents way to interact with user in
|
|
|
|
// handling functions. Is provided to Act() function always.
|
|
|
|
|
2023-07-12 14:06:05 +03:00
|
|
|
// Goroutie function to handle each user.
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *context) handleUpdateChan(updates chan *Update) {
|
|
|
|
beh := c.behaviour
|
2023-08-16 17:25:56 +03:00
|
|
|
|
|
|
|
if beh.Init != nil {
|
|
|
|
c.run(beh.Init, nil)
|
|
|
|
}
|
2023-07-12 14:06:05 +03:00
|
|
|
for u := range updates {
|
2023-08-18 14:20:33 +03:00
|
|
|
var act Action
|
2023-08-15 16:02:14 +03:00
|
|
|
screen := c.curScreen
|
2023-08-11 10:41:17 +03:00
|
|
|
// The part is added to implement custom update handling.
|
2023-07-12 14:06:05 +03:00
|
|
|
if u.Message != nil {
|
2023-08-12 14:35:33 +03:00
|
|
|
if u.Message.IsCommand() && !c.readingUpdate {
|
|
|
|
cmdName := CommandName(u.Message.Command())
|
2023-08-13 15:37:36 +03:00
|
|
|
cmd, ok := beh.Commands[cmdName]
|
2023-08-12 14:35:33 +03:00
|
|
|
if ok {
|
|
|
|
act = cmd.Action
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-15 16:02:14 +03:00
|
|
|
kbd := screen.Keyboard
|
|
|
|
if kbd == nil {
|
|
|
|
if c.readingUpdate {
|
|
|
|
c.updates <- u
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2023-08-12 14:35:33 +03:00
|
|
|
btns := kbd.buttonMap()
|
|
|
|
text := u.Message.Text
|
|
|
|
btn, ok := btns[text]
|
2023-08-12 15:54:05 +03:00
|
|
|
if !ok {
|
|
|
|
if u.Message.Location != nil {
|
|
|
|
for _, b := range btns {
|
|
|
|
if b.SendLocation {
|
|
|
|
btn = b
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if c.readingUpdate {
|
|
|
|
// Skipping the update sending it to
|
|
|
|
// the reading goroutine.
|
|
|
|
c.updates <- u
|
|
|
|
continue
|
|
|
|
}
|
2023-08-12 14:35:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
act = btn.Action
|
|
|
|
}
|
2023-07-12 14:06:05 +03:00
|
|
|
}
|
|
|
|
} else if u.CallbackQuery != nil {
|
2023-08-18 14:20:33 +03:00
|
|
|
cb := apix.NewCallback(
|
|
|
|
u.CallbackQuery.ID,
|
|
|
|
u.CallbackQuery.Data,
|
|
|
|
)
|
2023-07-12 14:30:05 +03:00
|
|
|
data := u.CallbackQuery.Data
|
2023-08-10 15:49:25 +03:00
|
|
|
|
2023-08-18 13:46:10 +03:00
|
|
|
_, err := c.Request(cb)
|
2023-07-12 14:06:05 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-08-15 16:02:14 +03:00
|
|
|
kbd := screen.InlineKeyboard
|
|
|
|
if kbd == nil {
|
|
|
|
if c.readingUpdate {
|
|
|
|
c.updates <- u
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:30:05 +03:00
|
|
|
btns := kbd.buttonMap()
|
2023-08-12 15:54:05 +03:00
|
|
|
btn, ok := btns[data]
|
|
|
|
if !ok && c.readingUpdate {
|
|
|
|
c.updates <- u
|
|
|
|
continue
|
|
|
|
}
|
2023-08-15 16:02:14 +03:00
|
|
|
if !ok {
|
|
|
|
c.Sendf("%q", btns)
|
|
|
|
continue
|
|
|
|
}
|
2023-08-13 15:37:36 +03:00
|
|
|
act = btn.Action
|
|
|
|
}
|
|
|
|
if act != nil {
|
|
|
|
c.run(act, u)
|
2023-07-12 14:06:05 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *context) run(a Action, u *Update) {
|
|
|
|
go a.Act(&Context{
|
|
|
|
context: c,
|
|
|
|
Update: u,
|
2023-08-13 15:37:36 +03:00
|
|
|
})
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
// Returns the next update ignoring current screen.
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *context) ReadUpdate() (*Update, error) {
|
2023-08-11 12:45:50 +03:00
|
|
|
c.readingUpdate = true
|
|
|
|
u := <-c.updates
|
|
|
|
c.readingUpdate = false
|
|
|
|
if u == nil {
|
|
|
|
return nil, NotAvailableErr
|
2023-08-11 10:41:17 +03:00
|
|
|
}
|
|
|
|
|
2023-08-11 12:45:50 +03:00
|
|
|
return u, nil
|
2023-08-11 10:41:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the next text message that the user sends.
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *context) ReadTextMessage() (string, error) {
|
2023-08-11 10:41:17 +03:00
|
|
|
u, err := c.ReadUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if u.Message == nil {
|
|
|
|
return "", WrongUpdateType{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.Message.Text, nil
|
|
|
|
}
|
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
// Sends to the user specified text.
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *context) Send(v ...any) error {
|
2023-08-11 10:41:17 +03:00
|
|
|
msg := apix.NewMessage(c.Id.ToTelegram(), fmt.Sprint(v...))
|
2023-08-18 13:46:10 +03:00
|
|
|
_, err := c.Bot.Send(msg)
|
2023-07-12 14:59:07 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
// Sends the formatted with fmt.Sprintf message to the user.
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *context) Sendf(format string, v ...any) error {
|
2023-07-12 14:59:07 +03:00
|
|
|
return c.Send(fmt.Sprintf(format, v...))
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
2023-08-13 15:37:36 +03:00
|
|
|
|
|
|
|
// Context for interaction inside groups.
|
2023-08-18 13:46:10 +03:00
|
|
|
type groupContext struct {
|
2023-08-13 15:37:36 +03:00
|
|
|
*GroupSession
|
2023-08-18 13:46:10 +03:00
|
|
|
*Bot
|
2023-08-13 15:37:36 +03:00
|
|
|
updates chan *Update
|
|
|
|
}
|
|
|
|
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *groupContext) run(a GroupAction, u *Update) {
|
|
|
|
go a.Act(&GroupContext{
|
|
|
|
groupContext: c,
|
2023-08-13 15:37:36 +03:00
|
|
|
Update: u,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *groupContext) handleUpdateChan(updates chan *Update) {
|
2023-08-13 15:37:36 +03:00
|
|
|
var act GroupAction
|
2023-08-18 13:46:10 +03:00
|
|
|
beh := c.groupBehaviour
|
2023-08-13 15:37:36 +03:00
|
|
|
for u := range updates {
|
|
|
|
if u.Message != nil {
|
|
|
|
msg := u.Message
|
|
|
|
if msg.IsCommand() {
|
|
|
|
cmdName := CommandName(msg.Command())
|
|
|
|
|
|
|
|
// Skipping the commands sent not to us.
|
|
|
|
atName := msg.CommandWithAt()[len(cmdName)+1:]
|
2023-08-18 13:46:10 +03:00
|
|
|
if c.Bot.Me.UserName != atName {
|
2023-08-13 15:37:36 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmd, ok := beh.Commands[cmdName]
|
|
|
|
if !ok {
|
|
|
|
// Some lack of command handling
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
act = cmd.Action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if act != nil {
|
|
|
|
c.run(act, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *groupContext) Sendf(format string, v ...any) error {
|
2023-08-13 15:37:36 +03:00
|
|
|
return c.Send(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sends into the chat specified values converted to strings.
|
2023-08-18 13:46:10 +03:00
|
|
|
func (c *groupContext) Send(v ...any) error {
|
2023-08-13 15:37:36 +03:00
|
|
|
msg := apix.NewMessage(c.Id.ToTelegram(), fmt.Sprint(v...))
|
2023-08-18 13:46:10 +03:00
|
|
|
_, err := c.Bot.Send(msg)
|
2023-08-13 15:37:36 +03:00
|
|
|
return err
|
|
|
|
}
|