Added the better reading ability.

This commit is contained in:
Andrey Parhomenko 2023-09-22 11:17:18 +03:00
parent 307ebd03e3
commit 4737464273
3 changed files with 83 additions and 19 deletions

View file

@ -52,7 +52,7 @@ func ExtractSessionData(c *tg.Context) *SessionData {
} }
var ( var (
startScreenButton = tg.NewButton("Home").Go("/") homeButton = tg.NewButton("Home").Go("/")
backButton = tg.NewButton("Back").Go("..") backButton = tg.NewButton("Back").Go("..")
backKeyboard = tg.NewKeyboard().Row( backKeyboard = tg.NewKeyboard().Row(
backButton, backButton,
@ -70,7 +70,7 @@ var (
c.Sendf("%d", d.Counter) c.Sendf("%d", d.Counter)
}), }),
).Row( ).Row(
startScreenButton, backButton,
) )
navKeyboard = tg.NewKeyboard().Row( navKeyboard = tg.NewKeyboard().Row(
@ -97,12 +97,7 @@ var (
) )
}), }),
).Row( ).Row(
startScreenButton, backButton,
).Reply()
// The keyboard to return to the start screen.
navToStartKeyboard = tg.NewKeyboard().Row(
startScreenButton,
).Reply() ).Reply()
) )
@ -190,6 +185,10 @@ WithInitFunc(func(c *tg.Context) {
), ),
), ),
)).WithCommands( )).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`)
}),
tg.NewCommand("start"). tg.NewCommand("start").
Desc( Desc(
"start or restart the bot or move to the start screen", "start or restart the bot or move to the start screen",
@ -201,17 +200,16 @@ WithInitFunc(func(c *tg.Context) {
}), }),
tg.NewCommand("read"). tg.NewCommand("read").
Desc("reads a string and sends it back"). Desc("reads a string and sends it back").
WidgetFunc(func(c *tg.Context) { WithWidget(
c.Sendf("Type text and I will send it back to you") tg.NewTextMessageRead(
for u := range c.Input() { tg.Func(func(c *tg.Context){
if u.Message == nil { c.Sendf("Type a string and it will send it back")
continue
}
c.Sendf("You typed %q", u.Message.Text)
break
}
c.Sendf("Done")
}), }),
tg.Func(func(c *tg.Context){
c.Sendf("You typed %q", c.Message.Text)
}),
),
),
tg.NewCommand("image"). tg.NewCommand("image").
Desc("sends a sample image"). Desc("sends a sample image").
ActionFunc(func(c *tg.Context) { ActionFunc(func(c *tg.Context) {

52
tg/read.go Normal file
View file

@ -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
}
}

View file

@ -36,6 +36,13 @@ type Filterer interface {
Filter(*Update, MessageMap) bool 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. // General type function for faster typing.
type Func func(*Context) type Func func(*Context)
func (f Func) Act(c *Context) { func (f Func) Act(c *Context) {
@ -224,6 +231,13 @@ func (widget *ReplyKeyboardWidget) SendConfig(
sid SessionId, sid SessionId,
bot *Bot, bot *Bot,
) (*SendConfig) { ) (*SendConfig) {
if widget == nil {
msgConfig := tgbotapi.NewMessage(sid.ToApi(), ">")
msgConfig.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
return &SendConfig{
Message: &msgConfig,
}
}
var text string var text string
if widget.Text != "" { if widget.Text != "" {
text = widget.Text text = widget.Text