From 4737464273f87782f4f3c63339a40f4d6488fe8f Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 22 Sep 2023 11:17:18 +0300 Subject: [PATCH] Added the better reading ability. --- cmd/test/main.go | 36 ++++++++++++++++----------------- tg/read.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ tg/widget.go | 14 +++++++++++++ 3 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 tg/read.go diff --git a/cmd/test/main.go b/cmd/test/main.go index dd73e3d..e1dce6b 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -52,7 +52,7 @@ func ExtractSessionData(c *tg.Context) *SessionData { } var ( - startScreenButton = tg.NewButton("Home").Go("/") + homeButton = tg.NewButton("Home").Go("/") backButton = tg.NewButton("Back").Go("..") backKeyboard = tg.NewKeyboard().Row( backButton, @@ -70,7 +70,7 @@ var ( c.Sendf("%d", d.Counter) }), ).Row( - startScreenButton, + backButton, ) navKeyboard = tg.NewKeyboard().Row( @@ -97,12 +97,7 @@ var ( ) }), ).Row( - startScreenButton, - ).Reply() - - // The keyboard to return to the start screen. - navToStartKeyboard = tg.NewKeyboard().Row( - startScreenButton, + backButton, ).Reply() ) @@ -190,6 +185,10 @@ WithInitFunc(func(c *tg.Context) { ), ), )).WithCommands( + tg.NewCommand("info"). + ActionFunc(func(c *tg.Context){ + c.SendfHTML(`cockcock die`) + }), tg.NewCommand("start"). Desc( "start or restart the bot or move to the start screen", @@ -201,17 +200,16 @@ WithInitFunc(func(c *tg.Context) { }), tg.NewCommand("read"). Desc("reads a string and sends it back"). - WidgetFunc(func(c *tg.Context) { - c.Sendf("Type text and I will send it back to you") - for u := range c.Input() { - if u.Message == nil { - continue - } - c.Sendf("You typed %q", u.Message.Text) - break - } - c.Sendf("Done") - }), + WithWidget( + tg.NewTextMessageRead( + tg.Func(func(c *tg.Context){ + c.Sendf("Type a string and it will send it back") + }), + tg.Func(func(c *tg.Context){ + c.Sendf("You typed %q", c.Message.Text) + }), + ), + ), tg.NewCommand("image"). Desc("sends a sample image"). ActionFunc(func(c *tg.Context) { diff --git a/tg/read.go b/tg/read.go new file mode 100644 index 0000000..e221646 --- /dev/null +++ b/tg/read.go @@ -0,0 +1,52 @@ +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 + } +} + + diff --git a/tg/widget.go b/tg/widget.go index 30c274d..9a4cfd2 100644 --- a/tg/widget.go +++ b/tg/widget.go @@ -36,6 +36,13 @@ type Filterer interface { Filter(*Update, MessageMap) bool } +type FilterFunc func(*Update, MessageMap) bool +func (f FilterFunc) Filter( + u *Update, msgs MessageMap, +) bool { + return f(u, msgs) +} + // General type function for faster typing. type Func func(*Context) func (f Func) Act(c *Context) { @@ -224,6 +231,13 @@ func (widget *ReplyKeyboardWidget) SendConfig( sid SessionId, bot *Bot, ) (*SendConfig) { + if widget == nil { + msgConfig := tgbotapi.NewMessage(sid.ToApi(), ">") + msgConfig.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true) + return &SendConfig{ + Message: &msgConfig, + } + } var text string if widget.Text != "" { text = widget.Text