forked from go-telegram-bot-api/telegram-bot-api
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathbot_options.go
More file actions
96 lines (85 loc) · 2.27 KB
/
Copy pathbot_options.go
File metadata and controls
96 lines (85 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package tgbotapi
import (
"fmt"
"log/slog"
"net/http"
)
type botAPIConfig struct {
apiEndpoint string
fileEndpoint string
client HTTPClient
debug bool
buffer int
logger any
loggingDisabled bool
}
// BotAPIOption configures a BotAPI instance created by NewBotAPIWithOptions.
type BotAPIOption func(*botAPIConfig) error
func defaultBotAPIConfig() botAPIConfig {
return botAPIConfig{
apiEndpoint: APIEndpoint,
fileEndpoint: FileEndpoint,
client: &http.Client{},
buffer: 100,
}
}
// WithAPIEndpoint configures the Telegram Bot API endpoint.
func WithAPIEndpoint(apiEndpoint string) BotAPIOption {
return func(config *botAPIConfig) error {
config.apiEndpoint = apiEndpoint
return nil
}
}
// WithFileEndpoint configures the Telegram file download endpoint.
func WithFileEndpoint(fileEndpoint string) BotAPIOption {
return func(config *botAPIConfig) error {
config.fileEndpoint = fileEndpoint
return nil
}
}
// WithHTTPClient configures the HTTP client used for API requests.
func WithHTTPClient(client HTTPClient) BotAPIOption {
return func(config *botAPIConfig) error {
config.client = client
return nil
}
}
// WithDebug configures debug logging for API requests and responses.
func WithDebug(debug bool) BotAPIOption {
return func(config *botAPIConfig) error {
config.debug = debug
return nil
}
}
// WithUpdatesBuffer configures the update channel buffer capacity.
func WithUpdatesBuffer(capacity int) BotAPIOption {
return func(config *botAPIConfig) error {
config.buffer = capacity
return nil
}
}
// WithLogger configures a logger for this BotAPI instance.
// Logger should be one of [BotLogger] or [*slog.Logger].
func WithLogger(logger any) BotAPIOption {
return func(config *botAPIConfig) error {
switch logger.(type) {
case nil:
config.logger = nil
config.loggingDisabled = true
case BotLogger, *slog.Logger:
config.logger = logger
config.loggingDisabled = false
default:
return fmt.Errorf("invalid type (%T) of logger", logger)
}
return nil
}
}
// WithLoggingDisabled disables all logging for this BotAPI instance.
func WithLoggingDisabled() BotAPIOption {
return func(config *botAPIConfig) error {
config.logger = nil
config.loggingDisabled = true
return nil
}
}