Skip to content

Commit bc6be74

Browse files
committed
Merge branch 'v3'
2 parents cf8477d + 53c4e0a commit bc6be74

File tree

19 files changed

+324
-272
lines changed

19 files changed

+324
-272
lines changed

buttons.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7-
tb "gopkg.in/tucnak/telebot.v2"
7+
tb "gopkg.in/tucnak/telebot.v3"
88
)
99

1010
type buttons struct {
@@ -20,8 +20,7 @@ func (b *buttons) SetMaxButtons(n int) {
2020

2121
type PostButtons struct {
2222
*buttons
23-
b Boter
24-
cbFn func(cb *tb.Callback)
23+
cbFn tb.HandlerFunc
2524
}
2625

2726
type PBOption func(*PostButtons)
@@ -35,9 +34,8 @@ func PBOptMaxButtons(n int) PBOption {
3534
// NewPostButtons creates an instance of PostButtons. The callbackFunction is
3635
// the function that will be assigned and called for each button press, so it
3736
// should handle all possible values.
38-
func NewPostButtons(b Boter, callbackFn func(cb *tb.Callback), opts ...PBOption) *PostButtons {
37+
func NewPostButtons(callbackFn func(c tb.Context) error, opts ...PBOption) *PostButtons {
3938
pb := &PostButtons{
40-
b: b,
4139
cbFn: callbackFn,
4240
buttons: &buttons{maxButtons: defNumButtons},
4341
}
@@ -48,11 +46,11 @@ func NewPostButtons(b Boter, callbackFn func(cb *tb.Callback), opts ...PBOption)
4846
}
4947

5048
// Markup returns the markup with buttons labeled with labels.
51-
func (pb *PostButtons) Markup(labels []string, pattern ...uint) (*tb.ReplyMarkup, error) {
49+
func (pb *PostButtons) Markup(c tb.Context, labels []string, pattern ...uint) (*tb.ReplyMarkup, error) {
5250
if len(pattern) == 0 {
53-
return ButtonMarkup(pb.b, labels, pb.maxButtons, pb.cbFn), nil
51+
return ButtonMarkup(c, labels, pb.maxButtons, pb.cbFn), nil
5452
}
55-
markup, err := ButtonPatternMarkup(pb.b, labels, pattern, pb.cbFn)
53+
markup, err := ButtonPatternMarkup(c, labels, pattern, pb.cbFn)
5654
if err != nil {
5755
panic(err)
5856
}
@@ -62,17 +60,17 @@ func (pb *PostButtons) Markup(labels []string, pattern ...uint) (*tb.ReplyMarkup
6260
// ButtonMarkup returns the button markup for the message. It creates handlers
6361
// for all the buttons assigning the cbFn callback function to each of them.
6462
// Values must be unique. maxRowButtons is maximum number of buttons in a row.
65-
func ButtonMarkup(b Boter, values []string, maxRowButtons int, cbFn func(*tb.Callback)) *tb.ReplyMarkup {
63+
func ButtonMarkup(c tb.Context, values []string, maxRowButtons int, cbFn func(c tb.Context) error) *tb.ReplyMarkup {
6664
if maxRowButtons <= 0 || defNumButtons < maxRowButtons {
6765
maxRowButtons = defNumButtons
6866
}
69-
markup, btns := createButtons(b, values, cbFn)
67+
markup, btns := createButtons(c, values, cbFn)
7068
markup.Inline(organizeButtons(btns, maxRowButtons)...)
7169
return markup
7270
}
7371

74-
func ButtonPatternMarkup(b Boter, values []string, pattern []uint, cbFn func(*tb.Callback)) (*tb.ReplyMarkup, error) {
75-
markup, btns := createButtons(b, values, cbFn)
72+
func ButtonPatternMarkup(c tb.Context, values []string, pattern []uint, cbFn func(c tb.Context) error) (*tb.ReplyMarkup, error) {
73+
markup, btns := createButtons(c, values, cbFn)
7674
rows, err := organizeButtonsPattern(btns, pattern)
7775
if err != nil {
7876
return nil, err
@@ -81,13 +79,13 @@ func ButtonPatternMarkup(b Boter, values []string, pattern []uint, cbFn func(*tb
8179
return markup, nil
8280
}
8381

84-
func createButtons(b Boter, values []string, cbFn func(*tb.Callback)) (*tb.ReplyMarkup, []tb.Btn) {
82+
func createButtons(c tb.Context, values []string, cbFn func(c tb.Context) error) (*tb.ReplyMarkup, []tb.Btn) {
8583
markup := new(tb.ReplyMarkup)
8684
var btns []tb.Btn
8785
for _, label := range values {
8886
btn := markup.Data(label, hash(label), label)
8987
btns = append(btns, btn)
90-
b.Handle(&btn, cbFn)
88+
c.Bot().Handle(&btn, cbFn)
9189
}
9290
return markup, btns
9391
}

buttons_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8-
tb "gopkg.in/tucnak/telebot.v2"
8+
tb "gopkg.in/tucnak/telebot.v3"
99
)
1010

1111
func Test_organizeButtons(t *testing.T) {

examples/input/main.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import (
88
"time"
99

1010
"github.com/joho/godotenv"
11-
"github.com/rusq/tbcomctl"
12-
tb "gopkg.in/tucnak/telebot.v2"
11+
"github.com/rusq/tbcomctl/v3"
12+
tb "gopkg.in/tucnak/telebot.v3"
1313
)
1414

1515
var _ = godotenv.Load()
1616

1717
var (
1818
token = os.Getenv("TOKEN")
19-
chat = os.Getenv("CHAT")
19+
// chat = os.Getenv("CHAT")
2020
)
2121

2222
func main() {
@@ -28,25 +28,26 @@ func main() {
2828
log.Fatal(err)
2929
}
3030

31-
nameIp := tbcomctl.NewInputText(b, "name", "Input your name:", processInput(b))
32-
ageIp := tbcomctl.NewInputText(b, "age", "Input your age", processInput(b))
31+
nameIp := tbcomctl.NewInputText("name", "Input your name:", processInput(b))
32+
ageIp := tbcomctl.NewInputText("age", "Input your age", processInput(b))
3333

3434
handler := tbcomctl.NewControllerChain(nameIp, ageIp)
3535
form := tbcomctl.NewForm(nameIp, ageIp)
3636

3737
b.Handle("/input", handler)
3838
b.Handle("/form", form.Handler)
3939
// b.Handle(tb.OnText, tbcomctl.NewMiddlewareChain(onText, nameIp.OnTextMw, ageIp.OnTextMw))
40-
b.Handle(tb.OnText, form.OnTextMiddleware(func(m *tb.Message) {
41-
log.Printf("onText is called: %q\nuser data: %v", m.Text, form.Data(m.Sender))
40+
b.Handle(tb.OnText, form.OnTextMiddleware(func(c tb.Context) error {
41+
log.Printf("onText is called: %q\nuser data: %v", c.Message().Text, form.Data(c.Sender()))
42+
return nil
4243
}))
4344

4445
b.Start()
4546
}
4647

47-
func processInput(b *tb.Bot) func(context.Context, *tb.Message) error {
48-
return func(ctx context.Context, m *tb.Message) error {
49-
val := m.Text
48+
func processInput(b *tb.Bot) func(ctx context.Context, c tb.Context) error {
49+
return func(ctx context.Context, c tb.Context) error {
50+
val := c.Message().Text
5051
log.Println("msgCb function is called, input value:", val)
5152
switch val {
5253
case "error":
@@ -55,7 +56,7 @@ func processInput(b *tb.Bot) func(context.Context, *tb.Message) error {
5556
return tbcomctl.NewInputError("wrong input")
5657
}
5758
if ctrl, ok := tbcomctl.ControllerFromCtx(ctx); ok {
58-
log.Println("form values so far: ", ctrl.Form().Data(m.Sender))
59+
log.Println("form values so far: ", ctrl.Form().Data(c.Sender()))
5960
}
6061
return nil
6162
}

examples/picklist/main.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"time"
99

1010
"github.com/joho/godotenv"
11-
"github.com/rusq/tbcomctl"
12-
tb "gopkg.in/tucnak/telebot.v2"
11+
"github.com/rusq/tbcomctl/v3"
12+
tb "gopkg.in/tucnak/telebot.v3"
1313
)
1414

1515
var _ = godotenv.Load()
@@ -28,31 +28,30 @@ func main() {
2828
log.Fatal(err)
2929
}
3030
p1 := tbcomctl.NewPicklistText(
31-
b,
3231
"1",
3332
"first picklist",
3433
[]string{"1", "2", "3", "4"},
35-
func(ctx context.Context, cb *tb.Callback) error {
36-
fmt.Println(tbcomctl.Sdump(cb))
34+
func(ctx context.Context, c tb.Context) error {
35+
fmt.Println(tbcomctl.Sdump(c.Callback()))
3736
return nil
3837
},
3938
)
4039
p2 := tbcomctl.NewPicklistText(
41-
b,
4240
"2",
4341
"second picklist",
4442
[]string{"5", "6", "7", "8"},
45-
func(ctx context.Context, cb *tb.Callback) error {
46-
fmt.Println(tbcomctl.Sdump(cb))
43+
func(ctx context.Context, c tb.Context) error {
44+
fmt.Println(tbcomctl.Sdump(c.Callback()))
4745
return nil
4846
},
4947
tbcomctl.PickOptBtnPattern([]uint{1, 2, 1}),
5048
)
51-
m := tbcomctl.NewMessageText(b, "msg", "all ok")
49+
m := tbcomctl.NewMessageText("msg", "all ok")
5250
form := tbcomctl.NewForm(p1, p2, m).
5351
SetOverwrite(true).
5452
SetRemoveButtons(true)
5553
b.Handle("/picklist", form.Handler)
5654

55+
log.Println("ready, send /picklist")
5756
b.Start()
5857
}

examples/rating/main.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package main
33
import (
44
"log"
55
"os"
6+
"strconv"
67
"time"
78

89
"github.com/joho/godotenv"
9-
"github.com/rusq/tbcomctl"
10-
tb "gopkg.in/tucnak/telebot.v2"
10+
"github.com/rusq/tbcomctl/v3"
11+
tb "gopkg.in/tucnak/telebot.v3"
1112
)
1213

1314
var _ = godotenv.Load()
@@ -29,7 +30,7 @@ func main() {
2930
log.Fatal(err)
3031
}
3132

32-
rb := tbcomctl.NewRating(b,
33+
rb := tbcomctl.NewRating(
3334
func(e tb.Editable, r *tb.User, idx int) ([2]tbcomctl.Button, error) {
3435
mid, cid := e.MessageSig()
3536
log.Printf("%s, %d: u: %s, idx %d", mid, cid, r.Recipient(), idx)
@@ -41,12 +42,17 @@ func main() {
4142
tbcomctl.RBOptShowVoteCounter(true),
4243
)
4344

45+
iChat, err := strconv.ParseInt(chat, 10, 64)
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
4450
go func() {
45-
ch, err := b.ChatByID(chat)
51+
ch, err := b.ChatByID(iChat)
4652
if err != nil {
4753
log.Fatal(err)
4854
}
49-
if _, err := b.Send(ch, "rating test", rb.Markup(ratingButtons())); err != nil {
55+
if _, err := b.Send(ch, "rating test", rb.Markup(b, ratingButtons())); err != nil {
5056
log.Fatal(err)
5157
}
5258
}()

examples/subcheck/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66
"time"
77

88
"github.com/joho/godotenv"
9-
"github.com/rusq/tbcomctl"
10-
tb "gopkg.in/tucnak/telebot.v2"
9+
"github.com/rusq/osenv"
10+
"github.com/rusq/tbcomctl/v3"
11+
tb "gopkg.in/tucnak/telebot.v3"
1112
)
1213

1314
var _ = godotenv.Load()
1415

1516
var (
1617
token = os.Getenv("TOKEN")
17-
chat = os.Getenv("CHAT")
18+
chat = osenv.Int64("CHAT", 0)
1819
)
1920

2021
func main() {
@@ -27,7 +28,7 @@ func main() {
2728
log.Fatal(err)
2829
}
2930

30-
sc := tbcomctl.NewSubChecker(b, "sc", tbcomctl.TextFn("test sub"), []string{chat})
31+
sc := tbcomctl.NewSubChecker("sc", tbcomctl.TextFn("test sub"), []int64{chat})
3132

3233
b.Handle("/subcheck", sc.Handler)
3334

examples/template/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"time"
77

88
"github.com/joho/godotenv"
9-
tb "gopkg.in/tucnak/telebot.v2"
9+
tb "gopkg.in/tucnak/telebot.v3"
1010
)
1111

1212
var _ = godotenv.Load()

form.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tbcomctl
22

33
import (
4-
tb "gopkg.in/tucnak/telebot.v2"
4+
tb "gopkg.in/tucnak/telebot.v3"
55
)
66

77
type Form struct {
@@ -59,8 +59,8 @@ func (fm *Form) SetRemoveButtons(b bool) *Form {
5959
return fm
6060
}
6161

62-
func (fm *Form) Handler(m *tb.Message) {
63-
fm.ctrls[0].Handler(m)
62+
func (fm *Form) Handler(c tb.Context) error {
63+
return fm.ctrls[0].Handler(c)
6464
}
6565

6666
// Controller returns the Form Controller by it's name.
@@ -70,11 +70,11 @@ func (fm *Form) Controller(name string) (Controller, bool) {
7070
}
7171

7272
type onTexter interface {
73-
OnTextMw(fn func(m *tb.Message)) func(*tb.Message)
73+
OnTextMw(fn tb.HandlerFunc) tb.HandlerFunc
7474
}
7575

7676
// OnTextMiddleware returns the middleware for OnText handler.
77-
func (fm *Form) OnTextMiddleware(onText func(m *tb.Message)) func(m *tb.Message) {
77+
func (fm *Form) OnTextMiddleware(onText tb.HandlerFunc) tb.HandlerFunc {
7878
var mwfn []MiddlewareFunc
7979
for _, ctrl := range fm.ctrls {
8080
otmw, ok := ctrl.(onTexter) // if the control satisfies onTexter, it contains middleware function
@@ -86,7 +86,7 @@ func (fm *Form) OnTextMiddleware(onText func(m *tb.Message)) func(m *tb.Message)
8686
return middlewareChain(onText, mwfn...)
8787
}
8888

89-
func middlewareChain(final func(m *tb.Message), mw ...MiddlewareFunc) func(m *tb.Message) {
89+
func middlewareChain(final tb.HandlerFunc, mw ...MiddlewareFunc) tb.HandlerFunc {
9090
var handler = final
9191
for i := len(mw) - 1; i >= 0; i-- {
9292
handler = mw[i](handler)

go.mod

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
module github.com/rusq/tbcomctl
1+
module github.com/rusq/tbcomctl/v3
22

33
go 1.15
44

55
require (
6-
github.com/google/uuid v1.2.0
7-
github.com/joho/godotenv v1.3.0
8-
github.com/rusq/dlog v1.3.0
6+
github.com/google/uuid v1.3.0
7+
github.com/joho/godotenv v1.4.0
8+
github.com/pkg/errors v0.9.1 // indirect
9+
github.com/rusq/dlog v1.3.3
10+
github.com/rusq/osenv v0.1.1
911
github.com/stretchr/testify v1.5.1
10-
golang.org/x/text v0.3.5
11-
gopkg.in/tucnak/telebot.v2 v2.3.5
12+
golang.org/x/text v0.3.7
13+
gopkg.in/tucnak/telebot.v3 v3.0.0-20210930165304-9a08d98f4ee7
1214
)

0 commit comments

Comments
 (0)