diff --git a/api.go b/api.go index 356a265..6e6626e 100644 --- a/api.go +++ b/api.go @@ -115,7 +115,7 @@ func getMe(token string) (User, error) { err = json.Unmarshal(meJSON, &botInfo) if err != nil { - return User{}, err + return User{}, AuthError{"invalid token"} } if botInfo.Ok { diff --git a/bot.go b/bot.go index 1595785..04428c0 100644 --- a/bot.go +++ b/bot.go @@ -163,7 +163,9 @@ func (b Bot) SendPhoto(recipient User, photo *Photo, options *SendOptions) error } thumbnails := &responseRecieved.Result.Photo + filename := photo.filename photo.File = (*thumbnails)[len(*thumbnails)-1].File + photo.filename = filename return nil } @@ -212,7 +214,9 @@ func (b Bot) SendAudio(recipient User, audio *Audio, options *SendOptions) error return SendError{responseRecieved.Description} } + filename := audio.filename *audio = responseRecieved.Result.Audio + audio.filename = filename return nil } @@ -261,7 +265,9 @@ func (b Bot) SendDocument(recipient User, doc *Document, options *SendOptions) e return SendError{responseRecieved.Description} } + filename := doc.filename *doc = responseRecieved.Result.Document + doc.filename = filename return nil } @@ -310,7 +316,9 @@ func (b *Bot) SendSticker(recipient User, sticker *Sticker, options *SendOptions return SendError{responseRecieved.Description} } + filename := sticker.filename *sticker = responseRecieved.Result.Sticker + sticker.filename = filename return nil } @@ -359,7 +367,9 @@ func (b Bot) SendVideo(recipient User, video *Video, options *SendOptions) error return SendError{responseRecieved.Description} } + filename := video.filename *video = responseRecieved.Result.Video + video.filename = filename return nil } diff --git a/file.go b/file.go index 47e8b72..2e2cbb4 100644 --- a/file.go +++ b/file.go @@ -10,8 +10,7 @@ type File struct { FileID string `json:"file_id"` FileSize int `json:"file_size"` - // Local absolute path to file on file system. Valid only for - // new files, meant to be uploaded soon. + // Local absolute path to file on local file system. filename string } @@ -30,11 +29,17 @@ func NewFile(path string) (File, error) { return File{filename: path}, nil } -// Exists says whether file presents on Telegram servers or not. +// Exists says whether the file presents on Telegram servers or not. func (f File) Exists() bool { - if f.filename == "" { + if f.FileID != "" { return true } return false } + +// Local returns location of file on local file system, if it's +// actually there, otherwise returns empty string. +func (f File) Local() string { + return f.filename +} diff --git a/telebot_test.go b/telebot_test.go index 7adb06d..ae3a545 100644 --- a/telebot_test.go +++ b/telebot_test.go @@ -6,7 +6,11 @@ import ( "testing" ) -func TestTelebot(t *testing.T) { +func TestBot(t *testing.T) { + if testing.Short() { + t.Skip("skipping test in short mode.") + } + token := os.Getenv("TELEBOT_SECRET") if token == "" { fmt.Println("ERROR: " + @@ -17,28 +21,43 @@ func TestTelebot(t *testing.T) { } _, err := NewBot(token) + if err != nil { + t.Fatal("Couldn't create bot:", err) + } +} + +func TestFile(t *testing.T) { + file, err := NewFile("telebot.go") if err != nil { t.Fatal(err) } - // TODO: Uncomment when Telegram fixes behavior for self-messaging + if file.Exists() { + t.Fatal("Newly created file can't exist on Telegram servers!") + } - /*messages := make(chan Message) + file.FileID = "magic" - intelligence := "welcome to the jungle" + if !file.Exists() { + t.Fatal("File with defined FileID is supposed to exist, fail.") + } - bot.SendMessage(bot.Identity, intelligence) - bot.Listen(messages, 1*time.Second) - - select { - case message := <-messages: - { - if message.Text != intelligence { - t.Error("Self-handshake failed.") - } - } - - case <-time.After(5 * time.Second): - t.Error("Self-handshake test took too long, aborting.") - }*/ + if file.Local() != "telebot.go" { + t.Fatal("File doesn't preserve its original filename.") + } +} + +func TestUser(t *testing.T) { + user := User{Title: "bazinga"} + + // According to API, user object with non-empty Title is a group chat. + if !user.IsGroupChat() { + t.Fatal("Can't tell users/bots and group chats apart!") + } + + // Reverse. + user.Title = "" + if user.IsGroupChat() { + t.Fatal("Can't tell users/bots and group chats apart!") + } } diff --git a/types.go b/types.go index 210f3d3..9216a6c 100644 --- a/types.go +++ b/types.go @@ -1,16 +1,27 @@ package telebot // User object represents a Telegram user, bot or group chat. +// +// Title field differs a group chat apart from users and bots: +// object represents a group chat if Title is empty. type User struct { ID int `json:"id"` FirstName string `json:"first_name"` LastName string `json:"last_name"` Username string `json:"username"` - // Title differs a group chat apart from users and bots. Title string `json:"title"` } +// IsGroupChat returns true if user object represents a group chat. +func (u User) IsGroupChat() bool { + if u.Title != "" { + return true + } + + return false +} + // Update object represents an incoming update. type Update struct { ID int `json:"update_id"`