-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmessage.go
53 lines (44 loc) · 895 Bytes
/
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
package main
import (
"strconv"
"time"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)
type Message struct {
Message *tb.Message `json:"message"`
duration time.Duration
}
type MessageOption func(m *Message)
func WithDuration(duration time.Duration, bot *tb.Bot) MessageOption {
return func(m *Message) {
m.duration = duration
go m.dispose(bot)
}
}
func NewMessage(m *tb.Message, opts ...MessageOption) *Message {
msg := &Message{
Message: m,
}
for _, opt := range opts {
opt(msg)
}
return msg
}
func (msg Message) Key() string {
return strconv.Itoa(msg.Message.ID)
}
func (msg Message) dispose(telegram *tb.Bot) {
// do not delete messages from private chat
if msg.Message.Private() {
return
}
go func() {
time.Sleep(msg.duration)
err := telegram.Delete(msg.Message)
if err != nil {
log.Println(err.Error())
return
}
}()
}