-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
164 lines (134 loc) · 4.53 KB
/
handler.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
package tbb
import (
"encoding/json"
"fmt"
"github.com/NicoNex/echotron/v3"
"strings"
)
type CommandRegistry map[string]Command
type Command struct {
Name string
Description string
Params []string
Data any
Handler CommandHandler
}
type CommandHandler interface {
Bot() *Bot
SetBot(*Bot)
Handle() StateFn
}
type DefaultCommandHandler struct {
bot *Bot
}
func (h *DefaultCommandHandler) Bot() *Bot {
return h.bot
}
func (h *DefaultCommandHandler) SetBot(bot *Bot) {
h.bot = bot
}
func (h *DefaultCommandHandler) Handle() StateFn {
h.bot.logger.Info("Command received!", "command", h.bot.cmd.Name, "params", fmt.Sprintf("[%s]", strings.Join(h.bot.cmd.Params, ",")))
return nil
}
type UpdateHandlerFn func() UpdateHandler
type UpdateHandler interface {
Bot() *Bot
SetBot(*Bot)
HandleMessage(echotron.Message) StateFn
HandleEditedMessage(echotron.Message) StateFn
HandleChannelPost(echotron.Message) StateFn
HandleEditedChannelPost(echotron.Message) StateFn
HandleInlineQuery(echotron.InlineQuery) StateFn
HandleChosenInlineResult(echotron.ChosenInlineResult) StateFn
HandleCallbackQuery(echotron.CallbackQuery) StateFn
HandleShippingQuery(echotron.ShippingQuery) StateFn
HandlePreCheckoutQuery(echotron.PreCheckoutQuery) StateFn
HandleChatMember(echotron.ChatMemberUpdated) StateFn
HandleMyChatMember(echotron.ChatMemberUpdated) StateFn
HandleChatJoinRequest(echotron.ChatJoinRequest) StateFn
}
// DefaultUpdateHandler implements the UpdateHandler interface
type DefaultUpdateHandler struct {
bot *Bot
}
func (h *DefaultUpdateHandler) Bot() *Bot {
return h.bot
}
func (h *DefaultUpdateHandler) SetBot(bot *Bot) {
h.bot = bot
}
func (h *DefaultUpdateHandler) HandleMessage(m echotron.Message) StateFn {
h.bot.Log().Info("Method: HandleMessage", "Message", h.printAsJson(m))
return nil
}
func (h *DefaultUpdateHandler) HandleEditedMessage(m echotron.Message) StateFn {
h.bot.Log().Info("Method: HandleEditedMessage", "Message", h.printAsJson(m))
return nil
}
func (h *DefaultUpdateHandler) HandleChannelPost(m echotron.Message) StateFn {
h.bot.Log().Info("Method: HandleChannelPost", "Message", h.printAsJson(m))
return nil
}
func (h *DefaultUpdateHandler) HandleEditedChannelPost(m echotron.Message) StateFn {
h.bot.Log().Info("Method: HandleEditedChannelPost", "Message", h.printAsJson(m))
return nil
}
func (h *DefaultUpdateHandler) HandleInlineQuery(i echotron.InlineQuery) StateFn {
h.bot.Log().Info("Method: HandleInlineQuery", "InlineQuery", h.printAsJson(i))
return nil
}
func (h *DefaultUpdateHandler) HandleChosenInlineResult(c echotron.ChosenInlineResult) StateFn {
h.bot.Log().Info("Method: HandleChosenInlineResult", "ChosenInlineResult", h.printAsJson(c))
return nil
}
func (h *DefaultUpdateHandler) HandleCallbackQuery(c echotron.CallbackQuery) StateFn {
h.bot.Log().Info("Method: HandleCallbackQuery", "CallbackQuery", h.printAsJson(c))
return nil
}
func (h *DefaultUpdateHandler) HandleShippingQuery(s echotron.ShippingQuery) StateFn {
h.bot.Log().Info("Method: HandleShippingQuery", "ShippingQuery", h.printAsJson(s))
return nil
}
func (h *DefaultUpdateHandler) HandlePreCheckoutQuery(p echotron.PreCheckoutQuery) StateFn {
h.bot.Log().Info("Method: HandlePreCheckoutQuery", "PreCheckoutQuery", h.printAsJson(p))
return nil
}
func (h *DefaultUpdateHandler) HandleChatMember(c echotron.ChatMemberUpdated) StateFn {
h.bot.Log().Info("Method: HandleChatMember", "ChatMemberUpdated", h.printAsJson(c))
return nil
}
func (h *DefaultUpdateHandler) HandleChatJoinRequest(c echotron.ChatJoinRequest) StateFn {
h.bot.Log().Info("Method: HandleChatJoinRequest", "ChatJoinRequest", h.printAsJson(c))
return nil
}
func (h *DefaultUpdateHandler) HandleMyChatMember(c echotron.ChatMemberUpdated) StateFn {
h.bot.Log().Info("Method: HandleMyChatMember", "ChatMemberUpdated", h.printAsJson(c))
status := c.NewChatMember.Status
switch status {
case memberStatusJoin:
// User unblocked the Bot
h.bot.Log().Info("Bot unblocked by user", "status", status, "user", h.bot.user.Firstname)
h.bot.EnableUser()
case memberStatusLeave:
// User blocked the Bot
h.bot.Log().Info("Bot blocked by user", "status", status, "user", h.bot.user.Firstname)
h.bot.DisableUser()
h.bot.DB().Save(h.bot.user)
default:
// Unknown
h.bot.Log().Info("MyChatMember.Status", "status", status, "user", c.From)
}
return nil
}
func (h *DefaultUpdateHandler) printAsJson(v any) string {
var (
err error
jsonStr []byte
)
jsonStr, err = json.Marshal(v)
if err != nil {
h.bot.Log().Error(err.Error())
}
return string(jsonStr)
}