2024-10-27 21:35:35 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mymmrac/telego"
|
|
|
|
"log/slog"
|
|
|
|
)
|
|
|
|
|
2024-10-30 16:12:39 +00:00
|
|
|
const HistoryLength = 150
|
2024-10-27 21:35:35 +00:00
|
|
|
|
2024-11-03 22:16:26 +00:00
|
|
|
type MessageData struct {
|
|
|
|
Name string
|
|
|
|
Username string
|
|
|
|
Text string
|
|
|
|
IsMe bool
|
|
|
|
IsUserRequest bool
|
|
|
|
ReplyTo *MessageData
|
2024-10-27 23:04:35 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 22:18:18 +00:00
|
|
|
type MessageHistory struct {
|
2024-11-03 22:16:26 +00:00
|
|
|
messages []MessageData
|
2024-10-27 21:35:35 +00:00
|
|
|
capacity int
|
|
|
|
}
|
|
|
|
|
2024-11-03 22:18:18 +00:00
|
|
|
func NewMessageHistory(capacity int) *MessageHistory {
|
|
|
|
return &MessageHistory{
|
2024-11-03 22:16:26 +00:00
|
|
|
messages: make([]MessageData, 0, capacity),
|
2024-10-27 21:35:35 +00:00
|
|
|
capacity: capacity,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-03 22:18:18 +00:00
|
|
|
func (b *MessageHistory) Push(element MessageData) {
|
2024-10-27 21:35:35 +00:00
|
|
|
if len(b.messages) >= b.capacity {
|
|
|
|
b.messages = b.messages[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
b.messages = append(b.messages, element)
|
|
|
|
}
|
|
|
|
|
2024-11-03 22:18:18 +00:00
|
|
|
func (b *MessageHistory) GetAll() []MessageData {
|
2024-10-27 21:35:35 +00:00
|
|
|
return b.messages
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) saveChatMessageToHistory(message *telego.Message) {
|
|
|
|
chatId := message.Chat.ID
|
|
|
|
|
|
|
|
slog.Info(
|
|
|
|
"history-message-save",
|
|
|
|
"chat", chatId,
|
|
|
|
"from_id", message.From.ID,
|
|
|
|
"from_name", message.From.FirstName,
|
|
|
|
"text", message.Text,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, ok := b.history[chatId]
|
|
|
|
if !ok {
|
2024-11-03 22:18:18 +00:00
|
|
|
b.history[chatId] = NewMessageHistory(HistoryLength)
|
2024-10-27 21:35:35 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 22:16:26 +00:00
|
|
|
msgData := tgUserMessageToMessageData(message)
|
|
|
|
|
|
|
|
b.history[chatId].Push(msgData)
|
2024-10-27 21:35:35 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 22:16:26 +00:00
|
|
|
func (b *Bot) saveBotReplyToHistory(message *telego.Message, text string) {
|
2024-10-27 21:35:35 +00:00
|
|
|
chatId := message.Chat.ID
|
|
|
|
|
|
|
|
slog.Info(
|
|
|
|
"history-reply-save",
|
|
|
|
"chat", chatId,
|
|
|
|
"to_id", message.From.ID,
|
|
|
|
"to_name", message.From.FirstName,
|
2024-11-03 22:16:26 +00:00
|
|
|
"text", text,
|
2024-10-27 21:35:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
_, ok := b.history[chatId]
|
|
|
|
if !ok {
|
2024-11-03 22:18:18 +00:00
|
|
|
b.history[chatId] = NewMessageHistory(HistoryLength)
|
2024-10-27 21:35:35 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 22:16:26 +00:00
|
|
|
msgData := MessageData{
|
|
|
|
Name: b.profile.Name,
|
|
|
|
Username: b.profile.Username,
|
|
|
|
Text: text,
|
|
|
|
IsMe: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if message.ReplyToMessage != nil {
|
|
|
|
replyMessage := message.ReplyToMessage
|
|
|
|
|
|
|
|
msgData.ReplyTo = &MessageData{
|
|
|
|
Name: replyMessage.From.FirstName,
|
|
|
|
Username: replyMessage.From.Username,
|
|
|
|
Text: replyMessage.Text,
|
|
|
|
IsMe: false,
|
|
|
|
ReplyTo: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.history[chatId].Push(msgData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tgUserMessageToMessageData(message *telego.Message) MessageData {
|
|
|
|
msgData := MessageData{
|
|
|
|
Name: message.From.FirstName,
|
|
|
|
Username: message.From.Username,
|
|
|
|
Text: message.Text,
|
|
|
|
IsMe: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
if message.ReplyToMessage != nil {
|
|
|
|
replyData := tgUserMessageToMessageData(message.ReplyToMessage)
|
|
|
|
msgData.ReplyTo = &replyData
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgData
|
2024-10-27 21:35:35 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 22:16:26 +00:00
|
|
|
func (b *Bot) getChatHistory(chatId int64) []MessageData {
|
2024-10-27 21:35:35 +00:00
|
|
|
_, ok := b.history[chatId]
|
|
|
|
if !ok {
|
2024-11-03 22:16:26 +00:00
|
|
|
return make([]MessageData, 0)
|
2024-10-27 21:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return b.history[chatId].GetAll()
|
|
|
|
}
|