Added some API and tests

This commit is contained in:
Ilya Kowalewski 2015-07-06 19:58:54 +03:00
parent eaf9b17a54
commit 40c91ee0b4
5 changed files with 69 additions and 24 deletions

2
api.go
View File

@ -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 {

10
bot.go
View File

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

13
file.go
View File

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

View File

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

View File

@ -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"`