chat: create ChatID Recipient type

pull/285/head^2
Demian 4 years ago
parent 1157f8b301
commit 183d6484d9

@ -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)
})

@ -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)
}

@ -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())
}
Loading…
Cancel
Save