391 lines
8 KiB
Go
391 lines
8 KiB
Go
package tg
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
//"path"
|
|
)
|
|
|
|
|
|
// Interface to interact with the user.
|
|
type Context struct {
|
|
// The session contains all
|
|
// the information between the contexts.
|
|
session *Session
|
|
// The update that called the Context usage.
|
|
update Update
|
|
// Used as way to provide outer values redirection
|
|
// into widgets and actions. It is like arguments
|
|
// for REST API request etc.
|
|
arg any
|
|
typ ContextType
|
|
// Instead of updates as argument.
|
|
input *UpdateChan
|
|
}
|
|
|
|
// 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
|
|
// or false if the user is not logged in.
|
|
func (c Context) As(sid SessionId) (Context, bool) {
|
|
s, ok := c.Bot().GotSession(sid)
|
|
if !ok {
|
|
return Context{}, false
|
|
}
|
|
|
|
c.session = s
|
|
return c, true
|
|
}
|
|
|
|
// General type function to define actions, single component widgets
|
|
// and components themselves.
|
|
type Func func(Context)
|
|
func (f Func) Act(c Context) {
|
|
f(c)
|
|
}
|
|
func (f Func) Serve(c Context) {
|
|
f(c)
|
|
}
|
|
func(f Func) Filter(_ Update) bool {
|
|
return false
|
|
}
|
|
func (f Func) Render(_ Context) UI {
|
|
return UI{
|
|
f,
|
|
}
|
|
}
|
|
|
|
type ContextType uint8
|
|
const (
|
|
NoContextType ContextType = iota
|
|
WidgetContextType
|
|
ActionContextType
|
|
)
|
|
|
|
// Goroutie function to handle each user.
|
|
func (c Context) serve() {
|
|
beh := c.Bot().behaviour
|
|
c.Run(beh.Init)
|
|
beh.Root.Serve(c)
|
|
}
|
|
|
|
func (c Context) Arg() any {
|
|
return c.arg
|
|
}
|
|
|
|
func (c Context) Run(a Action) {
|
|
if a != nil {
|
|
a.Act(c)
|
|
}
|
|
}
|
|
|
|
|
|
// Sends to the Sendable object to the session user.
|
|
func (c Context) Send(v Sendable) (*Message, error) {
|
|
config := v.SendConfig(c.SessionId(), c.Bot())
|
|
if config.Error != nil {
|
|
return nil, config.Error
|
|
}
|
|
|
|
msg, err := c.Bot().Api().Send(config.ToApi())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &msg, nil
|
|
}
|
|
|
|
// Sends the formatted with fmt.Sprintf message to the user
|
|
// using default Markdown parsing format.
|
|
func (c Context) Sendf(format string, v ...any) (*Message, error) {
|
|
return c.Send(Messagef(format, v...))
|
|
}
|
|
|
|
// Same as Sendf but uses Markdown 2 format for parsing.
|
|
func (c Context) Sendf2(format string, v ...any) (*Message, error) {
|
|
return c.Send(Messagef(format, v...).MD2())
|
|
}
|
|
|
|
// Same as Sendf but uses HTML format for parsing.
|
|
func (c Context) SendfHTML(format string, v ...any) (*Message, error) {
|
|
return c.Send(Messagef(format, v...).HTML())
|
|
}
|
|
|
|
// Send the message in raw format escaping all the special characters.
|
|
func (c Context) SendfR(format string, v ...any) (*Message, error) {
|
|
return c.Send(Messagef("%s", Escape2(fmt.Sprintf(format, v...))).MD2())
|
|
}
|
|
|
|
// Get the input for current widget.
|
|
// Should be used inside handlers (aka "Serve").
|
|
func (c Context) Input() chan Update {
|
|
return c.input.Chan()
|
|
}
|
|
|
|
func (c Context) WithArg(v any) Context {
|
|
c.arg = v
|
|
return c
|
|
}
|
|
|
|
func (c Context) WithUpdate(u Update) Context {
|
|
c.update = u
|
|
return c
|
|
}
|
|
|
|
func (c Context) WithInput(input *UpdateChan) Context {
|
|
c.input = input
|
|
return c
|
|
}
|
|
|
|
// Customized actions for the bot.
|
|
type Action interface {
|
|
Act(Context)
|
|
}
|
|
|
|
type ActionFunc func(Context)
|
|
|
|
func (af ActionFunc) Act(c Context) {
|
|
af(c)
|
|
}
|
|
|
|
// Simple way to read strings for widgets with
|
|
// the specified prompt.
|
|
func (c Context) ReadString(promptf string, args ...any) string {
|
|
var text string
|
|
if promptf != "" {
|
|
c.Sendf(promptf, args...)
|
|
}
|
|
for u := range c.Input() {
|
|
if u.Message == nil {
|
|
continue
|
|
}
|
|
text = u.Message.Text
|
|
break
|
|
}
|
|
return text
|
|
}
|
|
|
|
func (c Context) CallbackUpdate() *Update {
|
|
return &c.update
|
|
}
|
|
|
|
// Returns the reader for specified file ID and path.
|
|
func (c Context) GetFile(fileId FileId) (io.ReadCloser, string, error) {
|
|
file, err := c.Bot().Api().GetFile(tgbotapi.FileConfig{FileID:string(fileId)})
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
r, err := http.Get(fmt.Sprintf(
|
|
"https://api.telegram.org/file/bot%s/%s",
|
|
c.Bot().Api().Token,
|
|
file.FilePath,
|
|
))
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
if r.StatusCode != 200 {
|
|
return nil, "", StatusCodeErr
|
|
}
|
|
|
|
return r.Body, file.FilePath, nil
|
|
}
|
|
|
|
// Reads all the content from the specified file.
|
|
func (c Context) ReadFile(fileId FileId) ([]byte, string, error) {
|
|
file, pth, err := c.GetFile(fileId)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
defer file.Close()
|
|
|
|
bts, err := io.ReadAll(file)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
|
|
return bts, pth, nil
|
|
}
|
|
|
|
func (c Context) runCompo(compo Component, arg any) (*UpdateChan, error) {
|
|
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(
|
|
c.WithInput(updates).
|
|
WithArg(arg),
|
|
)
|
|
// 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.
|
|
func (c Context) runWidget(widget Widget, arg any) (*UpdateChan, error) {
|
|
var err error
|
|
if widget == nil {
|
|
return nil, EmptyWidgetErr
|
|
}
|
|
|
|
pth := c.Path()
|
|
compos := widget.Render(c.WithArg(arg))
|
|
// Leave if changed path or components are empty.
|
|
if compos == nil || pth != c.Path() {
|
|
return nil, EmptyCompoErr
|
|
}
|
|
chns := make([]*UpdateChan, len(compos))
|
|
for i, compo := range compos {
|
|
chns[i], err = c.runCompo(compo, arg)
|
|
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
|
|
}
|
|
|
|
// Simple go without an argument for context.
|
|
func (c Context) Go(pth Path) error {
|
|
return c.GoWithArg(pth, nil)
|
|
}
|
|
|
|
// Changes screen of user to the Id one.
|
|
// Also gives the arg to the widget calling
|
|
// contexts.
|
|
func (c Context) GoWithArg(pth Path, arg any) error {
|
|
var err error
|
|
if pth == "" {
|
|
c.session.pathHistory = []Path{}
|
|
return nil
|
|
}
|
|
var back bool
|
|
if pth == "-" {
|
|
if len(c.session.pathHistory) < 2 {
|
|
return c.GoWithArg("", arg)
|
|
}
|
|
pth = c.session.pathHistory[len(c.session.pathHistory)-2]
|
|
c.session.pathHistory = c.session.pathHistory[:len(c.session.pathHistory)-1]
|
|
}
|
|
// Getting the screen and changing to
|
|
// then executing its widget.
|
|
if !pth.IsAbs() {
|
|
pth = (c.Path() + "/" + pth).Clean()
|
|
}
|
|
|
|
if !c.PathExist(pth) {
|
|
return ScreenNotExistErr
|
|
}
|
|
|
|
if !back && c.Path() != pth {
|
|
c.session.pathHistory = append(c.session.pathHistory, pth)
|
|
}
|
|
|
|
// Stopping the current widget.
|
|
screen := c.Bot().behaviour.Screens[pth]
|
|
c.session.skippedUpdates.Close()
|
|
|
|
if screen.Widget != nil {
|
|
c.session.skippedUpdates, err = c.runWidget(screen.Widget, arg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
return NoWidgetForScreenErr
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (c Context) SessionId() SessionId {
|
|
return c.session.Id
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// Returns true if the path exists and false otherwise.
|
|
func (c Context) PathExist(pth Path) bool {
|
|
return c.session.bot.behaviour.PathExist(pth)
|
|
}
|
|
|
|
// Return context's session's path history.
|
|
func (c Context) PathHistory() []Path {
|
|
return c.session.pathHistory
|
|
}
|
|
|
|
func (c Context) Path() Path {
|
|
ln := len(c.session.pathHistory)
|
|
if ln == 0 {
|
|
return ""
|
|
}
|
|
return c.session.pathHistory[ln-1]
|
|
}
|