-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
context.go
214 lines (181 loc) · 6.75 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
210
211
212
213
214
package tgbot
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/nekomeowww/insights-bot/pkg/logger"
"github.com/nekomeowww/insights-bot/pkg/types/telegram"
"github.com/redis/rueidis"
)
type UpdateType string
const (
UpdateTypeUnknown UpdateType = "unknown"
UpdateTypeMessage UpdateType = "message"
UpdateTypeEditedMessage UpdateType = "edited_message"
UpdateTypeChannelPost UpdateType = "channel_post"
UpdateTypeEditedChannelPost UpdateType = "edited_channel_post"
UpdateTypeInlineQuery UpdateType = "inline_query"
UpdateTypeChosenInlineResult UpdateType = "chosen_inline_result"
UpdateTypeCallbackQuery UpdateType = "callback_query"
UpdateTypeShippingQuery UpdateType = "shipping_query"
UpdateTypePreCheckoutQuery UpdateType = "pre_checkout_query"
UpdateTypePoll UpdateType = "poll"
UpdateTypePollAnswer UpdateType = "poll_answer"
UpdateTypeMyChatMember UpdateType = "my_chat_member"
UpdateTypeChatMember UpdateType = "chat_member"
UpdateTypeLeftChatMember UpdateType = "left_chat_member"
UpdateTypeNewChatMembers UpdateType = "new_chat_members"
UpdateTypeChatJoinRequest UpdateType = "chat_join_request"
UpdateTypeChatMigrationFrom UpdateType = "chat_migration_from"
UpdateTypeChatMigrationTo UpdateType = "chat_migration_to"
)
type Context struct {
Bot *Bot
Update tgbotapi.Update
Logger *logger.Logger
mutex sync.Mutex
rueidisClient rueidis.Client
abort bool
isCallbackQuery bool
callbackQueryHandlerRoute string
callbackQueryHandlerRouteHash string
callbackQueryHandlerActionData string
callbackQueryHandlerActionDataHash string
callbackQueryHandlerActionDataIsEmpty bool
}
func NewContext(bot *tgbotapi.BotAPI, update tgbotapi.Update, logger *logger.Logger, rueidisClient rueidis.Client) *Context {
return &Context{
Bot: &Bot{BotAPI: bot, logger: logger, rueidisClient: rueidisClient},
Update: update,
Logger: logger,
rueidisClient: rueidisClient,
isCallbackQuery: false,
callbackQueryHandlerRoute: "",
callbackQueryHandlerRouteHash: "",
callbackQueryHandlerActionData: "",
callbackQueryHandlerActionDataHash: "",
callbackQueryHandlerActionDataIsEmpty: false,
}
}
func (c *Context) UpdateType() UpdateType {
switch {
case c.Update.Message != nil:
switch {
case c.Update.Message.NewChatMembers != nil:
return UpdateTypeNewChatMembers
case c.Update.Message.LeftChatMember != nil:
return UpdateTypeLeftChatMember
case c.Update.Message.MigrateFromChatID != 0:
return UpdateTypeChatMigrationFrom
case c.Update.Message.MigrateToChatID != 0:
return UpdateTypeChatMigrationTo
default:
return UpdateTypeMessage
}
case c.Update.EditedMessage != nil:
return UpdateTypeEditedMessage
case c.Update.ChannelPost != nil:
return UpdateTypeChannelPost
case c.Update.EditedChannelPost != nil:
return UpdateTypeEditedChannelPost
case c.Update.InlineQuery != nil:
return UpdateTypeInlineQuery
case c.Update.ChosenInlineResult != nil:
return UpdateTypeChosenInlineResult
case c.Update.CallbackQuery != nil:
return UpdateTypeCallbackQuery
case c.Update.ShippingQuery != nil:
return UpdateTypeShippingQuery
case c.Update.PreCheckoutQuery != nil:
return UpdateTypePreCheckoutQuery
case c.Update.Poll != nil:
return UpdateTypePoll
case c.Update.PollAnswer != nil:
return UpdateTypePollAnswer
case c.Update.MyChatMember != nil:
return UpdateTypeMyChatMember
case c.Update.ChatMember != nil:
return UpdateTypeChatMember
case c.Update.ChatJoinRequest != nil:
return UpdateTypeChatJoinRequest
default:
return UpdateTypeUnknown
}
}
func (c *Context) Abort() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.abort = true
}
func (c *Context) IsAborted() bool {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.abort
}
func (c *Context) initForCallbackQuery(route, routeHash, actionDataHash string) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.isCallbackQuery = true
c.callbackQueryHandlerRoute = route
c.callbackQueryHandlerRouteHash = routeHash
c.callbackQueryHandlerActionDataHash = actionDataHash
}
func (c *Context) fetchActionDataForCallbackQueryHandler() error {
c.mutex.Lock()
defer c.mutex.Unlock()
c.callbackQueryHandlerActionDataIsEmpty = true
if !c.isCallbackQuery {
return fmt.Errorf("not a callback query")
}
if c.callbackQueryHandlerRouteHash == "" {
return fmt.Errorf("callback query handler route hash is empty")
}
if c.callbackQueryHandlerActionDataHash == "" {
return fmt.Errorf("callback query handler action data hash is empty")
}
if c.rueidisClient == nil {
return fmt.Errorf("rueidis client is nil")
}
str, err := c.Bot.fetchCallbackQueryActionData(c.callbackQueryHandlerRoute, c.callbackQueryHandlerActionDataHash)
if err != nil {
return err
}
c.callbackQueryHandlerActionData = str
c.callbackQueryHandlerActionDataIsEmpty = false
return nil
}
func (c *Context) BindFromCallbackQueryData(dst any) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.callbackQueryHandlerActionDataIsEmpty {
return errors.New("empty action data")
}
return json.Unmarshal([]byte(c.callbackQueryHandlerActionData), dst)
}
func (c *Context) IsBotAdministrator() (bool, error) {
return c.Bot.IsBotAdministrator(c.Update.FromChat().ID)
}
func (c *Context) IsUserMemberStatus(userID int64, status []telegram.MemberStatus) (bool, error) {
return c.Bot.IsUserMemberStatus(c.Update.FromChat().ID, userID, status)
}
func (c *Context) RateLimitForCommand(chatID int64, command string, rate int64, perDuration time.Duration) (int64, time.Duration, bool, error) {
return c.Bot.RateLimitForCommand(chatID, command, rate, perDuration)
}
func (c *Context) NewMessage(message string) MessageResponse {
return NewMessage(c.Update.FromChat().ID, message)
}
func (c *Context) NewMessageReplyTo(message string, replyToMessageID int) MessageResponse {
return NewMessageReplyTo(c.Update.FromChat().ID, message, replyToMessageID)
}
func (c *Context) NewEditMessageText(messageID int, text string) EditMessageResponse {
return NewEditMessageText(c.Update.FromChat().ID, messageID, text)
}
func (c *Context) NewEditMessageTextAndReplyMarkup(messageID int, text string, replyMarkup tgbotapi.InlineKeyboardMarkup) EditMessageResponse {
return NewEditMessageTextAndReplyMarkup(c.Update.FromChat().ID, messageID, text, replyMarkup)
}
func (c *Context) NewEditMessageReplyMarkup(messageID int, replyMarkup tgbotapi.InlineKeyboardMarkup) EditMessageResponse {
return NewEditMessageReplyMarkup(c.Update.FromChat().ID, messageID, replyMarkup)
}