From 183d6484d924526ec19aa426d917a09a2d5fd1b7 Mon Sep 17 00:00:00 2001 From: Demian Date: Tue, 5 May 2020 22:24:40 +0300 Subject: [PATCH] chat: create ChatID Recipient type --- bot_test.go | 2 +- chat.go | 22 ++++++++++++++++++++++ chat_test.go | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 chat_test.go diff --git a/bot_test.go b/bot_test.go index 4073301..5b34963 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 ac26dea..a57934c 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 0000000..c1a7f88 --- /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()) +}