-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
209 lines (173 loc) · 4.54 KB
/
context.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package tgbot
import (
"context"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Context struct {
context.Context
*tgbotapi.BotAPI
update *tgbotapi.Update
}
// Command return command name if message is non-nil.
func (c *Context) Command() string {
if message := c.Message(); message != nil {
return message.Command()
}
return ""
}
// IsCommand report whether the current message is a command.
func (c *Context) IsCommand() bool {
if msg := c.Message(); msg != nil {
return msg.IsCommand()
}
return false
}
// CommandArgs return command arguments if message is non-nil.
func (c *Context) CommandArgs() string {
if message := c.Message(); message != nil {
return message.CommandArguments()
}
return ""
}
func (c *Context) Message() *tgbotapi.Message {
if c.update == nil {
return nil
}
switch {
case c.update.Message != nil:
return c.update.Message
case c.update.EditedMessage != nil:
return c.update.EditedMessage
case c.update.CallbackQuery != nil:
return c.update.CallbackQuery.Message
case c.update.ChannelPost != nil:
return c.update.ChannelPost
case c.update.EditedChannelPost != nil:
return c.update.EditedChannelPost
default:
return nil
}
}
func (c *Context) Update() *tgbotapi.Update {
return c.update
}
func (c *Context) SentFrom() *tgbotapi.User {
if c.update == nil {
return nil
}
return c.update.SentFrom()
}
func (c *Context) FromChat() *tgbotapi.Chat {
if c.update == nil {
return nil
}
return c.update.FromChat()
}
type MessageOption func(c *tgbotapi.MessageConfig)
// ReplyText reply to the current chat.
func (c *Context) ReplyText(text string, opts ...MessageOption) error {
return c.reply(text, mergeOpts(opts,
WithDisableWebPagePreview(true),
)...)
}
// ReplyMarkdown reply to the current chat, text format is markdown.
func (c *Context) ReplyMarkdown(text string, opts ...MessageOption) error {
return c.reply(text, mergeOpts(opts,
WithMarkdown(),
WithDisableWebPagePreview(true),
)...)
}
// ReplyHTML reply to the current chat, text format is HTML.
func (c *Context) ReplyHTML(text string, opts ...MessageOption) error {
return c.reply(text, mergeOpts(opts,
WithHTML(),
WithDisableWebPagePreview(true),
)...)
}
func (c *Context) reply(text string, opts ...MessageOption) error {
msg := tgbotapi.NewMessage(0, text)
if chat := c.update.FromChat(); chat != nil {
msg.ChatID = chat.ID
}
for _, o := range opts {
o(&msg)
}
return c.SendReply(msg)
}
// SendReply send reply.
func (c *Context) SendReply(chat tgbotapi.Chattable) error {
_, err := c.Request(chat)
return err
}
// WithContext clone a Context for use in other goroutine.
func (c *Context) WithContext(ctx context.Context) *Context {
nc := c.clone()
nc.Context = ctx
switch cli := nc.BotAPI.Client.(type) {
case *client:
nc.BotAPI.Client = cli.withContext(nc.Context)
default:
nc.BotAPI.Client = &client{cli: nc.BotAPI.Client, ctx: ctx}
}
return nc
}
func (c *Context) clone() *Context {
nc := new(Context)
*nc = *c
return nc
}
func (c *Context) reset() {
c.update = nil
c.Context = nil
}
func mergeOpts(opts []MessageOption, def ...MessageOption) []MessageOption {
return append(def, opts...)
}
// WithHTML set parse mode to html.
func WithHTML() MessageOption {
return func(c *tgbotapi.MessageConfig) {
c.ParseMode = tgbotapi.ModeHTML
}
}
// WithMarkdown set parse mode to markdown.
func WithMarkdown() MessageOption {
return func(c *tgbotapi.MessageConfig) {
c.ParseMode = tgbotapi.ModeMarkdown
}
}
// WithInlineKeyboardMarkup set inline keyboard.
func WithInlineKeyboardMarkup(markup tgbotapi.InlineKeyboardMarkup) MessageOption {
return func(c *tgbotapi.MessageConfig) {
c.ReplyMarkup = markup
}
}
// WithKeyboardMarkup set keyboard.
func WithKeyboardMarkup(markup tgbotapi.ReplyKeyboardMarkup) MessageOption {
return func(c *tgbotapi.MessageConfig) {
c.ReplyMarkup = markup
}
}
// WithRemoveKeyboard remove keyboard.
func WithRemoveKeyboard(markup tgbotapi.ReplyKeyboardRemove) MessageOption {
return func(c *tgbotapi.MessageConfig) {
c.ReplyMarkup = markup
}
}
// WithMarkdownV2 set parse mode to markdown v2.
func WithMarkdownV2() MessageOption {
return func(c *tgbotapi.MessageConfig) {
c.ParseMode = tgbotapi.ModeMarkdownV2
}
}
// WithDisableWebPagePreview disable web page preview.
func WithDisableWebPagePreview(disable bool) MessageOption {
return func(c *tgbotapi.MessageConfig) {
c.DisableWebPagePreview = disable
}
}
// WithChatId set message chat id.
func WithChatId(chatId int64) MessageOption {
return func(c *tgbotapi.MessageConfig) {
c.ChatID = chatId
}
}