diff --git a/bot_test.go b/bot_test.go index 4073301c..5b349630 100644 --- a/bot_test.go +++ b/bot_test.go @@ -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) }) diff --git a/chat.go b/chat.go index ac26deae..a57934c3 100644 --- a/chat.go +++ b/chat.go @@ -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) +} diff --git a/chat_test.go b/chat_test.go new file mode 100644 index 00000000..c1a7f882 --- /dev/null +++ b/chat_test.go @@ -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()) +}