diff --git a/cmd/test/main.go b/cmd/test/main.go index 8cbb01a..455d9df 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -30,7 +30,7 @@ func NewMutateMessageWidget(fn func(string) string) *MutateMessageWidget { } func (w *MutateMessageWidget) Serve(c *tg.Context) { - args, ok := c.Arg.([]any) + args, ok := c.Arg().([]any) if ok { for _, arg := range args { c.Sendf("%v", arg) diff --git a/tg/context.go b/tg/context.go index 5a5ffde..7a4343e 100644 --- a/tg/context.go +++ b/tg/context.go @@ -7,6 +7,36 @@ import ( //"path" ) +// General context for a specific user. +// Is always the same and is not reached +// inside end function-handlers. +type context struct { + Session *Session + // To reach the bot abilities inside callbacks. + Bot *Bot + // Costum status for currently running context. + Status any + Type ContextType + updates *UpdateChan + skippedUpdates *UpdateChan + // Current screen ID. + pathHistory []Path + //path, prevPath Path +} + +// Interface to interact with the user. +type Context struct { + *context + // The update that called the Context usage. + *Update + // Used as way to provide outer values redirection + // into widgets and actions. It is like arguments + // for REST API request etc. + arg any + // Instead of updates as argument. + input *UpdateChan +} + // General type function to define actions, single component widgets // and components themselves. type Func func(*Context) @@ -32,23 +62,6 @@ const ( ActionContextType ) -// General context for a specific user. -// Is always the same and is not reached -// inside end function-handlers. -type context struct { - Session *Session - // To reach the bot abilities inside callbacks. - Bot *Bot - // Costum status for currently running context. - Status any - Type ContextType - updates *UpdateChan - skippedUpdates *UpdateChan - // Current screen ID. - pathHistory []Path - //path, prevPath Path -} - // Goroutie function to handle each user. func (c *Context) serve() { beh := c.Bot.behaviour @@ -56,11 +69,6 @@ func (c *Context) serve() { beh.Root.Serve(c) } - -func (c *context) run(a Action, u *Update) { - a.Act(&Context{context: c, Update: u}) -} - func (c *Context) Path() Path { ln := len(c.pathHistory) if ln == 0 { @@ -69,6 +77,10 @@ func (c *Context) Path() Path { return c.pathHistory[ln-1] } +func (c *Context) Arg() any { + return c.arg +} + func (c *Context) Run(a Action) { if a != nil { a.Act(c) @@ -112,18 +124,6 @@ func (c *Context) SendfHTML(format string, v ...any) (*Message, error) { return c.Send(NewMessage(fmt.Sprintf(format, v...)).HTML()) } -// Interface to interact with the user. -type Context struct { - *context - // The update that called the Context usage. - *Update - // Used as way to provide outer values redirection - // into widgets and actions. It is like arguments - // for REST API request etc. - Arg any - // Instead of updates as argument. - input *UpdateChan -} // Get the input for current widget. // Should be used inside handlers (aka "Serve"). @@ -143,7 +143,7 @@ func (c *Context) Copy() *Context { func (c *Context) WithArg(v any) *Context { c = c.Copy() - c.Arg = v + c.arg = v return c } diff --git a/tg/file.go b/tg/file.go index 20bdacb..70a97b0 100644 --- a/tg/file.go +++ b/tg/file.go @@ -77,14 +77,14 @@ func (f *File) SendData() string { return "" } func (f *File) SendConfig( - c *Context, + sid SessionId, bot *Bot, ) (*SendConfig) { var config SendConfig - sid := c.Session.Id.ToApi() + cid := sid.ToApi() switch f.Type() { case ImageFileType: - photo := tgbotapi.NewPhoto(sid, f) + photo := tgbotapi.NewPhoto(cid, f) photo.Caption = f.caption config.Image = &photo