-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
bot_options.go
137 lines (120 loc) · 3.73 KB
/
bot_options.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package telego
import (
"errors"
"net/http"
"os"
"strings"
"github.com/valyala/fasthttp"
ta "github.com/mymmrac/telego/telegoapi"
)
// WithAPICaller sets custom API caller to use
func WithAPICaller(caller ta.Caller) BotOption {
return func(bot *Bot) error {
bot.api = caller
return nil
}
}
// WithFastHTTPClient sets fasthttp client to use
func WithFastHTTPClient(client *fasthttp.Client) BotOption {
return func(bot *Bot) error {
bot.api = ta.FastHTTPCaller{Client: client}
return nil
}
}
// WithHTTPClient sets http client to use
func WithHTTPClient(client *http.Client) BotOption {
return func(bot *Bot) error {
bot.api = ta.HTTPCaller{Client: client}
return nil
}
}
// WithRequestConstructor sets custom request constructor to use
func WithRequestConstructor(constructor ta.RequestConstructor) BotOption {
return func(bot *Bot) error {
bot.constructor = constructor
return nil
}
}
// WithDefaultLogger configures default logger. Redefines existing logger.
// Note: Default logger will hide your bot token, but it still may log sensitive information, it's only safe to use
// default logger in testing environment.
func WithDefaultLogger(debugMode, printErrors bool) BotOption {
return func(bot *Bot) error {
log := &logger{
Out: os.Stderr,
DebugMode: debugMode,
PrintErrors: printErrors,
Replacer: defaultReplacer(bot.Token()),
}
bot.log = log
return nil
}
}
// WithExtendedDefaultLogger configures default logger, replacer can be nil. Redefines existing loggers.
// Note: Keep in mind that debug logs will include your bot token. It's only safe to have them enabled in
// testing environment, or hide sensitive information (like bot token) yourself.
func WithExtendedDefaultLogger(debugMode, printErrors bool, replacer *strings.Replacer) BotOption {
return func(bot *Bot) error {
log := &logger{
Out: os.Stderr,
DebugMode: debugMode,
PrintErrors: printErrors,
Replacer: replacer,
}
bot.log = log
return nil
}
}
// WithDefaultDebugLogger configures default debug logger. Alias to default logger with debug and error logs.
// Redefines existing logger.
func WithDefaultDebugLogger() BotOption {
return WithDefaultLogger(true, true)
}
// WithDiscardLogger configures discard logger. Alias to default logger with disabled logs. Redefines existing logger.
func WithDiscardLogger() BotOption {
return WithDefaultLogger(false, false)
}
// WithLogger sets logger to use. Redefines existing loggers.
// Note: Keep in mind that debug logs will include your bot token. It's only safe to have them enabled in
// testing environment, or hide sensitive information (like bot token) yourself.
func WithLogger(log Logger) BotOption {
return func(bot *Bot) error {
bot.log = log
return nil
}
}
// WithAPIServer sets bot API server URL to use
func WithAPIServer(apiURL string) BotOption {
return func(bot *Bot) error {
if apiURL == "" {
return errors.New("empty bot api server url")
}
bot.apiURL = apiURL
return nil
}
}
// WithTestServerPath use the test server API path instead of regular API path
//
// Regular API: https://<api-server>/bot<token>/<method-name>
// Test API: https://<api-server>/bot<token>/test/<method-name>
func WithTestServerPath() BotOption {
return func(bot *Bot) error {
bot.useTestServerPath = true
return nil
}
}
// WithHealthCheck enables health check using [Bot.GetMe] method on start
func WithHealthCheck() BotOption {
return func(bot *Bot) error {
bot.healthCheckRequested = true
return nil
}
}
// WithWarnings treat Telegram warnings as an error
// Note: Any request that has a non-empty error will return both result and error
func WithWarnings() BotOption {
return func(bot *Bot) error {
bot.reportWarningAsErrors = true
return nil
}
}