Skip to content

Commit

Permalink
chat: create ChatID Recipient type
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed May 5, 2020
1 parent 1157f8b commit 183d648
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func TestBot(t *testing.T) {
assert.True(t, fwd.IsForwarded())

fwd.ID += 1 // nonexistent message
fwd, err = b.Forward(to, fwd)
_, err = b.Forward(to, fwd)
assert.Equal(t, ErrToForwardNotFound, err)
})

Expand Down
22 changes: 22 additions & 0 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,25 @@ type ChatMember struct {
//
RestrictedUntil int64 `json:"until_date,omitempty"`
}

// ChatID represents a chat or an user integer ID, which can be used
// as recipient in bot methods. It is very useful in cases where
// you have special group IDs, for example in your config, and don't
// want to wrap it into *tb.Chat every time you send messages.
//
// Example:
//
// group := tb.ChatID(-100756389456)
// b.Send(group, "Hello!")
//
// type Config struct {
// AdminGroup tb.ChatID `json:"admin_group"`
// }
// b.Send(conf.AdminGroup, "Hello!")
//
type ChatID int64

// Recipient returns chat ID (see Recipient interface).
func (i ChatID) Recipient() string {
return strconv.FormatInt(int64(i), 10)
}
21 changes: 21 additions & 0 deletions chat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package telebot

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestChat(t *testing.T) {
user := &User{ID: 1}
chat := &Chat{ID: 1}
chatID := ChatID(1)

assert.Implements(t, (*Recipient)(nil), user)
assert.Implements(t, (*Recipient)(nil), chat)
assert.Implements(t, (*Recipient)(nil), chatID)

assert.Equal(t, "1", user.Recipient())
assert.Equal(t, "1", chat.Recipient())
assert.Equal(t, "1", chatID.Recipient())
}

0 comments on commit 183d648

Please sign in to comment.