context: add Args helper function

pull/341/head
Demian 4 years ago
parent 69af851173
commit a69dd5aa62

@ -1,6 +1,7 @@
package telebot
import (
"errors"
"net/http"
"os"
"strconv"
@ -340,6 +341,25 @@ func TestBotProcessUpdate(t *testing.T) {
b.ProcessUpdate(Update{PollAnswer: &PollAnswer{PollID: "poll"}})
}
func TestBotOnError(t *testing.T) {
b, err := NewBot(Settings{Synchronous: true, offline: true})
if err != nil {
t.Fatal(err)
}
var ok bool
b.OnError = func(err error) {
assert.NotNil(t, err)
ok = true
}
b.runHandler(func(c Context) error {
return errors.New("not nil")
}, &nativeContext{})
assert.True(t, ok)
}
func TestBot(t *testing.T) {
if b == nil {
t.Skip("Cached bot instance is bad (probably wrong or empty TELEBOT_SECRET)")

@ -1,6 +1,10 @@
package telebot
import "github.com/pkg/errors"
import (
"strings"
"github.com/pkg/errors"
)
// HandlerFunc represents a handler function type
// which is used to handle endpoints.
@ -45,6 +49,10 @@ type Context interface {
// In the case when no related data presented, returns an empty string.
Text() string
// Args returns a raw slice of command or callback arguments as strings.
// The message arguments split by space, while the callback's ones by a "|" symbol.
Args() []string
// Send sends a message to the current recipient.
// See Send from bot.go.
Send(what interface{}, opts ...interface{}) error
@ -192,6 +200,16 @@ func (c *nativeContext) Text() string {
}
}
func (c *nativeContext) Args() []string {
if c.message != nil {
return strings.Split(c.message.Payload, " ")
}
if c.callback != nil {
return strings.Split(c.callback.Data, "|")
}
return nil
}
func (c *nativeContext) Send(what interface{}, opts ...interface{}) error {
_, err := c.b.Send(c.Sender(), what, opts...)
return err

Loading…
Cancel
Save