Skip to content

Commit e527800

Browse files
committedFeb 27, 2024··
feat(option parser): added more coverage
1 parent 7ced2a1 commit e527800

12 files changed

+72
-128
lines changed
 

‎animation_func.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (b *bot) SendAnimation(to Recipient, animation File, options ...any) (*Acce
2424
params.Thumbnail = v
2525

2626
default:
27-
if !format(&params, options...) {
27+
if !b.format(&params, options...) {
2828
panic(fmt.Errorf(GeneralBadInputError, v, methodSendAnimation))
2929
}
3030
}

‎audio_func.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (b *bot) SendAudio(to Recipient, audio File, options ...any) (*AccessibleMe
1919
params.Thumbnail = v
2020

2121
default:
22-
if !format(&params, options...) {
22+
if !b.format(&params, options...) {
2323
panic(fmt.Errorf(GeneralBadInputError, v, methodSendAnimation))
2424
}
2525
}

‎chat_action_func.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ func (b *bot) SendChatAction(recipient Recipient, action ChatAction, options ...
1010

1111
for _, option := range options {
1212
switch v := option.(type) {
13-
case *MessageThreadID:
14-
params.ThreadID = v
1513
default:
16-
panic("telebot: unknown option type " + fmt.Sprintf("%T", v) + " in SendChatAction.")
14+
if !b.format(&params, options...) {
15+
panic(fmt.Errorf(GeneralBadInputError, v, methodSendChatAction))
16+
}
1717
}
1818
}
1919

‎config/example-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ settings:
33
localesDir: "locales"
44
TOKEN: "env:TELEGRAM_SECRET"
55
URL: "https://api.telegram.org/"
6+
defaultParseMode: "HTML"
67
allowed_updates:
78
- message
89
- edited_message

‎config/settings.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package config
22

3-
import tele "go.mamad.dev/telebot"
3+
import (
4+
tele "go.mamad.dev/telebot"
5+
"strings"
6+
)
47

58
type Settings interface {
69
GetToken() string
710
GetURL() string
811
GetAllowedUpdates() []tele.UpdateType
12+
GetDefaultParseMode() tele.ParseMode
913
}
1014

1115
func (c *config) GetSettings() Settings {
1216
return &c.conf.Settings
1317
}
1418

1519
type settings struct {
16-
RootDir string `yaml:"rootDir" json:"root_dir,omitempty"`
17-
LocalesDir string `yaml:"localesDir" json:"locales_dir,omitempty"`
18-
Token string `yaml:"TOKEN" json:"token,omitempty"`
19-
URL string `yaml:"URL" json:"url,omitempty"`
20-
AllowedUpdates []tele.UpdateType `yaml:"allowed_updates" json:"allowed_updates,omitempty"`
20+
RootDir string `yaml:"rootDir" json:"root_dir,omitempty"`
21+
LocalesDir string `yaml:"localesDir" json:"locales_dir,omitempty"`
22+
Token string `yaml:"TOKEN" json:"token,omitempty"`
23+
URL string `yaml:"URL" json:"url,omitempty"`
24+
AllowedUpdates []tele.UpdateType `yaml:"allowed_updates" json:"allowed_updates,omitempty"`
25+
DefaultParseMode string `yaml:"defaultParseMode" json:"default_parse_mode,omitempty"`
2126
}
2227

2328
func (s *settings) GetToken() string {
@@ -31,3 +36,15 @@ func (s *settings) GetURL() string {
3136
func (s *settings) GetAllowedUpdates() []tele.UpdateType {
3237
return s.AllowedUpdates
3338
}
39+
40+
func (s *settings) GetDefaultParseMode() tele.ParseMode {
41+
switch strings.ToLower(s.DefaultParseMode) {
42+
case "markdown":
43+
return tele.ParseModeMarkdown
44+
case "markdownv2":
45+
return tele.ParseModeMarkdownV2
46+
case "html":
47+
return tele.ParseModeHTML
48+
}
49+
return tele.ParseModeDefault
50+
}

‎contact_func.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,11 @@ func (b *bot) SendContact(to Recipient, contact Contact, options ...any) (*Acces
1919

2020
for _, option := range options {
2121
switch v := option.(type) {
22-
case *MessageThreadID:
23-
params.ThreadID = v
24-
case ReplyMarkup:
25-
params.ReplyMarkup = v
26-
case *ReplyParameters:
27-
params.ReplyParameters = v
28-
29-
case Option:
30-
switch v {
31-
case Silent:
32-
params.DisableNotification = toPtr(true)
33-
case Protected:
34-
params.Protected = toPtr(true)
35-
}
3622

3723
default:
38-
panic("telebot: unknown option type " + fmt.Sprintf("%T", v) + " in SendContact.")
24+
if !b.format(&params, options...) {
25+
panic(fmt.Errorf(GeneralBadInputError, v, methodSendContact))
26+
}
3927
}
4028
}
4129

‎dice_func.go

+3-17
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,13 @@ func (b *bot) SendDice(to Recipient, options ...any) (*AccessibleMessage, error)
1212

1313
for _, option := range options {
1414
switch v := option.(type) {
15-
case *MessageThreadID:
16-
params.ThreadID = v
17-
1815
case DiceEmoji:
1916
params.Emoji = v
2017

21-
case ReplyMarkup:
22-
params.ReplyMarkup = v
23-
case *ReplyParameters:
24-
params.ReplyParameters = v
25-
26-
case Option:
27-
switch v {
28-
case Silent:
29-
params.DisableNotification = toPtr(true)
30-
case Protected:
31-
params.Protected = toPtr(true)
32-
}
33-
3418
default:
35-
panic("telebot: unknown option type " + fmt.Sprintf("%T", v) + " in SendDice.")
19+
if !b.format(&params, options...) {
20+
panic(fmt.Errorf(GeneralBadInputError, v, methodSendDice))
21+
}
3622
}
3723
}
3824

‎document_func.go

+8-23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package telebot
33
import (
44
"encoding/json"
55
"fmt"
6+
"go.mamad.dev/telebot/validation"
67
)
78

89
func (b *bot) SendDocument(to Recipient, doc File, options ...any) (*AccessibleMessage, error) {
@@ -13,35 +14,19 @@ func (b *bot) SendDocument(to Recipient, doc File, options ...any) (*AccessibleM
1314

1415
for _, option := range options {
1516
switch v := option.(type) {
16-
case *MessageThreadID:
17-
params.ThreadID = v
1817
case string:
18+
err := validation.ValidateCaptionLength(v)
19+
if err != nil {
20+
panic("telebot: Bad Caption: " + err.Error())
21+
}
1922
params.Caption = &v
20-
21-
case ParseMode:
22-
params.ParseMode = v
23-
case []Entity:
24-
params.Entities = v
25-
case ReplyMarkup:
26-
params.ReplyMarkup = v
27-
case *ReplyParameters:
28-
params.ReplyParameters = v
29-
3023
case *File:
3124
params.Thumbnail = v
3225

33-
case Option:
34-
switch v {
35-
case Silent:
36-
params.DisableNotification = toPtr(true)
37-
case Protected:
38-
params.Protected = toPtr(true)
39-
case DisableContentTypeDetection:
40-
params.DisableContentTypeDetection = toPtr(true)
41-
}
42-
4326
default:
44-
panic("telebot: unknown option type " + fmt.Sprintf("%T", v) + " in SendDocument.")
27+
if !b.format(&params, options...) {
28+
panic(fmt.Errorf(GeneralBadInputError, v, methodSendDocument))
29+
}
4530
}
4631
}
4732

‎message_func.go

+5-33
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,26 @@ import (
55
"fmt"
66
)
77

8-
func (b *bot) SendMessage(recipient Recipient, text string, option ...any) (*AccessibleMessage, error) {
8+
func (b *bot) SendMessage(recipient Recipient, text string, options ...any) (*AccessibleMessage, error) {
99
params := sendMessageParams{
1010
ChatID: recipient,
1111
Text: text,
1212
}
1313

14-
for _, opt := range option {
14+
for _, opt := range options {
1515
switch v := opt.(type) {
16-
case *MessageThreadID:
17-
params.MessageThreadID = v
18-
19-
case ParseMode:
20-
params.ParseMode = &v
21-
22-
case []Entity:
23-
params.Entities = v
24-
25-
case *LinkPreviewOptions:
26-
params.LinkPreviewOptions = v
27-
28-
case *ReplyParameters:
29-
params.ReplyParameters = v
30-
31-
case ReplyMarkup:
32-
params.ReplyMarkup = v
33-
34-
case Option:
35-
switch v {
36-
case Silent:
37-
params.DisableNotification = toPtr(true)
38-
case Protected:
39-
params.ProtectContent = toPtr(true)
40-
}
4116

4217
default:
43-
panic("telebot: unknown option type " + fmt.Sprintf("%T", v) + " in SendPhoto.")
18+
if !b.format(&params, options...) {
19+
panic(fmt.Errorf(GeneralBadInputError, v, methodSendMessage))
20+
}
4421
}
4522
}
4623

4724
var resp struct {
4825
Result *AccessibleMessage
4926
}
5027

51-
if b.offlineMode {
52-
53-
return resp.Result, nil
54-
}
55-
5628
req, err := b.sendMethodRequest(methodSendMessage, params)
5729

5830
if err != nil {

‎options.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func appendSliceFieldByName(obj any, fieldName string, values ...any) error {
5454
return nil
5555
}
5656

57-
func format(params any, options ...any) bool {
57+
func (b *bot) format(params any, options ...any) bool {
5858
param := reflect.TypeOf(params)
5959
if param.Kind() != reflect.Ptr {
6060
panic("telebot: first argument must be a pointer")
@@ -122,6 +122,8 @@ func format(params any, options ...any) bool {
122122
err = setFieldByName(params, "Width", toPtr(int(v)))
123123
case Height:
124124
err = setFieldByName(params, "Height", toPtr(int(v)))
125+
case *LinkPreviewOptions:
126+
err = setFieldByName(params, "LinkPreviewOptions", v)
125127

126128
case Option:
127129
switch v {
@@ -140,12 +142,19 @@ func format(params any, options ...any) bool {
140142
if err == nil {
141143
hasAnythingChanged = true
142144
}
145+
case DisableContentTypeDetection:
146+
err = setFieldByName(params, "DisableContentTypeDetection", toPtr(true))
143147
}
144148
}
145149
if err != nil {
146150
panic("telebot: " + err.Error())
147151
}
148152
}
149153

154+
if b.defaultParseMode != ParseModeDefault {
155+
_ = setFieldByName(params, "ParseMode", b.defaultParseMode)
156+
hasAnythingChanged = true
157+
}
158+
150159
return hasAnythingChanged
151160
}

‎telebot.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ type bot struct {
3131

3232
httpClient httpc.Client
3333

34-
offlineMode bool
34+
offlineMode bool
35+
defaultParseMode ParseMode
3536
}
3637

3738
type BotSettings struct {
@@ -46,7 +47,8 @@ type BotSettings struct {
4647

4748
Poller Poller
4849

49-
AllowedUpdates []UpdateType
50+
AllowedUpdates []UpdateType
51+
DefaultParseMode ParseMode
5052
}
5153

5254
// New creates a new bot instance.
@@ -85,7 +87,8 @@ func New(s BotSettings) Bot {
8587
synchronous: s.Synchronous,
8688
httpClient: httpc.NewHTTPClient(s.URL, time.Minute),
8789

88-
group: new(Group),
90+
group: new(Group),
91+
defaultParseMode: s.DefaultParseMode,
8992
}
9093

9194
telegramSecretToken = s.Token

‎voice_func.go

+8-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package telebot
33
import (
44
"encoding/json"
55
"fmt"
6-
"time"
6+
"go.mamad.dev/telebot/validation"
77
)
88

99
func (b *bot) SendVoice(to Recipient, voice File, options ...any) (*AccessibleMessage, error) {
@@ -14,34 +14,17 @@ func (b *bot) SendVoice(to Recipient, voice File, options ...any) (*AccessibleMe
1414

1515
for _, option := range options {
1616
switch v := option.(type) {
17-
case *MessageThreadID:
18-
params.ThreadID = v
19-
case *string:
20-
params.Caption = v
2117
case string:
22-
params.Caption = &v
23-
case *ParseMode:
24-
params.ParseMode = v
25-
case []Entity:
26-
params.Entities = v
27-
case ReplyMarkup:
28-
params.ReplyMarkup = v
29-
case *ReplyParameters:
30-
params.ReplyParameters = v
31-
32-
case time.Duration:
33-
params.Duration = toPtr(int(v.Seconds()))
34-
35-
case Option:
36-
switch v {
37-
case Silent:
38-
params.DisableNotification = toPtr(true)
39-
case Protected:
40-
params.Protected = toPtr(true)
18+
err := validation.ValidateCaptionLength(v)
19+
if err != nil {
20+
panic("telebot: Bad Caption: " + err.Error())
4121
}
22+
params.Caption = &v
4223

4324
default:
44-
panic("telebot: unknown option type " + fmt.Sprintf("%T", v) + " in SendVoice.")
25+
if !b.format(&params, options...) {
26+
panic(fmt.Errorf(GeneralBadInputError, v, methodSendVoice))
27+
}
4528
}
4629
}
4730

0 commit comments

Comments
 (0)
Please sign in to comment.