-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
102 lines (89 loc) · 2.48 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
package gtb
//A Handler responds to an Update.
//ServeUpdate should write reply data to the Bot then return.
type UpdateHandler interface {
ServeUpdate(*Bot, *Update)
}
type UpdateHandlerFunc func(*Bot, *Update)
func (f UpdateHandlerFunc) ServeUpdate(bot *Bot, upd *Update) {
f(bot, upd)
}
// MessageHandler
type MessageHandler interface {
ServeMessage(*Bot, *Message)
}
type MessageHandlerFunc func(*Bot, *Message)
func (f MessageHandlerFunc) ServeMessage(bot *Bot, msg *Message) {
f(bot, msg)
}
func (f MessageHandlerFunc) ServeUpdate(bot *Bot, upd *Update) {
if upd.Message != nil {
f(bot, upd.Message)
} else {
//TODO handle better
panic("Message handler cannot handle nil message")
}
}
type MigrationHandler interface {
ServeMigration(*Bot, int64, int64)
}
type MigrationHandlerFunc func(*Bot, int64, int64)
func (f MigrationHandlerFunc) ServeMigration(bot *Bot, from, to int64) {
f(bot, from, to)
}
func (f MigrationHandlerFunc) ServeMessage(bot *Bot, msg *Message) {
if msg.MigrateTo != 0 {
f(bot, msg.MigrateFrom, msg.MigrateTo)
} else {
//TODO handle better
panic("Message handler cannot handle nil message")
}
}
// Callback Handler
type CallbackHandler interface {
ServeCallback(*Bot, *Callback)
}
type CallbackHandlerFunc func(*Bot, *Callback)
func (f CallbackHandlerFunc) ServeCallback(bot *Bot, cb *Callback) {
f(bot, cb)
}
func (f CallbackHandlerFunc) ServeUpdate(bot *Bot, upd *Update) {
if upd.Callback != nil {
f(bot, upd.Callback)
} else {
//TODO handle better
panic("Callback handler cannot handle nil callback")
}
}
// QueryHandler
type QueryHandler interface {
ServeQuery(*Bot, *Query)
}
type QueryHandlerFunc func(*Bot, *Query)
func (f QueryHandlerFunc) ServeQuery(bot *Bot, q *Query) {
f(bot, q)
}
func (f QueryHandlerFunc) ServeUpdate(bot *Bot, upd *Update) {
if upd.Query != nil {
f(bot, upd.Query)
} else {
//TODO handle better
panic("Callback handler cannot handle nil callback")
}
}
// ChosenInlineResultHandler
type ChosenInlineResultHandler interface {
ServeChosenInlineResult(*Bot, *ChosenInlineResult)
}
type ChosenInlineResultHandlerFunc func(*Bot, *ChosenInlineResult)
func (f ChosenInlineResultHandlerFunc) ServeChosenInlineResult(bot *Bot, in_res *ChosenInlineResult) {
f(bot, in_res)
}
func (f ChosenInlineResultHandlerFunc) ServeUpdate(bot *Bot, upd *Update) {
if upd.ChosenInlineResult != nil {
f(bot, upd.ChosenInlineResult)
} else {
//TODO handle better
panic("ChosenInlineResult handler cannot handle nil chosen inline result")
}
}