Skip to content

Commit 5a2f450

Browse files
committed
tests: TType, more types ...
1 parent a6acaaf commit 5a2f450

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+673
-221
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ go get -u go.mamad.dev/telebot
4040
- [ ] Regex Route Support
4141
- [ ] Code Cleanup and Refactoring
4242
- [ ] 100% Test Coverage
43+
- [ ] Better Router
4344

4445

animation.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package telebot
22

3+
import "fmt"
4+
35
// Animation represents an animation file (GIF or H.264/MPEG-4 AVC video without sound).
46
type Animation struct {
57
// FileID is the identifier for this file, which can be used to download or reuse the file.
@@ -29,5 +31,16 @@ type Animation struct {
2931

3032
// FileSize is the file size in bytes. It can be bigger than 2^31, and some programming languages may have difficulty/silent defects in interpreting it.
3133
// But it has at most 52 significant bits, so a signed 64-bit integer or double-precision float type are safe for storing this value (optional).
32-
FileSize int `json:"file_size,omitempty"`
34+
FileSize int64 `json:"file_size,omitempty"`
35+
}
36+
37+
func (c *Animation) ReflectType() string { return fmt.Sprintf("%T", c) }
38+
func (c *Animation) Type() string { return "Animation" }
39+
40+
func (a *Animation) File() *File {
41+
f := FromFileID(a.FileID)
42+
f.UniqueID = a.FileUniqueID
43+
f.FileSize = a.FileSize
44+
f.fileName = a.FileName
45+
return &f
3346
}

audio.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package telebot
22

3+
import "fmt"
4+
35
// Audio represents an audio file to be treated as music by the Telegram clients.
46
type Audio struct {
57
// FileID is the identifier for this file, which can be used to download or reuse the file.
@@ -26,8 +28,19 @@ type Audio struct {
2628

2729
// FileSize is the file size in bytes. It can be bigger than 2^31, and some programming languages may have difficulty/silent defects in interpreting it.
2830
// But it has at most 52 significant bits, so a signed 64-bit integer or double-precision float type are safe for storing this value (optional).
29-
FileSize int `json:"file_size,omitempty"`
31+
FileSize int64 `json:"file_size,omitempty"`
3032

3133
// Thumbnail is the thumbnail of the album cover to which the music file belongs (optional).
3234
Thumbnail *PhotoSize `json:"thumbnail,omitempty"`
3335
}
36+
37+
func (c *Audio) ReflectType() string { return fmt.Sprintf("%T", c) }
38+
func (c *Audio) Type() string { return "Audio" }
39+
40+
func (c *Audio) File() *File {
41+
f := FromFileID(c.FileID)
42+
f.fileName = c.FileName
43+
f.FileSize = c.FileSize
44+
f.UniqueID = c.FileUniqueID
45+
return &f
46+
}

auto_delete_timer_changed.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package telebot
22

3+
import "fmt"
4+
35
// AutoDeleteTimerChanged represents a service message about a change in auto-delete timer settings.
46
type AutoDeleteTimerChanged struct {
57
// AutoDeleteTime is the new auto-delete time for messages in the chat; in seconds.
68
AutoDeleteTime int `json:"message_auto_delete_time"`
79
}
10+
11+
func (c *AutoDeleteTimerChanged) ReflectType() string { return fmt.Sprintf("%T", c) }
12+
func (c *AutoDeleteTimerChanged) Type() string { return "AutoDeleteTimerChanged" }

chat_shared.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package telebot
22

3+
import "fmt"
4+
35
// ChatShared contains information about the chat whose identifier was shared with the bot
46
// using a KeyboardButtonRequestChat button.
57
type ChatShared struct {
@@ -12,3 +14,6 @@ type ChatShared struct {
1214
// The bot may not have access to the chat and could be unable to use this identifier unless the chat is already known to the bot by some other means.
1315
ChatID int64 `json:"chat_id"`
1416
}
17+
18+
func (c *ChatShared) ReflectType() string { return fmt.Sprintf("%T", c) }
19+
func (c *ChatShared) Type() string { return "ChatShared" }

contact.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package telebot
22

3+
import "fmt"
4+
35
// Contact represents a phone contact.
46
type Contact struct {
57
// PhoneNumber is the contact's phone number.
@@ -19,3 +21,6 @@ type Contact struct {
1921
// VCard is an optional field providing additional data about the contact in the form of a vCard.
2022
VCard *string `json:"vcard,omitempty"`
2123
}
24+
25+
func (c *Contact) ReflectType() string { return fmt.Sprintf("%T", c) }
26+
func (c *Contact) Type() string { return "Contact" }

dice.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package telebot
22

3+
import "fmt"
4+
35
// Dice represents an animated emoji that displays a random value.
46
type Dice struct {
57
// Emoji is the emoji on which the dice throw animation is based.
@@ -11,3 +13,6 @@ type Dice struct {
1113
// For “🎰” base emoji, the value ranges from 1 to 64.
1214
Value int `json:"value"`
1315
}
16+
17+
func (c *Dice) ReflectType() string { return fmt.Sprintf("%T", c) }
18+
func (c *Dice) Type() string { return "Dice" }

document.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package telebot
22

3+
import "fmt"
4+
35
// Document represents a general file (as opposed to photos, voice messages, and audio files).
46
type Document struct {
57
// FileID is the identifier for this file, which can be used to download or reuse the file.
@@ -20,5 +22,16 @@ type Document struct {
2022

2123
// FileSize is the file size in bytes. It can be bigger than 2^31, and some programming languages may have difficulty/silent defects in interpreting it.
2224
// But it has at most 52 significant bits, so a signed 64-bit integer or double-precision float type is safe for storing this value (optional).
23-
FileSize int `json:"file_size,omitempty"`
25+
FileSize int64 `json:"file_size,omitempty"`
26+
}
27+
28+
func (c *Document) ReflectType() string { return fmt.Sprintf("%T", c) }
29+
func (c *Document) Type() string { return "Document" }
30+
31+
func (c *Document) File() *File {
32+
f := FromFileID(c.FileID)
33+
f.fileName = c.FileName
34+
f.FileSize = c.FileSize
35+
f.UniqueID = c.FileUniqueID
36+
return &f
2437
}

examples/echo/main.go

+26-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package main
22

3-
//
4-
//import (
5-
// tele "go.mamad.dev/telebot"
6-
// "os"
7-
//)
8-
//
9-
//func main() {
10-
// tg := tele.New(tele.BotSettings{
11-
// Token: os.Getenv("TELEGRAM_TOKEN"),
12-
// })
13-
//
14-
// tg.Handle("/start", func(c tele.Context) error {
15-
// return c.Send("Hello Sir, I'm Echo OldBot, Please send me something to echo.")
16-
// })
17-
//
18-
// tg.Handle(tele.OnText, func(c tele.Context) error {
19-
// return c.Send(c.Text())
20-
// })
21-
//
22-
// tg.Start()
23-
//}
3+
import (
4+
tele "go.mamad.dev/telebot"
5+
"os"
6+
)
7+
8+
func main() {
9+
tg := tele.New(tele.BotSettings{
10+
Token: os.Getenv("TELEGRAM_TOKEN"),
11+
})
12+
13+
tg.Handle("/start", func(c tele.Context) error {
14+
// return c.Send("Hello Sir, I'm Echo OldBot, Please send me something to echo.")
15+
16+
_, err := tg.SendMessage(c.Message().Chat, "Hello Sir, I'm Echo OldBot, Please send me something to echo.")
17+
return err
18+
})
19+
20+
tg.Handle(tele.OnText, func(c tele.Context) error {
21+
// return c.Send(c.Text())
22+
23+
_, err := tg.SendMessage(c.Message().Chat, *c.Message().Text)
24+
return err
25+
})
26+
27+
tg.Start()
28+
}

file.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package telebot
2+
3+
type IFile interface {
4+
File() *File
5+
}

files.go

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package telebot
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"io"
76
"os"
@@ -28,11 +27,6 @@ type File struct {
2827
fileName string
2928
}
3029

31-
type UserProfilePhotos struct {
32-
TotalCount int `json:"total_count"`
33-
Photos [][]PhotoSize `json:"photos"`
34-
}
35-
3630
// FromDisk constructs a new local (on-disk) file object.
3731
//
3832
// Note, it returns File, not *File for a very good reason:
@@ -69,6 +63,11 @@ func FromReader(reader io.Reader) File {
6963
return File{FileReader: reader}
7064
}
7165

66+
// FromFileID constructs a new file from file_id.
67+
func FromFileID(fileID string) File {
68+
return File{FileID: fileID}
69+
}
70+
7271
func (f *File) stealRef(g *File) {
7372
if g.OnDisk() {
7473
f.FileLocal = g.FileLocal
@@ -112,17 +111,9 @@ func (f *File) GetFileReader() io.Reader {
112111
return f.FileReader
113112
}
114113

115-
// MarshalJSON to be JSON serializable, but only include non-empty fields.
116-
func (f *File) MarshalJSON() ([]byte, error) {
117-
return json.Marshal(f)
114+
func (f *File) ReflectType() string {
115+
return fmt.Sprintf("%T", f)
118116
}
119-
120-
// UnmarshalJSON to be JSON unserializable
121-
func (f *File) UnmarshalJSON(b []byte) error {
122-
return json.Unmarshal(b, f)
123-
}
124-
125-
func (f *File) String() string {
126-
indented, _ := json.MarshalIndent(f, "", " ")
127-
return fmt.Sprintf("Update{ID: %s, Size: %dMB}\n%s\n", f.UniqueID, f.FileSize/1024/1024, indented)
117+
func (f *File) Type() string {
118+
return "File"
128119
}

files_func.go

-24
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,3 @@ func (f *File) Download() ([]byte, error) {
5656

5757
return nil, errors.New("failed to download file")
5858
}
59-
60-
func (b *bot) GetUserProfilePhotos(userID int64, offset, limit int) (*UserProfilePhotos, error) {
61-
var resp struct {
62-
Result UserProfilePhotos
63-
}
64-
65-
params := getUserProfilePhotosRequest{
66-
UserID: userID,
67-
Offset: offset,
68-
Limit: limit,
69-
}
70-
71-
req, err := b.sendMethodRequest(methodGetUserProfilePhotos, params)
72-
73-
if err != nil {
74-
return nil, err
75-
}
76-
77-
if err = json.Unmarshal(req, &resp); err != nil {
78-
return nil, err
79-
}
80-
81-
return &resp.Result, nil
82-
}

formatting_options.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package telebot
22

3+
import "fmt"
4+
35
type ParseMode string
46

57
const (
@@ -33,7 +35,7 @@ const (
3335
)
3436

3537
type Entity struct {
36-
Type EntityType `json:"type"`
38+
EntityType EntityType `json:"type"`
3739
Offset int `json:"offset"`
3840
Length int `json:"length"`
3941
URL string `json:"url,omitempty"`
@@ -42,10 +44,21 @@ type Entity struct {
4244
CustomEmojiID *CustomEmoji `json:"custom_emoji_id,omitempty"`
4345
}
4446

47+
func (c *Entity) ReflectType() string { return fmt.Sprintf("%T", c) }
48+
func (c *Entity) Type() string {
49+
if c.EntityType == "" {
50+
return "unknown"
51+
}
52+
return string(c.EntityType)
53+
}
54+
4555
type LinkPreviewOptions struct {
4656
IsDisabled *bool `json:"is_disabled,omitempty"`
4757
URL *string `json:"url,omitempty"`
4858
PreferSmallMedia *bool `json:"prefer_small_media,omitempty"`
4959
PreferLargeMedia *bool `json:"prefer_large_media,omitempty"`
5060
ShowAboveText *bool `json:"show_above_text,omitempty"`
5161
}
62+
63+
func (c *LinkPreviewOptions) ReflectType() string { return fmt.Sprintf("%T", c) }
64+
func (c *LinkPreviewOptions) Type() string { return "LinkPreviewOptions" }

game.go

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package telebot
22

3+
import "fmt"
4+
35
// Game represents a game. Use BotFather to create and edit games; their short names will act as unique identifiers.
46
type Game struct {
57
// Title is the title of the game.
@@ -24,8 +26,14 @@ type Game struct {
2426
Animation *Animation `json:"animation,omitempty"`
2527
}
2628

29+
func (c *Game) ReflectType() string { return fmt.Sprintf("%T", c) }
30+
func (c *Game) Type() string { return "Game" }
31+
2732
type GameHighScore struct {
2833
Position int `json:"position"`
2934
User User `json:"user"`
3035
Score int `json:"score"`
3136
}
37+
38+
func (c *GameHighScore) ReflectType() string { return fmt.Sprintf("%T", c) }
39+
func (c *GameHighScore) Type() string { return "GameHighScore" }

0 commit comments

Comments
 (0)