-
Notifications
You must be signed in to change notification settings - Fork 99
/
message.go
81 lines (68 loc) · 2.06 KB
/
message.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
package handlers
import (
"fmt"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers/filters"
)
type Message struct {
AllowEdited bool
AllowChannel bool
AllowBusiness bool
Filter filters.Message
Response Response
}
func NewMessage(f filters.Message, r Response) Message {
return Message{
AllowEdited: false,
AllowChannel: false,
Filter: f,
Response: r,
}
}
// SetAllowEdited Enables edited messages for this handler.
func (m Message) SetAllowEdited(allow bool) Message {
m.AllowEdited = allow
return m
}
// SetAllowChannel Enables channel messages for this handler.
func (m Message) SetAllowChannel(allow bool) Message {
m.AllowChannel = allow
return m
}
// SetAllowBusiness Enables business messages for this handler.
func (m Message) SetAllowBusiness(allow bool) Message {
m.AllowBusiness = allow
return m
}
func (m Message) CheckUpdate(b *gotgbot.Bot, ctx *ext.Context) bool {
if ctx.Message != nil {
return m.Filter == nil || m.Filter(ctx.Message)
}
// If edits are allowed, and message is edited.
if m.AllowEdited && ctx.EditedMessage != nil {
return m.Filter == nil || m.Filter(ctx.EditedMessage)
}
// If channel posts are allowed, and message is channel post.
if m.AllowChannel && ctx.ChannelPost != nil {
return m.Filter == nil || m.Filter(ctx.ChannelPost)
}
// If channel posts and edits are allowed, and post is edited.
if m.AllowChannel && m.AllowEdited && ctx.EditedChannelPost != nil {
return m.Filter == nil || m.Filter(ctx.EditedChannelPost)
}
// Same logic, for business messages
if m.AllowBusiness && ctx.BusinessMessage != nil {
return m.Filter == nil || m.Filter(ctx.BusinessMessage)
}
if m.AllowBusiness && m.AllowEdited && ctx.EditedBusinessMessage != nil {
return m.Filter == nil || m.Filter(ctx.EditedBusinessMessage)
}
return false
}
func (m Message) HandleUpdate(b *gotgbot.Bot, ctx *ext.Context) error {
return m.Response(b, ctx)
}
func (m Message) Name() string {
return fmt.Sprintf("message_%p", m.Response)
}