Finally implemented at least not stable but working version.

This commit is contained in:
Andrey Parhomenko 2023-09-27 14:09:49 +03:00
parent abf080164a
commit 0aaabff503
13 changed files with 206 additions and 249 deletions

View file

@ -7,8 +7,8 @@ import (
"fmt"
"github.com/mojosa-software/got/tg"
"math/rand"
"strconv"
//"math/rand"
//"strconv"
)
type BotData struct {
@ -20,17 +20,19 @@ type SessionData struct {
}
type MutateMessageWidget struct {
*tg.Compo
Mutate func(string) string
}
func NewMutateMessageWidget(fn func(string) string) *MutateMessageWidget {
ret := &MutateMessageWidget{}
ret.Compo = tg.NewCompo()
ret.Mutate = fn
return ret
}
func (w *MutateMessageWidget) Serve(c *tg.Context) {
args, ok := c.Arg.(tg.ArgSlice)
args, ok := c.Arg.([]any)
if ok {
for _, arg := range args {
c.Sendf("%v", arg)
@ -42,7 +44,7 @@ func (w *MutateMessageWidget) Serve(c *tg.Context) {
}
}
func (w *MutateMessageWidget) Filter(u *tg.Update, _ tg.MessageMap) bool {
func (w *MutateMessageWidget) Filter(u *tg.Update) bool {
if u.Message == nil {
return true
}
@ -75,8 +77,6 @@ var (
backButton,
)
navKeyboard =
sendLocationKeyboard = tg.NewKeyboard().Row(
tg.NewButton("Send location").
WithSendLocation(true).
@ -103,17 +103,19 @@ WithInitFunc(func(c *tg.Context) {
c.Session.Data = &SessionData{}
}).WithRootNode(tg.NewRootNode(
// The "/" widget.
tg.WidgetFunc(func(c *tg.Context) tg.UIs {return tg.UIs{
tg.RenderFunc(func(c *tg.Context) tg.UI {return tg.UI {
tg.NewKeyboard().Row(
tg.NewButton("GoT Github page").
WithUrl("https://github.com/mojosa-software/got"),
).Inline().Widget(
).Inline().Compo(
fmt.Sprintf(
"Hello, %s!\n"
"The testing bot started!\n",
"You can see the basics of usage in the ",
"cmd/test/main.go file!",
fmt.Sprint(
"Hello, %s!\n",
"The testing bot started!\n",
"You can see the basics of usage in the ",
"cmd/test/main.go file!",
),
c.SentFrom().UserName,
),
),
@ -124,72 +126,84 @@ WithInitFunc(func(c *tg.Context) {
tg.NewButton("Mutate messages").Go("/mutate-messages"),
).Row(
tg.NewButton("Send location").Go("/send-location"),
).Reply().Widget(
).Reply().Compo(
"Choose the point of your interest",
),
}}),
tg.NewNode(
"mutate-messages", tg.NewPage().WithReply(
tg.NewKeyboard().Row(
tg.NewButton("Upper case").Go("upper-case"),
tg.NewButton("Lower case").Go("lower-case"),
).Row(
backButton,
).Reply().Widget(
"Choose the function to mutate string",
),
),
"mutate-messages", tg.RenderFunc(func(c *tg.Context) tg.UI {
return tg.UI{
tg.NewKeyboard().Row(
tg.NewButton("Upper case").Go("upper-case"),
tg.NewButton("Lower case").Go("lower-case"),
).Row(
backButton,
).Reply().Compo(
"Choose the function to mutate string",
),
}
}),
tg.NewNode(
"upper-case", tg.NewPage().WithReply(
backKeyboard.Reply().
Widget(
"Type a string and the bot will convert it to upper case",
),
).WithSub(
NewMutateMessageWidget(strings.ToUpper),
),
),
tg.NewNode(
"lower-case", tg.NewPage().WithReply(
backKeyboard.Reply().
Widget(
"Type a string and the bot will convert it to lower case",
),
).WithSub(
NewMutateMessageWidget(strings.ToLower),
),
),
),
tg.NewNode(
"inc-dec", tg.NewPage().WithReply(
incDecKeyboard.Reply().Widget("Press the buttons to increment and decrement"),
).ActionFunc(func(c *tg.Context) {
// The function will be calleb before serving page.
d := ExtractSessionData(c)
c.Sendf("Current counter value = %d", d.Counter)
"upper-case", tg.RenderFunc(func(c *tg.Context) tg.UI {
return tg.UI{
backKeyboard.Reply().Compo(
"Type a string and the bot will convert it to upper case",
),
NewMutateMessageWidget(strings.ToUpper),
}
}),
),
tg.NewNode(
"lower-case", tg.RenderFunc(func(c *tg.Context) tg.UI {
return tg.UI{
backKeyboard.Reply().
Compo(
"Type a string and the bot will convert it to lower case",
),
NewMutateMessageWidget(strings.ToLower),
}
}),
),
),
tg.NewNode(
"send-location", tg.NewPage().WithReply(
sendLocationKeyboard.Widget("Press the button to send your location!"),
).WithInline(
tg.NewKeyboard().Row(
tg.NewButton(
"Check",
).WithData(
"check",
).ActionFunc(func(c *tg.Context) {
d := ExtractSessionData(c)
c.Sendf("Counter = %d", d.Counter)
}),
).Inline().Widget("Press the button to display your counter"),
),
"inc-dec", tg.RenderFunc(func(c *tg.Context) tg.UI {
d := ExtractSessionData(c)
return tg.UI{
incDecKeyboard.Reply().Compo(fmt.Sprintf(
"Press the buttons to increment and decrement.\n" +
"Current counter value = %d", d.Counter,
)),
}
}),
),
)).WithCommands(
tg.NewNode(
"send-location", tg.RenderFunc(func(c *tg.Context) tg.UI {
return tg.UI {
tg.NewKeyboard().Row(
tg.NewButton(
"Check",
).WithData(
"check",
).WithAction(tg.Func(func(c *tg.Context) {
d := ExtractSessionData(c)
c.Sendf("Counter = %d", d.Counter)
})),
).Inline().Compo("Press the button to display your counter"),
sendLocationKeyboard.Compo(
"Press the button to send your location!",
),
}
}),
),
)).WithRoot(tg.NewCommandCompo().
WithPreStart(tg.Func(func(c *tg.Context){
c.Sendf("Please, use /start ")
})).WithCommands(
tg.NewCommand("info").
ActionFunc(func(c *tg.Context){
c.SendfHTML(`<a href="https://res.cloudinary.com/demo/image/upload/v1312461204/sample.jpg">cock</a><strong>cock</strong> die`)
@ -219,37 +233,15 @@ WithInitFunc(func(c *tg.Context) {
}),
tg.NewCommand("botname").
Desc("get the bot name").
ActionFunc(func(c *tg.Context) {
WithAction(tg.Func(func(c *tg.Context) {
bd := c.Bot.Data.(*BotData)
c.Sendf("My name is %q", bd.Name)
}),
})),
tg.NewCommand("dynamic").
Desc("check of the dynamic work").
WidgetFunc(func(c *tg.Context){
nRow, nBtn := rand.Int()%10, rand.Int()%5
rows := []tg.ButtonRow{}
for i:=0 ; i<nRow ; i++ {
row := []*tg.Button{}
for j:=0 ; j<nBtn ; j++ {
row = append(row, tg.NewButton(
strconv.Itoa(i) + " " + strconv.Itoa(j),
))
}
rows = append(rows, row)
}
kbd := tg.NewKeyboard(rows...).ActionFunc(func(c *tg.Context){
c.Sendf(u.)
})Inline().Widget("sample text")
c.Send(kbd)
kbdChn := c.RunWidget(kbd)
for u := range c.Input() {
if kbd.Filter(u, nil) {
continue
}
kbdChn.Send(u)
}
}),
)
WithWidget(tg.Func(func(c *tg.Context){
})),
))
var gBeh = tg.NewGroupBehaviour().
InitFunc(func(c *tg.GC) {

View file

@ -5,7 +5,7 @@ package tg
// The type describes behaviour for the bot in personal chats.
type Behaviour struct {
Root Widget
Root Component
Init Action
Screens ScreenMap
}
@ -17,12 +17,6 @@ func NewBehaviour() *Behaviour {
}
}
// Set the root widget. Mostly the CommandWidget is used.
func (b *Behaviour) WithRoot(root Widget) *Behaviour {
b.Root = root
return b
}
// The Action will be called on session creation,
// not when starting or restarting the bot with the Start Action.
func (b *Behaviour) WithInit(a Action) *Behaviour {
@ -61,15 +55,8 @@ func (b *Behaviour) WithRootNode(node *RootNode) *Behaviour {
// The function sets as the standard root widget CommandWidget
// and its commands..
func (b *Behaviour) WithCommands(cmds ...*Command) *Behaviour {
b.Root = NewCommandWidget().
WithCommands(cmds...).
WithPreStartFunc(func(c *Context){
c.Sendf("Please, use the /start command to start")
}).WithUsageFunc(func(c *Context){
c.Sendf("No such command")
})
func (b *Behaviour) WithRoot(root Component) *Behaviour {
b.Root = root
return b
}

View file

@ -39,6 +39,7 @@ func NewBot(token string) (*Bot, error) {
return &Bot{
Api: bot,
contexts: make(map[SessionId] *context),
}, nil
}
@ -50,14 +51,18 @@ func (bot *Bot) Debug(debug bool) *Bot {
// Send the Renderable to the specified session client side.
// Can be used for both group and private sessions.
func (bot *Bot) Send(
sid SessionId, v Sendable,
sid SessionId, v Sendable, args ...any,
) (*Message, error) {
c, ok := bot.contexts[sid]
ctx, ok := bot.contexts[sid]
if !ok {
return nil, ContextNotExistErr
}
config := v.Render(sid, bot)
c := &Context{
context: ctx,
}
config := v.SendConfig(c.WithArg(c.MakeArg(args)))
if config.Error != nil {
return nil, config.Error
}
@ -220,18 +225,19 @@ func (bot *Bot) handlePrivate(updates chan *Update) {
sid = SessionId(u.FromChat().ID)
ctx, ctxOk := bot.contexts[sid]
if u.Message != nil && !ctxOk {
// Create context on any message
// if we have no one.
session, sessionOk := bot.sessions[sid]
if !sessionOk {
// Creating session if we have none.
session = bot.sessions.Add(sid)
session = bot.sessions.Add(sid, PrivateSessionScope)
}
session = bot.sessions[sid]
// Create context on any message
// if we have no one.
ctx = &context{
Bot: bot,
Session: session,
scope: PrivateContextScope,
updates: NewUpdateChan(),
}
if !ctxOk {

View file

@ -100,21 +100,21 @@ func (c *GroupCommand) ToApi() tgbotapi.BotCommand {
// The type is used to recognize commands and execute
// its actions and widgets .
type CommandWidget struct {
type CommandCompo struct {
PreStart Action
Commands CommandMap
Usage Action
}
// Returns new empty CommandWidget.
func NewCommandWidget() *CommandWidget {
ret := &CommandWidget{}
// Returns new empty CommandCompo.
func NewCommandCompo() *CommandCompo {
ret := &CommandCompo{}
ret.Commands = make(CommandMap)
return ret
}
// Set the commands to handle.
func (w *CommandWidget) WithCommands(cmds ...*Command) *CommandWidget {
func (w *CommandCompo) WithCommands(cmds ...*Command) *CommandCompo {
for _, cmd := range cmds {
if cmd.Name == "" {
panic("empty command name")
@ -129,41 +129,41 @@ func (w *CommandWidget) WithCommands(cmds ...*Command) *CommandWidget {
}
// Set the prestart action.
func (w *CommandWidget) WithPreStart(a Action) *CommandWidget {
func (w *CommandCompo) WithPreStart(a Action) *CommandCompo {
w.PreStart = a
return w
}
// Set the prestart action with function.
func (w *CommandWidget) WithPreStartFunc(fn ActionFunc) *CommandWidget {
func (w *CommandCompo) WithPreStartFunc(fn ActionFunc) *CommandCompo {
return w.WithPreStart(fn)
}
// Set the usage action.
func (w *CommandWidget) WithUsage(a Action) *CommandWidget {
func (w *CommandCompo) WithUsage(a Action) *CommandCompo {
w.Usage = a
return w
}
// Set the usage action with function.
func (w *CommandWidget) WithUsageFunc(fn ActionFunc) *CommandWidget {
func (w *CommandCompo) WithUsageFunc(fn ActionFunc) *CommandCompo {
return w.WithUsage(fn)
}
func (widget *CommandWidget) Filter(
func (widget *CommandCompo) Filter(
u *Update,
msgs ...*Message,
) bool {
/*if u.Message == nil || !u.Message.IsCommand() {
if u.Message == nil || !u.Message.IsCommand() {
return false
}*/
}
return false
}
func (widget *CommandWidget) Serve(c *Context) {
// Implementing server.
func (compo *CommandCompo) Serve(c *Context) {
commanders := make(map[CommandName] BotCommander)
for k, v := range widget.Commands {
for k, v := range compo.Commands {
commanders[k] = v
}
c.Bot.SetCommands(
@ -178,7 +178,7 @@ func (widget *CommandWidget) Serve(c *Context) {
// while we have the empty screen.
// E. g. the session did not start.
if !(u.Message.IsCommand() && u.Message.Command() == "start") {
c.Run(widget.PreStart, u)
c.WithUpdate(u).Run(compo.PreStart)
continue
}
}
@ -186,16 +186,16 @@ func (widget *CommandWidget) Serve(c *Context) {
if u.Message != nil && u.Message.IsCommand() {
// Command handling.
cmdName := CommandName(u.Message.Command())
cmd, ok := widget.Commands[cmdName]
cmd, ok := compo.Commands[cmdName]
if !ok {
c.Run(widget.Usage, u)
c.WithUpdate(u).Run(compo.Usage)
continue
}
c.Run(cmd.Action, u)
c.WithUpdate(u).Run(cmd.Action)
if cmd.Widget != nil {
cmdUpdates.Close()
cmdUpdates = c.runWidget(cmd.Widget)
cmdUpdates = c.WithUpdate(u).RunWidget(cmd.Widget)
}
continue
}

View file

@ -9,7 +9,7 @@ type Widget interface {
// The way to describe custom function based Widgets.
type RenderFunc func(c *Context) UI
func (fn RenderFunc) Uis(c *Context) UI {
func (fn RenderFunc) Render(c *Context) UI {
return fn(c)
}
@ -30,15 +30,18 @@ type Component interface {
}
// The type to embed into potential components.
// Implements empty versions of interfaces
// and contains
// Implements empty versions of interfaces.
type Compo struct{
*Message
}
// Defalut setting message
func (compo Compo) SetMessage(msg *Message) { compo.Message = msg }
func (compo Compo) GetMessage() *Message { return compo.Message }
// Default non filtering filter. Always returns false.
func (compo Compo) Filter(_ *Update, _ *Message) bool {return false}
func NewCompo() *Compo {
return &Compo{}
}
// Defalut setting message
func (compo *Compo) SetMessage(msg *Message) { compo.Message = msg }
func (compo *Compo) GetMessage() *Message { return compo.Message }
// Default non filtering filter. Always returns false.
func (compo *Compo) Filter(_ *Update) bool {return false}

View file

@ -16,7 +16,7 @@ func (f Func) Act(c *Context) {
func (f Func) Serve(c *Context) {
f(c)
}
func(f Func) Filter(_ *Context) bool {
func(f Func) Filter(_ *Update) bool {
return false
}
func (f Func) Render(_ *Context) UI {
@ -68,7 +68,7 @@ func (c *Context) PrevPath() Path {
func (c *Context) Run(a Action) {
if a != nil {
a.Act(c.Copy().WithUpdate(u))
a.Act(c)
}
}
@ -186,52 +186,62 @@ func (c *Context) PathExist(pth Path) bool {
return c.Bot.behaviour.PathExist(pth)
}
// Run widget in background returning the new input channel for it.
func (c *Context) runWidget(widget Widget, args ...any) *UpdateChan {
if widget == nil {
return nil
}
func (c *Context) MakeArg(args []any) any {
var arg any
if len(args) == 1 {
arg = args[0]
} else if len(args) > 1 {
arg = args
}
return arg
}
func (c *Context) RunCompo(compo Component, args ...any) *UpdateChan {
s, ok := compo.(Sendable)
if ok {
msg, err := c.Send(s)
if err != nil {
panic("could not send the message")
}
s.SetMessage(msg)
}
updates := NewUpdateChan()
go func() {
compo.Serve(
c.WithInput(updates).
WithArg(c.MakeArg(args)),
)
// To let widgets finish themselves before
// the channel is closed and close it by themselves.
updates.Close()
}()
return updates
}
// Run widget in background returning the new input channel for it.
func (c *Context) RunWidget(widget Widget, args ...any) *UpdateChan {
if widget == nil {
return nil
}
pth := c.Path()
uis := widget.UI()
compos := widget.Render(c.WithArg(c.MakeArg(args)))
// Leave if changed path.
if pth != c.Path() {
return nil
}
chns := make(map[UI] *UpdateChan)
for _, ui := range uis {
s, ok := ui.(Sendable)
if ok {
msg := c.Send(s.SendConfig(c))
ui.SetMessage(msg)
}
updates := NewUpdateChan()
go func() {
ui.Serve(
c.Copy().
WithInput(updates).
WithArg(arg),
)
// To let widgets finish themselves before
// the channel is closed and close it by themselves.
updates.Close()
}()
chns[ui] = updates
chns := make([]*UpdateChan, len(compos))
for i, compo := range compos {
chns[i] = c.RunCompo(compo)
}
ret := NewUpdateChan()
go func() {
for u := range ret {
for ui := range uis {
if !ui.Filter() {
chns[ui] <- u
for u := range ret.Chan() {
for i, compo := range compos {
chn := chns[i]
if !compo.Filter(u) {
chn.Send(u)
}
}
}
@ -239,7 +249,7 @@ func (c *Context) runWidget(widget Widget, args ...any) *UpdateChan {
for _, chn := range chns {
chn.Close()
}
}
}()
return ret
}

View file

@ -23,15 +23,18 @@ var (
)
type File struct {
*Compo
path string
typ FileType
caption string
}
func NewFile(path string) *File {
return &File{
ret := &File{
path: path,
}
ret.Compo = NewCompo()
return ret
}
func (f *File) withType(typ FileType) *File {
@ -73,14 +76,14 @@ func (f *File) SendData() string {
return ""
}
func (f *File) SendConfig(
sid SessionId, bot *Bot,
c *Context,
) (*SendConfig) {
var config SendConfig
cid := sid.ToApi()
sid := c.Session.Id.ToApi()
switch f.Type() {
case ImageFileType:
photo := tgbotapi.NewPhoto(cid, f)
photo := tgbotapi.NewPhoto(sid, f)
photo.Caption = f.caption
config.Image = &photo

View file

@ -10,9 +10,9 @@ type Filterer interface {
Filter(*Update) bool
}
type FilterFunc func(*Update, MessageMap) bool
type FilterFunc func(*Update) bool
func (f FilterFunc) Filter(
u *Update, msgs MessageMap,
u *Update,
) bool {
return f(u, msgs)
return f(u)
}

View file

@ -25,15 +25,16 @@ func (kbd *Inline) ToApi() tgbotapi.InlineKeyboardMarkup {
// Transform the keyboard to widget with the specified text.
func (kbd *Inline) Compo(text string) *InlineCompo {
ret := &InlinCompo{}
ret := &InlineCompo{}
ret.Inline = kbd
ret.Text = text
ret.Compo = NewCompo()
return ret
}
// The type implements message with an inline keyboard.
type InlineCompo struct {
Compo
*Compo
Text string
*Inline
}
@ -87,13 +88,13 @@ func (widget *InlineCompo) Serve(c *Context) {
} else if widget.Action != nil {
act = widget.Action
}
c.Run(act, u)
c.WithUpdate(u).Run(act)
}
}
// Implementing the Filterer interface.
func (compo *InlineCompo) Filter(u *Update) bool {
if widget == nil || u.CallbackQuery == nil {
if compo == nil || u.CallbackQuery == nil {
return true
}

View file

@ -6,6 +6,7 @@ import (
// Simple text message type.
type MessageConfig struct {
Compo
ParseMode string
Text string
}
@ -38,10 +39,10 @@ func (msg *MessageConfig) HTML() *MessageConfig {
}
func (config *MessageConfig) SendConfig(
sid SessionId, bot *Bot,
c *Context,
) (*SendConfig) {
var ret SendConfig
msg := tgbotapi.NewMessage(sid.ToApi(), config.Text)
msg := tgbotapi.NewMessage(c.Session.Id.ToApi(), config.Text)
ret.Message = &msg
ret.Message.ParseMode = config.ParseMode
return &ret

View file

@ -1,52 +1,3 @@
package tg
// The type to descsribe one line reading widget.
type UpdateRead struct {
Pre Action
Filterer Filterer
Post Widget
}
func (rd *UpdateRead) Filter(u *Update, msgs MessageMap) bool {
if rd.Filterer != nil {
return rd.Filterer.Filter(u, msgs)
}
return false
}
// Returns new empty update reader.
func NewUpdateRead(filter Filterer, post Widget) *UpdateRead {
ret := &UpdateRead{}
ret.Filterer = filter
ret.Post = post
return ret
}
func (rd *UpdateRead) WithPre(a Action) *UpdateRead {
rd.Pre = a
return rd
}
func NewTextMessageRead(pre Action, post Widget) *UpdateRead {
ret := NewUpdateRead(
FilterFunc(func(u *Update, _ MessageMap) bool {
return u.Message == nil
}),
post,
).WithPre(pre)
return ret
}
func (rd *UpdateRead) Serve(c *Context) {
c.Run(rd.Pre, c.Update)
for u := range c.Input() {
if rd.Filter(u, nil) {
continue
}
c.RunWidget(rd.Post, u)
break
}
}

View file

@ -55,17 +55,19 @@ func (kbd *Reply) Compo(text string) *ReplyCompo {
ret := &ReplyCompo{}
ret.Reply = kbd
ret.Text = text
ret.Compo = NewCompo()
return ret
}
// The type implements reply keyboard widget.
type ReplyCompo struct {
*Compo
Text string
*Reply
}
// Implementing the sendable interface.
func (compo *ReplyCompo) Render(
func (compo *ReplyCompo) SendConfig(
c *Context,
) (*SendConfig) {
sid := c.Session.Id.ToApi()
@ -98,10 +100,10 @@ func (compo *ReplyCompo) Filter(
return true
}
_, ok := widget.ButtonMap()[u.Message.Text]
_, ok := compo.ButtonMap()[u.Message.Text]
if !ok {
if u.Message.Location != nil {
locBtn := widget.ButtonMap().LocationButton()
locBtn := compo.ButtonMap().LocationButton()
if locBtn == nil {
return true
}
@ -117,7 +119,7 @@ func (compo *ReplyCompo) Serve(c *Context) {
for u := range c.Input() {
var btn *Button
text := u.Message.Text
btns := widget.ButtonMap()
btns := compo.ButtonMap()
btn, ok := btns[text]
if !ok {
@ -127,7 +129,7 @@ func (compo *ReplyCompo) Serve(c *Context) {
}
if btn != nil {
c.Run(btn.Action, u)
c.WithUpdate(u).Run(btn.Action)
}
}
}

View file

@ -4,7 +4,7 @@ package tg
// related to.
type SessionScope uint8
const (
NoSessionScope ContextScope = iota
NoSessionScope SessionScope = iota
PrivateSessionScope
GroupSessionScope
ChannelSessionScope
@ -30,9 +30,10 @@ type Session struct {
}
// Return new empty session with specified user ID.
func NewSession(id SessionId) *Session {
func NewSession(id SessionId, scope SessionScope) *Session {
return &Session{
Id: id,
Scope: scope,
}
}
@ -41,9 +42,9 @@ func NewSession(id SessionId) *Session {
type SessionMap map[SessionId]*Session
// Add new empty session by it's ID.
func (sm SessionMap) Add(sid SessionId) *Session {
ret := NewSession(sid)
sm[sid] = NewSession(sid)
func (sm SessionMap) Add(sid SessionId, scope SessionScope) *Session {
ret := NewSession(sid, scope)
sm[sid] = ret
return ret
}