Multibot finished

This commit is contained in:
HugeFrog24
2024-10-13 16:41:03 +02:00
parent 9f2b3df4c8
commit 0ab56448c7
15 changed files with 429 additions and 824 deletions

View File

@@ -3,9 +3,11 @@ package main
import (
"context"
"log"
"strings"
"github.com/go-telegram/bot"
"github.com/go-telegram/bot/models"
"github.com/liushuangls/go-anthropic/v2"
)
func (b *Bot) handleUpdate(ctx context.Context, tgBot *bot.Bot, update *models.Update) {
@@ -16,6 +18,21 @@ func (b *Bot) handleUpdate(ctx context.Context, tgBot *bot.Bot, update *models.U
chatID := update.Message.Chat.ID
userID := update.Message.From.ID
// Check if the message is a command
if update.Message.Entities != nil {
for _, entity := range update.Message.Entities {
if entity.Type == "bot_command" {
command := strings.TrimSpace(update.Message.Text[entity.Offset : entity.Offset+entity.Length])
switch command {
case "/stats":
b.sendStats(ctx, chatID)
return
}
}
}
}
// Existing rate limit and message handling
if !b.checkRateLimits(userID) {
b.sendRateLimitExceededMessage(ctx, chatID)
return
@@ -31,6 +48,7 @@ func (b *Bot) handleUpdate(ctx context.Context, tgBot *bot.Bot, update *models.U
}
userMessage := b.createMessage(chatID, userID, username, user.Role.Name, text, true)
userMessage.UserRole = string(anthropic.RoleUser) // Convert to string
b.storeMessage(userMessage)
chatMemory := b.getOrCreateChatMemory(chatID)
@@ -46,27 +64,11 @@ func (b *Bot) handleUpdate(ctx context.Context, tgBot *bot.Bot, update *models.U
b.sendResponse(ctx, chatID, response)
assistantMessage := b.createMessage(chatID, 0, "Assistant", "assistant", response, false)
assistantMessage := b.createMessage(chatID, 0, "", string(anthropic.RoleAssistant), response, false)
b.storeMessage(assistantMessage)
b.addMessageToChatMemory(chatMemory, assistantMessage)
}
func (b *Bot) sendRateLimitExceededMessage(ctx context.Context, chatID int64) {
_, err := b.tgBot.SendMessage(ctx, &bot.SendMessageParams{
ChatID: chatID,
Text: "Rate limit exceeded. Please try again later.",
})
if err != nil {
log.Printf("Error sending rate limit message: %v", err)
}
}
func (b *Bot) sendResponse(ctx context.Context, chatID int64, text string) {
_, err := b.tgBot.SendMessage(ctx, &bot.SendMessageParams{
ChatID: chatID,
Text: text,
})
if err != nil {
log.Printf("Error sending message: %v", err)
}
b.sendResponse(ctx, chatID, "Rate limit exceeded. Please try again later.")
}