2023-08-19 09:12:26 +03:00
|
|
|
package tg
|
2023-09-25 19:58:59 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-12 12:31:38 +03:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2023-09-25 19:58:59 +03:00
|
|
|
//"path"
|
2024-07-23 16:08:39 +03:00
|
|
|
//"log"
|
2023-09-25 19:58:59 +03:00
|
|
|
)
|
|
|
|
|
2023-12-19 22:35:57 +03:00
|
|
|
|
2023-10-11 13:41:08 +03:00
|
|
|
// Interface to interact with the user.
|
|
|
|
type Context struct {
|
2024-03-29 14:30:48 +03:00
|
|
|
// The session contains all
|
|
|
|
// the information between the contexts.
|
2024-03-28 10:41:09 +03:00
|
|
|
session *Session
|
2023-10-11 13:41:08 +03:00
|
|
|
// The update that called the Context usage.
|
2024-03-28 10:41:09 +03:00
|
|
|
update Update
|
2023-10-11 13:41:08 +03:00
|
|
|
// Used as way to provide outer values redirection
|
|
|
|
// into widgets and actions. It is like arguments
|
|
|
|
// for REST API request etc.
|
|
|
|
arg any
|
2024-03-28 10:41:09 +03:00
|
|
|
typ ContextType
|
2023-10-11 13:41:08 +03:00
|
|
|
// Instead of updates as argument.
|
|
|
|
input *UpdateChan
|
|
|
|
}
|
|
|
|
|
2023-12-20 19:58:10 +03:00
|
|
|
// Run commands as other user. Was implemented to
|
|
|
|
// make other user to leave the bot at first but
|
|
|
|
// maybe you will find another usage for this.
|
|
|
|
// Returns users context by specified session ID
|
2024-03-29 14:30:48 +03:00
|
|
|
// or false if the user is not logged in.
|
2024-07-21 16:02:47 +03:00
|
|
|
func (c Context) As(sid SessionID) (Context, bool) {
|
2024-03-29 14:30:48 +03:00
|
|
|
s, ok := c.Bot().GotSession(sid)
|
2023-12-20 19:58:10 +03:00
|
|
|
if !ok {
|
2024-03-29 14:30:48 +03:00
|
|
|
return Context{}, false
|
2023-12-20 19:58:10 +03:00
|
|
|
}
|
2024-03-29 14:30:48 +03:00
|
|
|
|
|
|
|
c.session = s
|
|
|
|
return c, true
|
2023-12-20 19:58:10 +03:00
|
|
|
}
|
|
|
|
|
2023-09-26 17:13:31 +03:00
|
|
|
// General type function to define actions, single component widgets
|
|
|
|
// and components themselves.
|
2024-03-28 10:41:09 +03:00
|
|
|
type Func func(Context)
|
|
|
|
func (f Func) Act(c Context) {
|
2023-09-25 23:43:22 +03:00
|
|
|
f(c)
|
|
|
|
}
|
2024-03-28 10:41:09 +03:00
|
|
|
func (f Func) Serve(c Context) {
|
2023-09-25 23:43:22 +03:00
|
|
|
f(c)
|
|
|
|
}
|
2024-03-29 14:30:48 +03:00
|
|
|
func(f Func) Filter(_ Update) bool {
|
2023-09-26 17:13:31 +03:00
|
|
|
return false
|
|
|
|
}
|
2024-03-29 14:30:48 +03:00
|
|
|
func (f Func) Render(_ Context) UI {
|
2023-09-26 17:13:31 +03:00
|
|
|
return UI{
|
|
|
|
f,
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 19:58:59 +03:00
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
// The type represents type
|
|
|
|
// of current context the processing is happening
|
|
|
|
// in.
|
2023-09-25 19:58:59 +03:00
|
|
|
type ContextType uint8
|
|
|
|
const (
|
|
|
|
NoContextType ContextType = iota
|
|
|
|
WidgetContextType
|
|
|
|
ActionContextType
|
|
|
|
)
|
|
|
|
|
|
|
|
// Goroutie function to handle each user.
|
2024-03-28 10:41:09 +03:00
|
|
|
func (c Context) serve() {
|
2024-03-29 14:30:48 +03:00
|
|
|
beh := c.Bot().behaviour
|
2023-09-26 17:13:31 +03:00
|
|
|
c.Run(beh.Init)
|
2024-07-21 16:02:47 +03:00
|
|
|
for {
|
|
|
|
defer func(){
|
|
|
|
if err := recover() ; err != nil {
|
|
|
|
// Need to add some handling later.
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
beh.Root.Serve(c)
|
|
|
|
}
|
2023-09-25 19:58:59 +03:00
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (c Context) Arg() any {
|
2023-10-11 13:41:08 +03:00
|
|
|
return c.arg
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (c Context) Run(a Action) {
|
2023-09-25 19:58:59 +03:00
|
|
|
if a != nil {
|
2023-09-27 14:09:49 +03:00
|
|
|
a.Act(c)
|
2023-09-25 19:58:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
// Sends to the Sendable object to the session user.
|
|
|
|
func (c Context) Send(v Sendable) (*Message, error) {
|
2024-07-21 16:02:47 +03:00
|
|
|
config := v.SendConfig(c.SessionID(), c.Bot())
|
2023-09-29 13:36:37 +03:00
|
|
|
if config.Error != nil {
|
|
|
|
return nil, config.Error
|
|
|
|
}
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
msg, err := c.Bot().API().Send(config.ToAPI())
|
2023-09-29 13:36:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &msg, nil
|
2023-09-25 19:58:59 +03:00
|
|
|
}
|
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
// Sends the formatted with fmt.Sprintf message to the user
|
|
|
|
// using default Markdown parsing format.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (c Context) Sendf(format string, v ...any) (*Message, error) {
|
|
|
|
return c.Send(Messagef(format, v...))
|
2023-09-25 19:58:59 +03:00
|
|
|
}
|
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
// Same as Sendf but uses Markdown 2 format for parsing.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (c Context) Sendf2(format string, v ...any) (*Message, error) {
|
|
|
|
return c.Send(Messagef(format, v...).MD2())
|
2023-09-25 19:58:59 +03:00
|
|
|
}
|
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
// Same as Sendf but uses HTML format for parsing.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (c Context) SendfHTML(format string, v ...any) (*Message, error) {
|
|
|
|
return c.Send(Messagef(format, v...).HTML())
|
2023-09-25 19:58:59 +03:00
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
// Send the message in raw format escaping all the special characters.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (c Context) SendfR(format string, v ...any) (*Message, error) {
|
|
|
|
return c.Send(Messagef("%s", Escape2(fmt.Sprintf(format, v...))).MD2())
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
2023-09-25 19:58:59 +03:00
|
|
|
|
|
|
|
// Get the input for current widget.
|
|
|
|
// Should be used inside handlers (aka "Serve").
|
2024-03-28 10:41:09 +03:00
|
|
|
func (c Context) Input() chan Update {
|
2023-09-25 19:58:59 +03:00
|
|
|
return c.input.Chan()
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (c Context) WithArg(v any) Context {
|
2023-10-11 13:41:08 +03:00
|
|
|
c.arg = v
|
2023-09-25 19:58:59 +03:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
func (c Context) WithUpdate(u Update) Context {
|
|
|
|
c.update = u
|
2023-09-25 19:58:59 +03:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
func (c Context) WithInput(input *UpdateChan) Context {
|
2023-09-25 19:58:59 +03:00
|
|
|
c.input = input
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Customized actions for the bot.
|
|
|
|
type Action interface {
|
2024-03-28 10:41:09 +03:00
|
|
|
Act(Context)
|
2023-09-25 19:58:59 +03:00
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
type ActionFunc func(Context)
|
2023-09-25 19:58:59 +03:00
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
func (af ActionFunc) Act(c Context) {
|
2023-09-25 19:58:59 +03:00
|
|
|
af(c)
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
// Simple way to read strings for widgets with
|
|
|
|
// the specified prompt.
|
|
|
|
func (c Context) ReadString(promptf string, args ...any) string {
|
2023-09-25 19:58:59 +03:00
|
|
|
var text string
|
2024-03-29 14:30:48 +03:00
|
|
|
if promptf != "" {
|
2024-03-28 10:41:09 +03:00
|
|
|
c.Sendf(promptf, args...)
|
2023-10-02 21:45:21 +03:00
|
|
|
}
|
2023-09-25 19:58:59 +03:00
|
|
|
for u := range c.Input() {
|
|
|
|
if u.Message == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
text = u.Message.Text
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
2024-07-25 17:48:02 +03:00
|
|
|
func (c Context) Update(updater Updater) error {
|
|
|
|
return updater.Update(c)
|
|
|
|
}
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
func (c Context) CallbackUpdate() *Update {
|
|
|
|
return &c.update
|
2024-03-28 10:41:09 +03:00
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
// Returns the reader for specified file ID and path.
|
2024-07-21 16:02:47 +03:00
|
|
|
func (c Context) GetFile(fileID FileID) (io.ReadCloser, string, error) {
|
|
|
|
file, err := c.Bot().API().GetFile(tgbotapi.FileConfig{
|
|
|
|
FileID: string(fileID),
|
|
|
|
})
|
2023-12-12 12:31:38 +03:00
|
|
|
if err != nil {
|
2023-12-25 17:35:00 +03:00
|
|
|
return nil, "", err
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
r, err := http.Get(fmt.Sprintf(
|
|
|
|
"https://api.telegram.org/file/bot%s/%s",
|
2024-07-21 16:02:47 +03:00
|
|
|
c.Bot().API().Token,
|
2023-12-12 12:31:38 +03:00
|
|
|
file.FilePath,
|
|
|
|
))
|
|
|
|
if err != nil {
|
2023-12-25 17:35:00 +03:00
|
|
|
return nil, "", err
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
if r.StatusCode != 200 {
|
2023-12-25 17:35:00 +03:00
|
|
|
return nil, "", StatusCodeErr
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
return r.Body, file.FilePath, nil
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
// Reads all the content from the specified file.
|
2024-07-21 16:02:47 +03:00
|
|
|
func (c Context) ReadFile(fileID FileID) ([]byte, string, error) {
|
|
|
|
file, pth, err := c.GetFile(fileID)
|
2023-12-12 12:31:38 +03:00
|
|
|
if err != nil {
|
2023-12-25 17:35:00 +03:00
|
|
|
return nil, "", err
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
bts, err := io.ReadAll(file)
|
|
|
|
if err != nil {
|
2023-12-25 17:35:00 +03:00
|
|
|
return nil, "", err
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
|
2023-12-25 17:35:00 +03:00
|
|
|
return bts, pth, nil
|
2023-12-12 12:31:38 +03:00
|
|
|
}
|
|
|
|
|
2024-07-23 15:41:31 +03:00
|
|
|
func (c Context) RunCompo(compo Component) (*UpdateChan, error) {
|
2024-03-29 14:30:48 +03:00
|
|
|
if compo == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
sendable, canSend := compo.(Sendable)
|
|
|
|
if canSend {
|
|
|
|
msg, err := c.Send(sendable)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sendable.SetMessage(msg)
|
|
|
|
}
|
|
|
|
updates := NewUpdateChan()
|
|
|
|
go func() {
|
|
|
|
compo.Serve(
|
2024-07-23 15:41:31 +03:00
|
|
|
c.WithInput(updates),
|
2024-03-29 14:30:48 +03:00
|
|
|
)
|
|
|
|
// To let widgets finish themselves before
|
|
|
|
// the channel is closed and close it by themselves.
|
|
|
|
updates.Close()
|
|
|
|
}()
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run widget in background returning the new input channel for it.
|
2024-07-23 15:41:31 +03:00
|
|
|
func (c Context) RunWidget(widget Widget) (*UpdateChan, error) {
|
2024-03-29 14:30:48 +03:00
|
|
|
var err error
|
|
|
|
if widget == nil {
|
|
|
|
return nil, EmptyWidgetErr
|
|
|
|
}
|
|
|
|
|
2024-07-23 15:41:31 +03:00
|
|
|
compos := widget.Render(c)
|
2024-03-29 14:30:48 +03:00
|
|
|
// Leave if changed path or components are empty.
|
2024-07-21 16:02:47 +03:00
|
|
|
if compos == nil {
|
2024-03-29 14:30:48 +03:00
|
|
|
return nil, EmptyCompoErr
|
|
|
|
}
|
|
|
|
chns := make([]*UpdateChan, len(compos))
|
|
|
|
for i, compo := range compos {
|
2024-07-23 15:41:31 +03:00
|
|
|
chns[i], err = c.RunCompo(compo)
|
2024-03-29 14:30:48 +03:00
|
|
|
if err != nil {
|
|
|
|
for _, chn := range chns {
|
|
|
|
chn.Close()
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := NewUpdateChan()
|
|
|
|
go func() {
|
|
|
|
ln := len(compos)
|
|
|
|
//ation: u != nil (mismatchedtypes Update and untyped nil)
|
|
|
|
UPDATE:
|
|
|
|
for u := range ret.Chan() {
|
|
|
|
cnt := 0
|
|
|
|
for i, compo := range compos {
|
|
|
|
chn := chns[i]
|
|
|
|
if chn.Closed() {
|
|
|
|
cnt++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !compo.Filter(u) {
|
|
|
|
chn.Send(u)
|
|
|
|
continue UPDATE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cnt == ln {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.Close()
|
|
|
|
for _, chn := range chns {
|
|
|
|
chn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2024-07-23 15:47:24 +03:00
|
|
|
func (c Context) GoRet(pth Widget) UI {
|
|
|
|
return UI{WidgetGo{
|
2024-07-23 15:41:31 +03:00
|
|
|
Path: pth,
|
|
|
|
Arg: c.Arg(),
|
2024-07-23 15:47:24 +03:00
|
|
|
}}
|
2024-03-29 14:30:48 +03:00
|
|
|
}
|
|
|
|
|
2024-07-23 15:41:31 +03:00
|
|
|
// Go to the specified widget
|
|
|
|
// using context values.
|
|
|
|
func (c Context) Go(pth Widget) error {
|
2024-03-29 14:30:48 +03:00
|
|
|
var err error
|
2024-07-21 16:02:47 +03:00
|
|
|
if pth == nil {
|
|
|
|
c.session.pathHistory = []Widget{}
|
2024-03-29 14:30:48 +03:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-21 16:02:47 +03:00
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
var back bool
|
2024-07-21 16:02:47 +03:00
|
|
|
if pth == Back {
|
2024-07-23 16:08:39 +03:00
|
|
|
if len(c.session.pathHistory) <= 1 {
|
2024-07-23 15:41:31 +03:00
|
|
|
return c.Go(nil)
|
2024-03-29 14:30:48 +03:00
|
|
|
}
|
|
|
|
pth = c.session.pathHistory[len(c.session.pathHistory)-2]
|
2024-07-21 16:02:47 +03:00
|
|
|
c.session.pathHistory =
|
|
|
|
c.session.pathHistory[:len(c.session.pathHistory)-1]
|
|
|
|
back = true
|
2024-03-29 14:30:48 +03:00
|
|
|
}
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
if !back {
|
2024-03-29 14:30:48 +03:00
|
|
|
c.session.pathHistory = append(c.session.pathHistory, pth)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stopping the current widget.
|
|
|
|
c.session.skippedUpdates.Close()
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
// Running the new one.
|
2024-07-23 15:41:31 +03:00
|
|
|
c.session.skippedUpdates, err = c.RunWidget(pth)
|
2024-07-21 16:02:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-03-29 14:30:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Context) Session() Session {
|
|
|
|
return *c.session
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Context) SetSessionData(v any) {
|
|
|
|
c.session.Data = v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Context) SessionData() any {
|
|
|
|
return c.session.Data
|
|
|
|
}
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
func (c Context) SessionID() SessionID {
|
|
|
|
return c.session.ID
|
2024-03-29 14:30:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Context) SessionScope() SessionScope {
|
|
|
|
return c.session.Scope
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only for the root widget usage.
|
|
|
|
// Skip the update sending it down to
|
|
|
|
// the underlying widget.
|
|
|
|
func (c Context) SkipUpdate(u Update) {
|
|
|
|
c.session.skippedUpdates.Send(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the session related bot.
|
|
|
|
func (c Context) Bot() *Bot {
|
|
|
|
return c.session.bot
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return context's session's path history.
|
2024-07-21 16:02:47 +03:00
|
|
|
func (c Context) PathHistory() []Widget {
|
2024-03-29 14:30:48 +03:00
|
|
|
return c.session.pathHistory
|
|
|
|
}
|
|
|
|
|
2024-07-24 06:50:45 +03:00
|
|
|
func (c Context) SetPathHistory(hist []Widget) {
|
|
|
|
c.session.pathHistory = hist
|
|
|
|
}
|
|
|
|
|
2024-07-21 16:02:47 +03:00
|
|
|
func (c Context) Path() Widget {
|
2024-03-29 14:30:48 +03:00
|
|
|
ln := len(c.session.pathHistory)
|
|
|
|
if ln == 0 {
|
2024-07-21 16:02:47 +03:00
|
|
|
return nil
|
2024-03-29 14:30:48 +03:00
|
|
|
}
|
|
|
|
return c.session.pathHistory[ln-1]
|
|
|
|
}
|