Skip to content

Commit

Permalink
Merge branch 'develop' into verbose-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
demget authored Jun 9, 2020
2 parents 75901ba + 98a898f commit 29af30f
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 164 deletions.
2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (b *Bot) sendText(to Recipient, text string, opt *SendOptions) (*Message, e
"chat_id": to.Recipient(),
"text": text,
}
embedSendOptions(params, opt)
b.embedSendOptions(params, opt)

data, err := b.Raw("sendMessage", params)
if err != nil {
Expand Down
58 changes: 43 additions & 15 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewBot(pref Settings) (*Bot, error) {
handlers: make(map[string]interface{}),
synchronous: pref.Synchronous,
verbose: pref.Verbose,
parseMode: pref.ParseMode,
stop: make(chan struct{}),
reporter: pref.Reporter,
client: client,
Expand Down Expand Up @@ -67,6 +68,7 @@ type Bot struct {
handlers map[string]interface{}
synchronous bool
verbose bool
parseMode ParseMode
reporter func(error)
stop chan struct{}
client *http.Client
Expand Down Expand Up @@ -95,6 +97,11 @@ type Settings struct {
// Shows upcoming requests
Verbose bool

// ParseMode used to set default parse mode of all sent messages.
// It attaches to every send, edit or whatever method. You also
// will be able to override the default mode by passing a new one.
ParseMode ParseMode

// Reporter is a callback function that will get called
// on any panics recovered from endpoint handlers.
Reporter func(error)
Expand Down Expand Up @@ -524,7 +531,7 @@ func (b *Bot) Send(to Recipient, what interface{}, options ...interface{}) (*Mes

// SendAlbum sends multiple instances of media as a single message.
//
// From all existing options, it only supports tb.Silent option.
// From all existing options, it only supports tb.Silent.
func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Message, error) {
if to == nil {
return nil, ErrBadRecipient
Expand Down Expand Up @@ -596,7 +603,7 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
}

sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
b.embedSendOptions(params, sendOpts)

data, err := b.sendFiles("sendMediaGroup", files, params)
if err != nil {
Expand Down Expand Up @@ -655,7 +662,7 @@ func (b *Bot) Forward(to Recipient, msg Editable, options ...interface{}) (*Mess
}

sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw("forwardMessage", params)
if err != nil {
Expand All @@ -667,11 +674,16 @@ func (b *Bot) Forward(to Recipient, msg Editable, options ...interface{}) (*Mess

// Edit is magic, it lets you change already sent message.
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// Use cases:
//
// b.Edit(msg, msg.Text, newMarkup)
// b.Edit(msg, "new <b>text</b>", tb.ModeHTML)
// b.Edit(msg, tb.Location{42.1337, 69.4242})
// b.Edit(m, m.Text, newMarkup)
// b.Edit(m, "new <b>text</b>", tb.ModeHTML)
// b.Edit(m, &tb.ReplyMarkup{...})
// b.Edit(m, &tb.Photo{File: ...})
// b.Edit(m, tb.Location{42.1337, 69.4242})
//
// This function will panic upon nil Editable.
func (b *Bot) Edit(msg Editable, what interface{}, options ...interface{}) (*Message, error) {
Expand All @@ -681,6 +693,10 @@ func (b *Bot) Edit(msg Editable, what interface{}, options ...interface{}) (*Mes
)

switch v := what.(type) {
case *ReplyMarkup:
return b.EditReplyMarkup(msg, v)
case InputMedia:
return b.EditMedia(msg, v, options...)
case string:
method = "editMessageText"
params["text"] = v
Expand All @@ -702,7 +718,7 @@ func (b *Bot) Edit(msg Editable, what interface{}, options ...interface{}) (*Mes
}

sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw(method, params)
if err != nil {
Expand All @@ -715,6 +731,9 @@ func (b *Bot) Edit(msg Editable, what interface{}, options ...interface{}) (*Mes
// EditReplyMarkup edits reply markup of already sent message.
// Pass nil or empty ReplyMarkup to delete it from the message.
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// On success, returns edited message object.
// This function will panic upon nil Editable.
func (b *Bot) EditReplyMarkup(msg Editable, markup *ReplyMarkup) (*Message, error) {
Expand Down Expand Up @@ -747,6 +766,9 @@ func (b *Bot) EditReplyMarkup(msg Editable, markup *ReplyMarkup) (*Message, erro

// EditCaption edits already sent photo caption with known recipient and message id.
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// On success, returns edited message object.
// This function will panic upon nil Editable.
func (b *Bot) EditCaption(msg Editable, caption string, options ...interface{}) (*Message, error) {
Expand All @@ -764,7 +786,7 @@ func (b *Bot) EditCaption(msg Editable, caption string, options ...interface{})
}

sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw("editMessageCaption", params)
if err != nil {
Expand All @@ -776,10 +798,13 @@ func (b *Bot) EditCaption(msg Editable, caption string, options ...interface{})

// EditMedia edits already sent media with known recipient and message id.
//
// If edited message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// Use cases:
//
// bot.EditMedia(msg, &tb.Photo{File: tb.FromDisk("chicken.jpg")})
// bot.EditMedia(msg, &tb.Video{File: tb.FromURL("http://video.mp4")})
// b.EditMedia(m, &tb.Photo{File: tb.FromDisk("chicken.jpg")})
// b.EditMedia(m, &tb.Video{File: tb.FromURL("http://video.mp4")})
//
// This function will panic upon nil Editable.
func (b *Bot) EditMedia(msg Editable, media InputMedia, options ...interface{}) (*Message, error) {
Expand Down Expand Up @@ -875,7 +900,7 @@ func (b *Bot) EditMedia(msg Editable, media InputMedia, options ...interface{})
params := make(map[string]string)

sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
b.embedSendOptions(params, sendOpts)

if sendOpts != nil {
result.ParseMode = sendOpts.ParseMode
Expand All @@ -888,7 +913,7 @@ func (b *Bot) EditMedia(msg Editable, media InputMedia, options ...interface{})
data, _ := json.Marshal(result)
params["media"] = string(data)

if chatID == 0 { // If inline message.
if chatID == 0 { // if inline message
params["inline_message_id"] = msgID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
Expand Down Expand Up @@ -1121,6 +1146,9 @@ func (b *Bot) GetFile(file *File) (io.ReadCloser, error) {
// StopLiveLocation stops broadcasting live message location
// before Location.LivePeriod expires.
//
// If the message is sent by the bot, returns it,
// otherwise returns nil and ErrTrueResult.
//
// It supports tb.ReplyMarkup.
// This function will panic upon nil Editable.
func (b *Bot) StopLiveLocation(msg Editable, options ...interface{}) (*Message, error) {
Expand All @@ -1132,7 +1160,7 @@ func (b *Bot) StopLiveLocation(msg Editable, options ...interface{}) (*Message,
}

sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw("stopMessageLiveLocation", params)
if err != nil {
Expand All @@ -1156,7 +1184,7 @@ func (b *Bot) StopPoll(msg Editable, options ...interface{}) (*Poll, error) {
}

sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
b.embedSendOptions(params, sendOpts)

data, err := b.Raw("stopPoll", params)
if err != nil {
Expand Down Expand Up @@ -1289,7 +1317,7 @@ func (b *Bot) Pin(msg Editable, options ...interface{}) error {
}

sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
b.embedSendOptions(params, sendOpts)

_, err := b.Raw("pinChatMessage", params)
return err
Expand Down
73 changes: 44 additions & 29 deletions bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestNewBot(t *testing.T) {
pref.Client = client
pref.Poller = &LongPoller{Timeout: time.Second}
pref.Updates = 50
pref.ParseMode = ModeHTML
pref.offline = true

b, err = NewBot(pref)
Expand All @@ -71,6 +72,7 @@ func TestNewBot(t *testing.T) {
assert.Equal(t, pref.URL, b.URL)
assert.Equal(t, pref.Poller, b.Poller)
assert.Equal(t, 50, cap(b.Updates))
assert.Equal(t, ModeHTML, b.parseMode)
}

func TestBotHandle(t *testing.T) {
Expand Down Expand Up @@ -309,23 +311,36 @@ func TestBot(t *testing.T) {
_, err = b.Forward(nil, nil)
assert.Equal(t, ErrBadRecipient, err)

t.Run("Send(what=Sendable)", func(t *testing.T) {
photo := &Photo{
File: File{FileID: photoID},
Caption: t.Name(),
}
photo := &Photo{
File: File{FileID: photoID},
Caption: t.Name(),
}
var msg *Message

msg, err := b.Send(to, photo)
t.Run("Send(what=Sendable)", func(t *testing.T) {
msg, err = b.Send(to, photo)
assert.NoError(t, err)
assert.NotNil(t, msg.Photo)
assert.Equal(t, photo.Caption, msg.Caption)
})

t.Run("EditCaption()+ParseMode", func(t *testing.T) {
b.parseMode = ModeHTML
edited, err := b.EditCaption(msg, "<b>new caption with parse mode</b>")
assert.NoError(t, err)
assert.Equal(t, "new caption with parse mode", edited.Caption)

msg, err = b.EditCaption(msg, "new caption")
b.parseMode = ModeDefault
edited, err = b.EditCaption(msg, "*new caption w/o parse mode*", ModeMarkdown)
assert.NoError(t, err)
assert.Equal(t, "new caption", msg.Caption)
assert.Equal(t, "new caption w/o parse mode", edited.Caption)
})

var msg *Message
t.Run("Edit(what=InputMedia)", func(t *testing.T) {
edited, err := b.Edit(msg, photo)
assert.NoError(t, err)
assert.Equal(t, edited.Photo.FileID, photo.FileID)
})

t.Run("Send(what=string)", func(t *testing.T) {
msg, err = b.Send(to, t.Name())
Expand Down Expand Up @@ -358,28 +373,16 @@ func TestBot(t *testing.T) {
assert.Error(t, err) // message is not modified
})

t.Run("Edit(what=Location)", func(t *testing.T) {
loc := &Location{Lat: 42, Lng: 69, LivePeriod: 60}
msg, err := b.Send(to, loc)
assert.NoError(t, err)
assert.NotNil(t, msg.Location)

loc = &Location{Lat: loc.Lng, Lng: loc.Lat}
msg, err = b.Edit(msg, *loc)
assert.NoError(t, err)
assert.NotNil(t, msg.Location)
})

t.Run("EditReplyMarkup()", func(t *testing.T) {
markup := &ReplyMarkup{
t.Run("Edit(what=ReplyMarkup)", func(t *testing.T) {
good := &ReplyMarkup{
InlineKeyboard: [][]InlineButton{
{{
Data: "btn",
Text: "Hi Telebot!",
}},
},
}
badMarkup := &ReplyMarkup{
bad := &ReplyMarkup{
InlineKeyboard: [][]InlineButton{
{{
Data: strings.Repeat("*", 65),
Expand All @@ -388,18 +391,30 @@ func TestBot(t *testing.T) {
},
}

msg, err := b.EditReplyMarkup(msg, markup)
edited, err := b.Edit(msg, good)
assert.NoError(t, err)
assert.Equal(t, msg.ReplyMarkup.InlineKeyboard, markup.InlineKeyboard)
assert.Equal(t, edited.ReplyMarkup.InlineKeyboard, good.InlineKeyboard)

msg, err = b.EditReplyMarkup(msg, nil)
edited, err = b.EditReplyMarkup(edited, nil)
assert.NoError(t, err)
assert.Nil(t, msg.ReplyMarkup.InlineKeyboard)
assert.Nil(t, edited.ReplyMarkup.InlineKeyboard)

_, err = b.EditReplyMarkup(msg, badMarkup)
_, err = b.Edit(edited, bad)
assert.Equal(t, ErrButtonDataInvalid, err)
})

t.Run("Edit(what=Location)", func(t *testing.T) {
loc := &Location{Lat: 42, Lng: 69, LivePeriod: 60}
edited, err := b.Send(to, loc)
assert.NoError(t, err)
assert.NotNil(t, edited.Location)

loc = &Location{Lat: loc.Lng, Lng: loc.Lat}
edited, err = b.Edit(edited, *loc)
assert.NoError(t, err)
assert.NotNil(t, edited.Location)
})

t.Run("Notify()", func(t *testing.T) {
assert.Equal(t, ErrBadRecipient, b.Notify(nil, Typing))
assert.NoError(t, b.Notify(to, Typing))
Expand Down
Loading

0 comments on commit 29af30f

Please sign in to comment.