lightningtipbot/internal/telegram/message.go
LightningTipBot ba6bf0a317
telebot fork user ID int64 (#151)
Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
2021-12-01 11:24:28 +01:00

47 lines
818 B
Go

package telegram
import (
"strconv"
"time"
tb "gopkg.in/lightningtipbot/telebot.v2"
)
type Message struct {
Message *tb.Message `json:"message"`
duration time.Duration
}
type MessageOption func(m *Message)
func WithDuration(duration time.Duration, tipBot *TipBot) MessageOption {
return func(m *Message) {
m.duration = duration
go m.dispose(tipBot)
}
}
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(tipBot *TipBot) {
// do not delete messages from private chat
if msg.Message.Private() {
return
}
go func() {
time.Sleep(msg.duration)
tipBot.tryDeleteMessage(msg.Message)
}()
}