mirror of
https://github.com/HugeFrog24/go-telegram-bot.git
synced 2026-03-02 00:14:34 +00:00
Multibot finished
This commit is contained in:
38
handlers.go
38
handlers.go
@@ -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.")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user