-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer.go
More file actions
165 lines (135 loc) · 4.52 KB
/
Copy pathanswer.go
File metadata and controls
165 lines (135 loc) · 4.52 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package botapi
import (
"context"
"strconv"
"github.com/gotd/td/tg"
)
// AnswerCallbackQueryOption configures an AnswerCallbackQuery call.
type AnswerCallbackQueryOption func(*answerCallbackConfig)
type answerCallbackConfig struct {
text string
showAlert bool
url string
cacheTime int
}
// WithCallbackText sets the notification text shown to the user (0-200
// characters). By default nothing is shown.
func WithCallbackText(text string) AnswerCallbackQueryOption {
return func(c *answerCallbackConfig) { c.text = text }
}
// WithCallbackAlert shows the notification as an alert dialog instead of a
// top-of-screen toast.
func WithCallbackAlert() AnswerCallbackQueryOption {
return func(c *answerCallbackConfig) { c.showAlert = true }
}
// WithCallbackURL sets a URL to open. Telegram restricts which URLs are
// accepted (game URLs, or t.me/your_bot?start= links).
func WithCallbackURL(url string) AnswerCallbackQueryOption {
return func(c *answerCallbackConfig) { c.url = url }
}
// WithCallbackCacheTime sets, in seconds, how long the result may be cached
// client-side.
func WithCallbackCacheTime(seconds int) AnswerCallbackQueryOption {
return func(c *answerCallbackConfig) { c.cacheTime = seconds }
}
// AnswerCallbackQuery responds to a callback query sent from an inline keyboard.
// The callbackQueryID is the CallbackQuery.ID from the update.
func (b *Bot) AnswerCallbackQuery(ctx context.Context, callbackQueryID string, opts ...AnswerCallbackQueryOption) error {
var cfg answerCallbackConfig
for _, o := range opts {
o(&cfg)
}
queryID, err := strconv.ParseInt(callbackQueryID, 10, 64)
if err != nil {
return &Error{Code: 400, Description: "Bad Request: invalid callback query id"}
}
req := &tg.MessagesSetBotCallbackAnswerRequest{
Alert: cfg.showAlert,
QueryID: queryID,
CacheTime: cfg.cacheTime,
}
if cfg.text != "" {
req.SetMessage(cfg.text)
}
if cfg.url != "" {
req.SetURL(cfg.url)
}
if _, err := b.raw.MessagesSetBotCallbackAnswer(ctx, req); err != nil {
return asAPIError(err)
}
return nil
}
// AnswerInlineQueryOption configures an AnswerInlineQuery call.
type AnswerInlineQueryOption func(*answerInlineConfig)
type answerInlineConfig struct {
cacheTime int
isPersonal bool
nextOffset string
switchPMText string
switchPMParam string
}
// WithInlineCacheTime sets the maximum time, in seconds, the result may be
// cached on the server.
func WithInlineCacheTime(seconds int) AnswerInlineQueryOption {
return func(c *answerInlineConfig) { c.cacheTime = seconds }
}
// WithInlinePersonal marks the results as personal so they are not cached for
// other users querying the same thing.
func WithInlinePersonal() AnswerInlineQueryOption {
return func(c *answerInlineConfig) { c.isPersonal = true }
}
// WithInlineNextOffset sets the offset a client returns to request the next page
// of results. An empty offset (the default) means no more results.
func WithInlineNextOffset(offset string) AnswerInlineQueryOption {
return func(c *answerInlineConfig) { c.nextOffset = offset }
}
// WithInlineSwitchPM shows a button above the results that switches the user to
// a private chat with the bot, passing startParam as the /start parameter.
func WithInlineSwitchPM(text, startParam string) AnswerInlineQueryOption {
return func(c *answerInlineConfig) {
c.switchPMText = text
c.switchPMParam = startParam
}
}
// AnswerInlineQuery sends the results of an inline query. inlineQueryID is the
// InlineQuery.ID from the update.
func (b *Bot) AnswerInlineQuery(
ctx context.Context, inlineQueryID string, results []InlineQueryResult, opts ...AnswerInlineQueryOption,
) error {
var cfg answerInlineConfig
for _, o := range opts {
o(&cfg)
}
queryID, err := strconv.ParseInt(inlineQueryID, 10, 64)
if err != nil {
return &Error{Code: 400, Description: "Bad Request: invalid inline query id"}
}
tgResults := make([]tg.InputBotInlineResultClass, 0, len(results))
for _, r := range results {
if r == nil {
return errNilInlineResult()
}
converted, err := r.toTg(ctx, b)
if err != nil {
return err
}
tgResults = append(tgResults, converted)
}
req := &tg.MessagesSetInlineBotResultsRequest{
QueryID: queryID,
Results: tgResults,
CacheTime: cfg.cacheTime,
Private: cfg.isPersonal,
NextOffset: cfg.nextOffset,
}
if cfg.switchPMText != "" {
req.SetSwitchPm(tg.InlineBotSwitchPM{
Text: cfg.switchPMText,
StartParam: cfg.switchPMParam,
})
}
if _, err := b.raw.MessagesSetInlineBotResults(ctx, req); err != nil {
return asAPIError(err)
}
return nil
}