44 "errors"
55 "fmt"
66
7- tb "gopkg.in/tucnak/telebot.v2 "
7+ tb "gopkg.in/tucnak/telebot.v3 "
88)
99
1010type buttons struct {
@@ -20,8 +20,7 @@ func (b *buttons) SetMaxButtons(n int) {
2020
2121type PostButtons struct {
2222 * buttons
23- b Boter
24- cbFn func (cb * tb.Callback )
23+ cbFn tb.HandlerFunc
2524}
2625
2726type 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}
0 commit comments