forked from PaulSonOfLars/gotgbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
54 lines (45 loc) · 1.28 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
package handlers
import (
"fmt"
"github.com/asvedr/gotgbot/v2"
"github.com/asvedr/gotgbot/v2/ext"
"github.com/asvedr/gotgbot/v2/ext/handlers/filters"
)
type Message struct {
AllowEdited bool
AllowChannel bool
Filter filters.Message
Response Response
}
func NewMessage(f filters.Message, r Response) Message {
return Message{
AllowEdited: false,
AllowChannel: false,
Filter: f,
Response: r,
}
}
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)
}
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)
}