-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
138 lines (118 loc) · 3.67 KB
/
Copy pathoptions.go
File metadata and controls
138 lines (118 loc) · 3.67 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
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
138
package maxigobot
import (
"time"
maxigo "github.com/maxigo-bot/maxigo-client"
)
// Option configures the Bot during creation.
type Option func(*Bot)
// WithLongPolling sets up long polling with the given timeout in seconds.
func WithLongPolling(timeout int) Option {
return func(b *Bot) {
b.poller = &LongPoller{Timeout: timeout}
}
}
// WithClient injects a pre-configured maxigo-client (useful for testing).
func WithClient(c *maxigo.Client) Option {
return func(b *Bot) {
b.client = c
}
}
// WithUpdateTypes filters which update types the poller will receive.
func WithUpdateTypes(types ...string) Option {
return func(b *Bot) {
b.updateTypes = types
}
}
// WithRateLimitIntervals configures the retry schedule for HTTP 429 (rate limit) errors.
// Default: DefaultRateLimitIntervals (4 attempts: 1s, 2s, 5s, 10s).
// Pass no arguments to disable rate limit retries.
func WithRateLimitIntervals(intervals ...time.Duration) Option {
return func(b *Bot) {
b.retry.rateLimitIntervals = intervals
}
}
// WithUploadRetryIntervals configures the retry schedule for "file not processed" errors
// (HTTP 400 with "not.processed" message, returned when a file is still being processed).
// Default: DefaultUploadRetryIntervals (4 attempts: 200ms, 500ms, 1s, 2s).
// Pass no arguments to disable upload retries.
func WithUploadRetryIntervals(intervals ...time.Duration) Option {
return func(b *Bot) {
b.retry.uploadRetryIntervals = intervals
}
}
// sendConfig holds parameters for a send/reply/edit operation.
type sendConfig struct {
ReplyTo string
Notify *bool
Format *maxigo.TextFormat
Attachments []maxigo.AttachmentRequest
DisableLinkPreview bool
}
// SendOption configures a send/reply/edit operation.
type SendOption func(*sendConfig)
// WithReplyTo sets the message ID to reply to.
func WithReplyTo(messageID string) SendOption {
return func(cfg *sendConfig) {
cfg.ReplyTo = messageID
}
}
// WithNotify controls whether chat members are notified.
func WithNotify(notify bool) SendOption {
return func(cfg *sendConfig) {
cfg.Notify = ¬ify
}
}
// WithFormat sets the text formatting mode (markdown or html).
func WithFormat(format maxigo.TextFormat) SendOption {
return func(cfg *sendConfig) {
cfg.Format = &format
}
}
// WithAttachments adds attachments to the message.
func WithAttachments(attachments ...maxigo.AttachmentRequest) SendOption {
return func(cfg *sendConfig) {
cfg.Attachments = append(cfg.Attachments, attachments...)
}
}
// WithDisableLinkPreview prevents the server from generating link previews.
func WithDisableLinkPreview() SendOption {
return func(cfg *sendConfig) {
cfg.DisableLinkPreview = true
}
}
// WithKeyboard adds an inline keyboard to the message.
// Each argument is a row of buttons.
func WithKeyboard(rows ...[]maxigo.Button) SendOption {
return WithAttachments(maxigo.NewInlineKeyboardAttachment(rows))
}
// buildSendConfig merges all send options into a sendConfig.
func buildSendConfig(opts []SendOption) sendConfig {
var cfg sendConfig
for _, opt := range opts {
opt(&cfg)
}
return cfg
}
// toMessageBody converts text + sendConfig into a maxigo NewMessageBody.
func toMessageBody(text string, cfg sendConfig) *maxigo.NewMessageBody {
body := &maxigo.NewMessageBody{
Text: maxigo.Some(text),
DisableLinkPreview: cfg.DisableLinkPreview,
}
if cfg.Notify != nil {
body.Notify = maxigo.Some(*cfg.Notify)
}
if cfg.Format != nil {
body.Format = maxigo.Some(*cfg.Format)
}
if len(cfg.Attachments) > 0 {
body.Attachments = cfg.Attachments
}
if cfg.ReplyTo != "" {
body.Link = &maxigo.NewMessageLink{
Type: maxigo.LinkReply,
MID: cfg.ReplyTo,
}
}
return body
}