-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
inline.go
130 lines (114 loc) · 4.32 KB
/
inline.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
package inline
import (
"context"
"io"
"time"
"github.com/go-faster/errors"
"github.com/gotd/td/tg"
)
// ResultBuilder is inline result builder.
type ResultBuilder struct {
raw *tg.Client
random io.Reader
// Set this flag if the results are composed of media files
gallery bool
// Set this flag if results may be cached on the server side only for the user that sent
// the query. By default, results may be returned to any user who sends the same query
private bool
// Unique identifier for the answered query
queryID int64
// The maximum amount of time in seconds that the result of the inline query may be
// cached on the server. Defaults to 300.
cacheTime int
// Pass the offset that a client should send in the next query with the same text to
// receive more results. Pass an empty string if there are no more results or if you
// don‘t support pagination. Offset length can’t exceed 64 bytes.
nextOffset string
// If passed, clients will display a button with specified text that switches the user to
// a private chat with the bot and sends the bot a start message with a certain parameter.
switchPm tg.InlineBotSwitchPM
// If passed, clients will display a button on top of the remaining inline result list
// with the specified text, that switches the user to the specified bot web app.
switchWebview tg.InlineBotWebView
}
// New creates new ResultBuilder.
func New(raw *tg.Client, random io.Reader, queryID int64) *ResultBuilder {
return &ResultBuilder{raw: raw, random: random, queryID: queryID}
}
// Gallery sets flag if the results are composed of media files.
func (r *ResultBuilder) Gallery(gallery bool) *ResultBuilder {
r.gallery = gallery
return r
}
// Private sets flag if results may be cached on the server side only for the user that sent
// the query. By default, results may be returned to any user who sends the same query.
func (r *ResultBuilder) Private(private bool) *ResultBuilder {
r.private = private
return r
}
// CacheTime sets the maximum amount of time that the result of the inline query may be
// cached on the server. Server's default is 300 seconds.
func (r *ResultBuilder) CacheTime(cacheTime time.Duration) *ResultBuilder {
return r.CacheTimeSeconds(int(cacheTime.Seconds()))
}
// CacheTimeSeconds sets the maximum amount of time in seconds that the result of the inline query may be
// cached on the server. Server's default is 300.
func (r *ResultBuilder) CacheTimeSeconds(cacheTime int) *ResultBuilder {
r.cacheTime = cacheTime
return r
}
// NextOffset sets offset that a client should send in the next query with the same text to
// receive more results. Pass an empty string if there are no more results or if you
// don‘t support pagination. Offset length can’t exceed 64 bytes.
func (r *ResultBuilder) NextOffset(nextOffset string) *ResultBuilder {
r.nextOffset = nextOffset
return r
}
// SwitchPM sets SwitchPm field.
//
// If passed, clients will display a button with specified text that switches the user to
// a private chat with the bot and sends the bot a start message with a certain parameter.
func (r *ResultBuilder) SwitchPM(text, startParam string) *ResultBuilder {
r.switchPm = tg.InlineBotSwitchPM{
Text: text,
StartParam: startParam,
}
return r
}
// SwitchWebview sets SwitchWebview field.
//
// If passed, clients will display a button on top of the remaining inline result list
// with the specified text, that switches the user to the specified bot web app.
func (r *ResultBuilder) SwitchWebview(text, url string) *ResultBuilder {
r.switchWebview = tg.InlineBotWebView{
Text: text,
URL: url,
}
return r
}
// Set sets inline results for given query.
func (r *ResultBuilder) Set(ctx context.Context, opts ...ResultOption) (bool, error) {
res := resultPageBuilder{
results: nil,
random: r.random,
}
for idx, opt := range opts {
if err := opt.apply(&res); err != nil {
return false, errors.Wrapf(err, "apply %d option", idx+1)
}
}
ok, err := r.raw.MessagesSetInlineBotResults(ctx, &tg.MessagesSetInlineBotResultsRequest{
Private: r.private,
QueryID: r.queryID,
Results: res.results,
CacheTime: r.cacheTime,
NextOffset: r.nextOffset,
SwitchPm: r.switchPm,
Gallery: r.gallery,
SwitchWebview: r.switchWebview,
})
if err != nil {
return false, errors.Wrap(err, "set inline results")
}
return ok, nil
}