-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatehandlers.go
168 lines (149 loc) · 5.5 KB
/
updatehandlers.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
package telebot
import (
"encoding/json"
"github.com/aliforever/go-telegram-bot-api"
"reflect"
"github.com/aliforever/go-telegram-bot-api/structs"
log "github.com/sirupsen/logrus"
)
type updateHandlers struct {
bot *Bot
messageTypeGroup reflect.Method
channelPost reflect.Method
inlineQuery reflect.Method
chosenInlineResult reflect.Method
callbackQuery reflect.Method
shippingQuery reflect.Method
preCheckoutQuery reflect.Method
pollAnswer reflect.Method
myChatMember reflect.Method
chatMember reflect.Method
}
func updateHandlersFromType(bot *Bot, t reflect.Type) (uh *updateHandlers) {
uh = &updateHandlers{
bot: bot,
}
uh.messageTypeGroup, _ = t.MethodByName("MessageTypeGroupHandler")
uh.channelPost, _ = t.MethodByName("ChannelPostHandler")
uh.inlineQuery, _ = t.MethodByName("InlineQueryHandler")
uh.chosenInlineResult, _ = t.MethodByName("ChosenInlineResultHandler")
uh.callbackQuery, _ = t.MethodByName("CallbackQueryHandler")
uh.shippingQuery, _ = t.MethodByName("ShippingQueryHandler")
uh.preCheckoutQuery, _ = t.MethodByName("PreCheckoutQueryHandler")
uh.pollAnswer, _ = t.MethodByName("PollAnswerHandler")
uh.myChatMember, _ = t.MethodByName("MyChatMemberHandler")
uh.chatMember, _ = t.MethodByName("ChatMemberHandler")
return
}
func (uh *updateHandlers) allowedUpdates() (allowedUpdates []string) {
allowedUpdates = append(allowedUpdates, "message", "edited_message")
if uh.channelPost.Name != "" {
allowedUpdates = append(allowedUpdates, "channel_post", "edited_channel_post")
}
if uh.inlineQuery.Name != "" {
allowedUpdates = append(allowedUpdates, "inline_query")
}
if uh.chosenInlineResult.Name != "" {
allowedUpdates = append(allowedUpdates, "chosen_inline_result")
}
if uh.callbackQuery.Name != "" {
allowedUpdates = append(allowedUpdates, "callback_query")
}
if uh.shippingQuery.Name != "" {
allowedUpdates = append(allowedUpdates, "shipping_query")
}
if uh.preCheckoutQuery.Name != "" {
allowedUpdates = append(allowedUpdates, "pre_checkout_query")
}
if uh.chatMember.Name != "" {
allowedUpdates = append(allowedUpdates, "chat_member")
}
if uh.pollAnswer.Name != "" {
allowedUpdates = append(allowedUpdates, "poll_answer")
}
if uh.myChatMember.Name != "" {
allowedUpdates = append(allowedUpdates, "my_chat_member")
}
return
}
func (uh *updateHandlers) handleProcessUpdateError(update *tgbotapi.Update, message string) {
j, _ := json.Marshal(update)
log.Errorf("Error processing update: %s. Update: %s", message, j)
}
func (uh *updateHandlers) processMessageTypeGroup(app reflect.Value, update *tgbotapi.Update) {
if uh.messageTypeGroup.Name == "" {
j, _ := json.Marshal(update)
log.Errorf("Handler for Group Message Type was not found!\n%s", string(j))
return
}
uh.messageTypeGroup.Func.Call(uh.bot.appWithUpdate(app, update, &update.Message.Chat.Id))
}
func (uh *updateHandlers) processChannelPost(app reflect.Value, update *tgbotapi.Update) {
if uh.channelPost.Name == "" {
j, _ := json.Marshal(update)
log.Errorf("Handler for ChannelPost was not found!\n%s", string(j))
return
}
uh.channelPost.Func.Call(uh.bot.appWithUpdate(app, update, &update.ChannelPost.Chat.Id))
}
func (uh *updateHandlers) processMyChatMember(app reflect.Value, update *tgbotapi.Update) {
if uh.myChatMember.Name == "" {
j, _ := json.Marshal(update)
log.Errorf("Handler for MyChatMember was not found!\n%s", string(j))
return
}
uh.myChatMember.Func.Call(uh.bot.appWithUpdate(app, update, &update.MyChatMember.Chat.Id))
}
func (uh *updateHandlers) processChatMember(app reflect.Value, update *tgbotapi.Update) {
if uh.chatMember.Name == "" {
j, _ := json.Marshal(update)
log.Errorf("Handler for ChatMember was not found!\n%s", string(j))
return
}
uh.chatMember.Func.Call(uh.bot.appWithUpdate(app, update, &update.ChatMember.Chat.Id))
}
func (uh *updateHandlers) processCallbackQuery(app reflect.Value, update *tgbotapi.Update) {
if uh.callbackQuery.Name == "" {
j, _ := json.Marshal(update)
log.Errorf("Handler for CallbackQuery was not found!\n%s", string(j))
return
}
uh.callbackQuery.Func.Call(uh.bot.appWithUpdate(app, update, &update.CallbackQuery.Message.Chat.Id))
}
func (uh *updateHandlers) processPollAnswer(app reflect.Value, update *tgbotapi.Update) {
if uh.pollAnswer.Name == "" {
j, _ := json.Marshal(update)
log.Errorf("Handler for PollAnswer was not found!\n%s", string(j))
return
}
uh.pollAnswer.Func.Call(uh.bot.appWithUpdate(app, update, &update.PollAnswer.User.Id))
}
func (uh *updateHandlers) processUpdate(app reflect.Value, update tgbotapi.Update) {
var message *structs.Message
if update.Message != nil {
message = update.Message
} else if update.EditedMessage != nil {
message = update.EditedMessage
}
var channelPost *structs.Message
if update.ChannelPost != nil {
channelPost = update.ChannelPost
} else if update.EditedChannelPost != nil {
channelPost = update.EditedChannelPost
}
if message != nil && (message.Chat.Type == "group" || message.Chat.Type == "supergroup") {
uh.processMessageTypeGroup(app, &update)
} else if channelPost != nil {
uh.processChannelPost(app, &update)
} else if update.MyChatMember != nil {
uh.processMyChatMember(app, &update)
} else if update.ChatMember != nil {
uh.processChatMember(app, &update)
} else if update.CallbackQuery != nil {
uh.processCallbackQuery(app, &update)
} else if update.PollAnswer != nil {
uh.processPollAnswer(app, &update)
} else {
uh.handleProcessUpdateError(&update, "message_type_not_supported")
}
}