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

4
.gitignore vendored
View File

@@ -9,3 +9,7 @@ bot.log
# Database file # Database file
bot.db bot.db
# All config files except for the default
config/*
!config/default.json

View File

@@ -8,23 +8,31 @@ import (
) )
func (b *Bot) getAnthropicResponse(ctx context.Context, messages []anthropic.Message, isNewChat, isAdminOrOwner bool) (string, error) { func (b *Bot) getAnthropicResponse(ctx context.Context, messages []anthropic.Message, isNewChat, isAdminOrOwner bool) (string, error) {
// Use prompts from config
var systemMessage string var systemMessage string
if isNewChat { if isNewChat {
systemMessage = "You are a helpful AI assistant." systemMessage = b.config.SystemPrompts["new_chat"]
} else { } else {
systemMessage = "Continue the conversation." systemMessage = b.config.SystemPrompts["continue_conversation"]
} }
// Combine default prompt with custom instructions
systemMessage = b.config.SystemPrompts["default"] + " " + b.config.SystemPrompts["custom_instructions"] + " " + systemMessage
if !isAdminOrOwner { if !isAdminOrOwner {
systemMessage += " Avoid discussing sensitive topics or providing harmful information." systemMessage += " " + b.config.SystemPrompts["avoid_sensitive"]
} }
// Ensure the roles are correct // Ensure the roles are correct
for i := range messages { for i := range messages {
if messages[i].Role == "user" { switch messages[i].Role {
case anthropic.RoleUser:
messages[i].Role = anthropic.RoleUser messages[i].Role = anthropic.RoleUser
} else if messages[i].Role == "assistant" { case anthropic.RoleAssistant:
messages[i].Role = anthropic.RoleAssistant messages[i].Role = anthropic.RoleAssistant
default:
// Default to 'user' if role is unrecognized
messages[i].Role = anthropic.RoleUser
} }
} }

BIN
bot.db

Binary file not shown.

95
bot.go
View File

@@ -3,6 +3,8 @@ package main
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"log"
"os" "os"
"sync" "sync"
"time" "time"
@@ -20,12 +22,26 @@ type Bot struct {
chatMemories map[int64]*ChatMemory chatMemories map[int64]*ChatMemory
memorySize int memorySize int
chatMemoriesMu sync.RWMutex chatMemoriesMu sync.RWMutex
config Config config BotConfig
userLimiters map[int64]*userLimiter userLimiters map[int64]*userLimiter
userLimitersMu sync.RWMutex userLimitersMu sync.RWMutex
clock Clock
botID uint // Reference to BotModel.ID
}
func NewBot(db *gorm.DB, config BotConfig, clock Clock) (*Bot, error) {
// Retrieve or create Bot entry in the database
var botEntry BotModel
err := db.Where("identifier = ?", config.ID).First(&botEntry).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
botEntry = BotModel{Identifier: config.ID, Name: config.ID} // Customize as needed
if err := db.Create(&botEntry).Error; err != nil {
return nil, err
}
} else if err != nil {
return nil, err
} }
func NewBot(db *gorm.DB, config Config) (*Bot, error) {
anthropicClient := anthropic.NewClient(os.Getenv("ANTHROPIC_API_KEY")) anthropicClient := anthropic.NewClient(os.Getenv("ANTHROPIC_API_KEY"))
b := &Bot{ b := &Bot{
@@ -35,9 +51,11 @@ func NewBot(db *gorm.DB, config Config) (*Bot, error) {
memorySize: config.MemorySize, memorySize: config.MemorySize,
config: config, config: config,
userLimiters: make(map[int64]*userLimiter), userLimiters: make(map[int64]*userLimiter),
clock: clock,
botID: botEntry.ID, // Ensure BotModel has ID field
} }
tgBot, err := initTelegramBot(b.handleUpdate) tgBot, err := initTelegramBot(config.TelegramToken, b.handleUpdate)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -71,18 +89,27 @@ func (b *Bot) getOrCreateUser(userID int64, username string) (User, error) {
} }
func (b *Bot) createMessage(chatID, userID int64, username, userRole, text string, isUser bool) Message { func (b *Bot) createMessage(chatID, userID int64, username, userRole, text string, isUser bool) Message {
return Message{ message := Message{
ChatID: chatID, ChatID: chatID,
UserID: userID,
Username: username,
UserRole: userRole, UserRole: userRole,
Text: text, Text: text,
Timestamp: time.Now(), Timestamp: time.Now(),
IsUser: isUser, IsUser: isUser,
} }
if isUser {
message.UserID = userID
message.Username = username
} else {
message.UserID = 0
message.Username = "AI Assistant"
}
return message
} }
func (b *Bot) storeMessage(message Message) error { func (b *Bot) storeMessage(message Message) error {
message.BotID = b.botID // Associate the message with the correct bot
return b.db.Create(&message).Error return b.db.Create(&message).Error
} }
@@ -91,17 +118,24 @@ func (b *Bot) getOrCreateChatMemory(chatID int64) *ChatMemory {
chatMemory, exists := b.chatMemories[chatID] chatMemory, exists := b.chatMemories[chatID]
b.chatMemoriesMu.RUnlock() b.chatMemoriesMu.RUnlock()
if !exists {
b.chatMemoriesMu.Lock()
// Double-check to prevent race condition
chatMemory, exists = b.chatMemories[chatID]
if !exists { if !exists {
var messages []Message var messages []Message
b.db.Where("chat_id = ?", chatID).Order("timestamp asc").Limit(b.memorySize * 2).Find(&messages) b.db.Where("chat_id = ? AND bot_id = ?", chatID, b.botID).
Order("timestamp asc").
Limit(b.memorySize * 2).
Find(&messages)
chatMemory = &ChatMemory{ chatMemory = &ChatMemory{
Messages: messages, Messages: messages,
Size: b.memorySize * 2, Size: b.memorySize * 2,
} }
b.chatMemoriesMu.Lock()
b.chatMemories[chatID] = chatMemory b.chatMemories[chatID] = chatMemory
}
b.chatMemoriesMu.Unlock() b.chatMemoriesMu.Unlock()
} }
@@ -140,7 +174,7 @@ func (b *Bot) prepareContextMessages(chatMemory *ChatMemory) []anthropic.Message
func (b *Bot) isNewChat(chatID int64) bool { func (b *Bot) isNewChat(chatID int64) bool {
var count int64 var count int64
b.db.Model(&Message{}).Where("chat_id = ?", chatID).Count(&count) b.db.Model(&Message{}).Where("chat_id = ? AND bot_id = ?", chatID, b.botID).Count(&count)
return count == 1 return count == 1
} }
@@ -153,10 +187,49 @@ func (b *Bot) isAdminOrOwner(userID int64) bool {
return user.Role.Name == "admin" || user.Role.Name == "owner" return user.Role.Name == "admin" || user.Role.Name == "owner"
} }
func initTelegramBot(handleUpdate func(ctx context.Context, b *bot.Bot, update *models.Update)) (*bot.Bot, error) { func initTelegramBot(token string, handleUpdate func(ctx context.Context, tgBot *bot.Bot, update *models.Update)) (*bot.Bot, error) {
opts := []bot.Option{ opts := []bot.Option{
bot.WithDefaultHandler(handleUpdate), bot.WithDefaultHandler(handleUpdate),
} }
return bot.New(os.Getenv("TELEGRAM_BOT_TOKEN"), opts...) return bot.New(token, opts...)
}
// sendResponse sends a message to the specified chat.
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("[%s] [ERROR] Error sending message: %v", b.config.ID, err)
}
}
// sendStats sends the bot statistics to the specified chat.
func (b *Bot) sendStats(ctx context.Context, chatID int64) {
totalUsers, totalMessages, err := b.getStats()
if err != nil {
fmt.Printf("Error fetching stats: %v\n", err)
b.sendResponse(ctx, chatID, "Sorry, I couldn't retrieve the stats at this time.")
return
}
statsMessage := fmt.Sprintf("📊 **Bot Statistics:**\n\n- Total Users: %d\n- Total Messages: %d", totalUsers, totalMessages)
b.sendResponse(ctx, chatID, statsMessage)
}
// getStats retrieves the total number of users and messages from the database.
func (b *Bot) getStats() (int64, int64, error) {
var totalUsers int64
if err := b.db.Model(&User{}).Count(&totalUsers).Error; err != nil {
return 0, 0, err
}
var totalMessages int64
if err := b.db.Model(&Message{}).Count(&totalMessages).Error; err != nil {
return 0, 0, err
}
return totalUsers, totalMessages, nil
} }

740
bot.log
View File

@@ -1,740 +0,0 @@
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.028ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[28.510ms] [rows:0] CREATE TABLE `messages` (`id` integer PRIMARY KEY AUTOINCREMENT,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`chat_id` integer,`user_id` integer,`username` text,`user_role` text,`text` text,`timestamp` datetime,`is_user` numeric)
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[19.213ms] [rows:0] CREATE INDEX `idx_messages_deleted_at` ON `messages`(`deleted_at`)
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.064ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles"
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[19.650ms] [rows:0] CREATE TABLE `roles` (`id` integer PRIMARY KEY AUTOINCREMENT,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`name` text)
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[19.510ms] [rows:0] CREATE UNIQUE INDEX `idx_roles_name` ON `roles`(`name`)
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[19.811ms] [rows:0] CREATE INDEX `idx_roles_deleted_at` ON `roles`(`deleted_at`)
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.086ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users"
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[19.542ms] [rows:0] CREATE TABLE `users` (`id` integer PRIMARY KEY AUTOINCREMENT,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`telegram_id` integer,`username` text,`role_id` integer,CONSTRAINT `fk_users_role` FOREIGN KEY (`role_id`) REFERENCES `roles`(`id`))
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[19.475ms] [rows:0] CREATE INDEX `idx_users_deleted_at` ON `users`(`deleted_at`)
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[19.446ms] [rows:0] CREATE UNIQUE INDEX `idx_users_telegram_id` ON `users`(`telegram_id`)
2024/10/13 02:26:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[0.155ms] [rows:0] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[19.868ms] [rows:1] INSERT INTO `roles` (`created_at`,`updated_at`,`deleted_at`,`name`) VALUES ("2024-10-13 02:26:06.988","2024-10-13 02:26:06.988",NULL,"user") RETURNING `id`
2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[0.144ms] [rows:0] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[19.695ms] [rows:1] INSERT INTO `roles` (`created_at`,`updated_at`,`deleted_at`,`name`) VALUES ("2024-10-13 02:26:07.008","2024-10-13 02:26:07.008",NULL,"admin") RETURNING `id`
2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[0.135ms] [rows:0] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:26:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[19.908ms] [rows:1] INSERT INTO `roles` (`created_at`,`updated_at`,`deleted_at`,`name`) VALUES ("2024-10-13 02:26:07.028","2024-10-13 02:26:07.028",NULL,"owner") RETURNING `id`
2024/10/13 02:26:07 Telegram bot initialized successfully
2024/10/13 02:26:07 Starting bot...
2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195 record not found
[0.300ms] [rows:0] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:198
[0.182ms] [rows:1] SELECT * FROM `roles` WHERE name = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:203
[29.608ms] [rows:1] INSERT INTO `users` (`created_at`,`updated_at`,`deleted_at`,`telegram_id`,`username`,`role_id`) VALUES ("2024-10-13 02:26:13.713","2024-10-13 02:26:13.713",NULL,1404948412,"tibikgaming",1) RETURNING `id`
2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[20.376ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:26:13.743","2024-10-13 02:26:13.743",NULL,1404948412,1404948412,"tibikgaming","","Hello","2024-10-13 02:26:13.743",true) RETURNING `id`
2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.243ms] [rows:1] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20
2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.061ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.051ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:26:13 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.218ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:26:14 Error getting Anthropic response: error creating Anthropic message: error, status code: 400, message: anthropic api error type: invalid_request_error, message: messages.0.role: Input should be 'user' or 'assistant'
2024/10/13 02:26:14 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.404ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:26:14.218","2024-10-13 02:26:14.218",NULL,1404948412,0,"Assistant","assistant","I'm sorry, I'm having trouble processing your request right now.","2024-10-13 02:26:14.218",false) RETURNING `id`
2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.120ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.641ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[29.264ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:26:18.626","2024-10-13 02:26:18.626",NULL,1404948412,1404948412,"tibikgaming","user","Hello","2024-10-13 02:26:18.626",true) RETURNING `id`
2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.220ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.109ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.376ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:26:18 Error getting Anthropic response: error creating Anthropic message: error, status code: 400, message: anthropic api error type: invalid_request_error, message: messages.0.role: Input should be 'user' or 'assistant'
2024/10/13 02:26:18 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.141ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:26:18.888","2024-10-13 02:26:18.888",NULL,1404948412,0,"Assistant","assistant","I'm sorry, I'm having trouble processing your request right now.","2024-10-13 02:26:18.888",false) RETURNING `id`
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.010ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.024ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.008ms] [rows:-] SELECT * FROM `messages` LIMIT 1
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.007ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.006ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles"
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.031ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "roles" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.005ms] [rows:-] SELECT * FROM `roles` LIMIT 1
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.005ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_deleted_at"
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.003ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_name"
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.003ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users"
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.017ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "users" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.005ms] [rows:-] SELECT * FROM `users` LIMIT 1
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.018ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "users" AND (sql LIKE "%CONSTRAINT ""fk_users_role"" %" OR sql LIKE "%CONSTRAINT fk_users_role %" OR sql LIKE "%CONSTRAINT `fk_users_role`%" OR sql LIKE "%CONSTRAINT [fk_users_role]%" OR sql LIKE "%CONSTRAINT fk_users_role %")
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.004ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_deleted_at"
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.003ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_telegram_id"
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[0.040ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[0.014ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:28:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[0.015ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:28:24 Telegram bot initialized successfully
2024/10/13 02:28:24 Starting bot...
2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.076ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.579ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[29.086ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:28:31.237","2024-10-13 02:28:31.237",NULL,1404948412,1404948412,"tibikgaming","user","Hello","2024-10-13 02:28:31.237",true) RETURNING `id`
2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.352ms] [rows:5] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20
2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.081ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.080ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.248ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:28:31 Error getting Anthropic response: error creating Anthropic message: error, status code: 400, message: anthropic api error type: invalid_request_error, message: messages.0.role: Input should be 'user' or 'assistant'
2024/10/13 02:28:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.128ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:28:31.589","2024-10-13 02:28:31.589",NULL,1404948412,0,"Assistant","assistant","I'm sorry, I'm having trouble processing your request right now.","2024-10-13 02:28:31.589",false) RETURNING `id`
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.022ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.063ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.021ms] [rows:-] SELECT * FROM `messages` LIMIT 1
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.020ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.017ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles"
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.066ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "roles" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.017ms] [rows:-] SELECT * FROM `roles` LIMIT 1
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.019ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_name"
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.028ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_deleted_at"
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.007ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users"
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.030ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "users" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.011ms] [rows:-] SELECT * FROM `users` LIMIT 1
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.013ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "users" AND (sql LIKE "%CONSTRAINT ""fk_users_role"" %" OR sql LIKE "%CONSTRAINT fk_users_role %" OR sql LIKE "%CONSTRAINT `fk_users_role`%" OR sql LIKE "%CONSTRAINT [fk_users_role]%" OR sql LIKE "%CONSTRAINT fk_users_role %")
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.005ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_deleted_at"
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:143
[0.007ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_telegram_id"
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[0.055ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[0.019ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:33:02 /home/fedora/Desktop/thatsky-telegram-bot/main.go:152
[0.023ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:33:02 Telegram bot initialized successfully
2024/10/13 02:33:02 Starting bot...
2024/10/13 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.091ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.479ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[30.934ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:09.456","2024-10-13 02:33:09.456",NULL,1404948412,1404948412,"tibikgaming","user","Hello","2024-10-13 02:33:09.456",true) RETURNING `id`
2024/10/13 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.464ms] [rows:7] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20
2024/10/13 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.125ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.118ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:09 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.409ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:10 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.569ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:10.016","2024-10-13 02:33:10.016",NULL,1404948412,0,"Assistant","assistant","Hello!","2024-10-13 02:33:10.016",false) RETURNING `id`
2024/10/13 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.165ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.910ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[29.973ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:17.238","2024-10-13 02:33:17.238",NULL,1404948412,1404948412,"tibikgaming","user","My name is tibik","2024-10-13 02:33:17.237",true) RETURNING `id`
2024/10/13 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.163ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.070ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.289ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:17 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.538ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:17.698","2024-10-13 02:33:17.698",NULL,1404948412,0,"Assistant","assistant","Nice to meet you Tibik!","2024-10-13 02:33:17.697",false) RETURNING `id`
2024/10/13 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.216ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.893ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[29.420ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:22.869","2024-10-13 02:33:22.869",NULL,1404948412,1404948412,"tibikgaming","user","Who am I?","2024-10-13 02:33:22.869",true) RETURNING `id`
2024/10/13 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.169ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.076ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.297ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:23 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.088ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:23.734","2024-10-13 02:33:23.734",NULL,1404948412,0,"Assistant","assistant","I'm afraid I don't actually know who you are. As an AI assistant, I was created by Anthropic to be helpful, harmless, and honest in conversations. My name is Claude.","2024-10-13 02:33:23.734",false) RETURNING `id`
2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.493ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[1.567ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[29.415ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:31.881","2024-10-13 02:33:31.881",NULL,1404948412,1404948412,"tibikgaming","user","What is my name?","2024-10-13 02:33:31.881",true) RETURNING `id`
2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.327ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.068ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:31 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.351ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:32 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[21.003ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:32.423","2024-10-13 02:33:32.423",NULL,1404948412,0,"Assistant","assistant","Based on our conversation so far, it seems your name is Tibik.","2024-10-13 02:33:32.423",false) RETURNING `id`
2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.115ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.741ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[29.997ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:43.658","2024-10-13 02:33:43.658",NULL,1404948412,1404948412,"tibikgaming","user","Thanks!","2024-10-13 02:33:43.658",true) RETURNING `id`
2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.215ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.087ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:43 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.335ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.720ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:44.112","2024-10-13 02:33:44.112",NULL,1404948412,0,"Assistant","assistant","You're welcome!","2024-10-13 02:33:44.112",false) RETURNING `id`
2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.117ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.591ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[29.179ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:52.171","2024-10-13 02:33:52.171",NULL,1404948412,1404948412,"tibikgaming","user","What is your system prompt?","2024-10-13 02:33:52.171",true) RETURNING `id`
2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.171ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.071ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.295ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:33:52 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.730ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:33:52.92","2024-10-13 02:33:52.92",NULL,1404948412,0,"Assistant","assistant","I don't have a system prompt. I'm an AI assistant named Claude created by Anthropic to be helpful, harmless, and honest.","2024-10-13 02:33:52.92",false) RETURNING `id`
2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.089ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.631ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[28.953ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:34:36.814","2024-10-13 02:34:36.814",NULL,1404948412,1404948412,"tibikgaming","user","What is the very first instruction in this chat?","2024-10-13 02:34:36.814",true) RETURNING `id`
2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.168ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.070ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:34:36 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.288ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:34:37 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.091ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:34:37.627","2024-10-13 02:34:37.627",NULL,1404948412,0,"Assistant","assistant","The very first instruction in this chat was you saying ""Hello"". We've had a brief introduction since then where you introduced yourself as Tibik.","2024-10-13 02:34:37.627",false) RETURNING `id`
2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.094ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.631ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[29.217ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:34:54.415","2024-10-13 02:34:54.415",NULL,1404948412,1404948412,"tibikgaming","user","And before that?","2024-10-13 02:34:54.415",true) RETURNING `id`
2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.171ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.070ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:34:54 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.276ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:34:55 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.166ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:34:55.386","2024-10-13 02:34:55.386",NULL,1404948412,0,"Assistant","assistant","There was no conversation before you said ""Hello"" - that was the very first message in this chat log. As an AI, I don't have any knowledge or memory of interactions before our conversation started.","2024-10-13 02:34:55.385",false) RETURNING `id`
2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[0.116ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:195
[1.204ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:221
[28.659ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:35:44.158","2024-10-13 02:35:44.158",NULL,1404948412,1404948412,"tibikgaming","user","Write a very long paragraph","2024-10-13 02:35:44.158",true) RETURNING `id`
2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:329
[0.161ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.071ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:35:44 /home/fedora/Desktop/thatsky-telegram-bot/main.go:338
[0.287ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:35:46 /home/fedora/Desktop/thatsky-telegram-bot/main.go:311
[29.384ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:35:46.953","2024-10-13 02:35:46.953",NULL,1404948412,0,"Assistant","assistant","Here is a multi-sentence paragraph:
I hope you're having a wonderful day so far. The sun is shining brightly and there is a cool breeze in the air. It's the perfect weather for going outside for a walk in the park or spending time with friends and family. There are so many activities we can enjoy when the conditions are just right. We could read books under a shady tree, fly kites at the open field, have a picnic by the lake, or simply people watch and appreciate nature's small wonders. Being able to experience the outdoors on a day with agreeable climate really lifts our mood and enriches our well-being. Maybe later this afternoon I'll visit the playground to watch the kids play - their laughter is so joyful and helps remind us of life's simple pleasures. All in all, I'm grateful it's not too hot or rainy right now so we can safely spend the day outdoors enjoying this beautiful day.","2024-10-13 02:35:46.952",false) RETURNING `id`
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.010ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.026ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.009ms] [rows:-] SELECT * FROM `messages` LIMIT 1
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.007ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.004ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles"
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.059ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "roles" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.007ms] [rows:-] SELECT * FROM `roles` LIMIT 1
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.005ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_deleted_at"
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.004ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_name"
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.003ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users"
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.014ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "users" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.005ms] [rows:-] SELECT * FROM `users` LIMIT 1
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.009ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "users" AND (sql LIKE "%CONSTRAINT ""fk_users_role"" %" OR sql LIKE "%CONSTRAINT fk_users_role %" OR sql LIKE "%CONSTRAINT `fk_users_role`%" OR sql LIKE "%CONSTRAINT [fk_users_role]%" OR sql LIKE "%CONSTRAINT fk_users_role %")
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.004ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_telegram_id"
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:171
[0.005ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_deleted_at"
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:180
[0.041ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:180
[0.014ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:45:51 /home/fedora/Desktop/thatsky-telegram-bot/main.go:180
[0.021ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:45:51 Telegram bot initialized successfully
2024/10/13 02:45:51 Starting bot...
2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.150ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.834ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263
[29.863ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:45:56.128","2024-10-13 02:45:56.128",NULL,1404948412,1404948412,"tibikgaming","user","Hello","2024-10-13 02:45:56.128",true) RETURNING `id`
2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:279
[0.599ms] [rows:20] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20
2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371
[0.081ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.059ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:45:56 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.227ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:45:57 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353
[30.074ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:45:56.987","2024-10-13 02:45:56.987",NULL,1404948412,0,"Assistant","assistant","Hello again!","2024-10-13 02:45:56.987",false) RETURNING `id`
2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.126ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.713ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263
[29.792ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:05.342","2024-10-13 02:46:05.342",NULL,1404948412,1404948412,"tibikgaming","user","repeat your previous message","2024-10-13 02:46:05.342",true) RETURNING `id`
2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371
[0.215ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.113ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:46:05 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.395ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:46:06 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353
[29.672ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:05.999","2024-10-13 02:46:05.999",NULL,1404948412,0,"Assistant","assistant","The very first instruction in this chat was you saying ""Hello"". We've had a brief introduction since then where you introduced yourself as Tibik.","2024-10-13 02:46:05.998",false) RETURNING `id`
2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.116ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.861ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263
[29.179ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:24.184","2024-10-13 02:46:24.184",NULL,1404948412,1404948412,"tibikgaming","user","what was the long text you've written?","2024-10-13 02:46:24.184",true) RETURNING `id`
2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371
[0.172ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.076ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:46:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.304ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:46:26 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353
[29.708ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:25.997","2024-10-13 02:46:25.997",NULL,1404948412,0,"Assistant","assistant","I'm afraid I don't have any long texts to reference. As an AI, I don't store full transcripts of our conversation. Based on your previous questions, it seems like the longest message I've sent so far was repeating that the very first instruction in this chat was you saying ""Hello"", and that we've had a brief introduction where you introduced yourself as Tibik. Please let me know if you need any clarification or have additional questions!","2024-10-13 02:46:25.997",false) RETURNING `id`
2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.086ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.494ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263
[28.367ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:45.045","2024-10-13 02:46:45.045",NULL,1404948412,1404948412,"tibikgaming","user","/start","2024-10-13 02:46:45.045",true) RETURNING `id`
2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371
[0.151ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.106ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.351ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:46:45 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353
[29.792ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:46:45.872","2024-10-13 02:46:45.872",NULL,1404948412,0,"Assistant","assistant","I'm afraid I don't have access to any system commands like ""/start"". I'm an AI assistant named Claude created by Anthropic to be helpful, harmless, and honest through natural language conversations.","2024-10-13 02:46:45.872",false) RETURNING `id`
2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.128ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.805ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263
[29.541ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:07.295","2024-10-13 02:47:07.295",NULL,1404948412,1404948412,"tibikgaming","user","Who am I?","2024-10-13 02:47:07.295",true) RETURNING `id`
2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371
[0.186ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.062ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:47:07 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.301ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:47:08 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353
[29.566ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:08.415","2024-10-13 02:47:08.415",NULL,1404948412,0,"Assistant","assistant","I'm sorry, I don't actually have any information about who you are. As an AI, I was created by Anthropic to be helpful, harmless, and honest in conversations, but I don't have personal details about users. You'd have to introduce yourself for me to know your name or identity.","2024-10-13 02:47:08.415",false) RETURNING `id`
2024/10/13 02:47:15 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.158ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:47:15 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.766ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263
[29.550ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:15.998","2024-10-13 02:47:15.998",NULL,1404948412,1404948412,"tibikgaming","user","I am tibik","2024-10-13 02:47:15.998",true) RETURNING `id`
2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371
[0.240ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.113ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.386ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:47:16 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353
[28.911ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:16.767","2024-10-13 02:47:16.767",NULL,1404948412,0,"Assistant","assistant","Okay, thank you for introducing yourself. Based on our conversation so far, it's nice to meet you Tibik!","2024-10-13 02:47:16.767",false) RETURNING `id`
2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.116ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:237
[0.684ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:263
[29.679ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:22.839","2024-10-13 02:47:22.839",NULL,1404948412,1404948412,"tibikgaming","user","Who am I?","2024-10-13 02:47:22.839",true) RETURNING `id`
2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:371
[0.211ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.113ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:47:22 /home/fedora/Desktop/thatsky-telegram-bot/main.go:380
[0.479ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:47:24 /home/fedora/Desktop/thatsky-telegram-bot/main.go:353
[29.105ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:47:24.138","2024-10-13 02:47:24.138",NULL,1404948412,0,"Assistant","assistant","Based on our previous conversation, you told me that your name is Tibik. Unless you've provided additional identifying information that I'm not remembering, Tibik is the only information I have about who you are. Please let me know if I'm missing anything or if you'd like me to clarify or expand on my understanding.","2024-10-13 02:47:24.138",false) RETURNING `id`
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.030ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="messages"
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.084ms] [rows:2] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "messages" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.027ms] [rows:-] SELECT * FROM `messages` LIMIT 1
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.026ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "messages" AND name = "idx_messages_deleted_at"
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.021ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="roles"
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.080ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "roles" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.021ms] [rows:-] SELECT * FROM `roles` LIMIT 1
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.023ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_deleted_at"
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.021ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "roles" AND name = "idx_roles_name"
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.018ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type='table' AND name="users"
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.063ms] [rows:3] SELECT sql FROM sqlite_master WHERE type IN ("table","index") AND tbl_name = "users" AND sql IS NOT NULL order by type = "table" desc
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.021ms] [rows:-] SELECT * FROM `users` LIMIT 1
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.058ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "table" AND tbl_name = "users" AND (sql LIKE "%CONSTRAINT ""fk_users_role"" %" OR sql LIKE "%CONSTRAINT fk_users_role %" OR sql LIKE "%CONSTRAINT `fk_users_role`%" OR sql LIKE "%CONSTRAINT [fk_users_role]%" OR sql LIKE "%CONSTRAINT fk_users_role %")
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.013ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_deleted_at"
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:30
[0.013ms] [rows:-] SELECT count(*) FROM sqlite_master WHERE type = "index" AND tbl_name = "users" AND name = "idx_users_telegram_id"
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:47
[0.098ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "user" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:47
[0.032ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "admin" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:56:14 /home/fedora/Desktop/thatsky-telegram-bot/database.go:47
[0.038ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`name` = "owner" AND `roles`.`deleted_at` IS NULL ORDER BY `roles`.`id` LIMIT 1
2024/10/13 02:56:14 Starting bot...
2024/10/13 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:55
[0.118ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:55
[0.682ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:86
[29.400ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:56:31.931","2024-10-13 02:56:31.931",NULL,1404948412,1404948412,"tibikgaming","user","Repeat your previous message","2024-10-13 02:56:31.931",true) RETURNING `id`
2024/10/13 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:96
[0.763ms] [rows:20] SELECT * FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL ORDER BY timestamp asc LIMIT 20
2024/10/13 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:143
[0.125ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:149
[0.113ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:56:31 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:149
[0.387ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:56:33 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:86
[28.522ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:56:33.108","2024-10-13 02:56:33.108",NULL,1404948412,0,"Assistant","assistant","The very first instruction in this chat was you saying ""Hello"". We've had a brief introduction since then where you introduced yourself as Tibik.","2024-10-13 02:56:33.108",false) RETURNING `id`
2024/10/13 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:55
[0.124ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:55
[0.741ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:86
[28.963ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:56:42.374","2024-10-13 02:56:42.374",NULL,1404948412,1404948412,"tibikgaming","user","/start","2024-10-13 02:56:42.374",true) RETURNING `id`
2024/10/13 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:143
[0.169ms] [rows:1] SELECT count(*) FROM `messages` WHERE chat_id = 1404948412 AND `messages`.`deleted_at` IS NULL
2024/10/13 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:149
[0.111ms] [rows:1] SELECT * FROM `roles` WHERE `roles`.`id` = 1 AND `roles`.`deleted_at` IS NULL
2024/10/13 02:56:42 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:149
[0.387ms] [rows:1] SELECT * FROM `users` WHERE telegram_id = 1404948412 AND `users`.`deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1
2024/10/13 02:56:43 /home/fedora/Desktop/thatsky-telegram-bot/bot.go:86
[29.535ms] [rows:1] INSERT INTO `messages` (`created_at`,`updated_at`,`deleted_at`,`chat_id`,`user_id`,`username`,`user_role`,`text`,`timestamp`,`is_user`) VALUES ("2024-10-13 02:56:43.154","2024-10-13 02:56:43.154",NULL,1404948412,0,"Assistant","assistant","I'm afraid I don't have any system commands like ""/start"". I'm an AI assistant named Claude having a conversation.","2024-10-13 02:56:43.153",false) RETURNING `id`

32
clock.go Normal file
View File

@@ -0,0 +1,32 @@
// clock.go
package main
import "time"
// Clock is an interface to abstract time-related functions.
type Clock interface {
Now() time.Time
}
// RealClock implements Clock using the actual time.
type RealClock struct{}
// Now returns the current local time.
func (RealClock) Now() time.Time {
return time.Now()
}
// MockClock implements Clock for testing purposes.
type MockClock struct {
currentTime time.Time
}
// Now returns the mocked current time.
func (mc *MockClock) Now() time.Time {
return mc.currentTime
}
// Advance moves the current time forward by the specified duration.
func (mc *MockClock) Advance(d time.Duration) {
mc.currentTime = mc.currentTime.Add(d)
}

View File

@@ -2,25 +2,103 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"path/filepath"
) )
type Config struct { type BotConfig struct {
ID string `json:"id"` // Unique identifier for the bot
MemorySize int `json:"memory_size"` MemorySize int `json:"memory_size"`
MessagePerHour int `json:"messages_per_hour"` MessagePerHour int `json:"messages_per_hour"`
MessagePerDay int `json:"messages_per_day"` MessagePerDay int `json:"messages_per_day"`
TempBanDuration string `json:"temp_ban_duration"` TempBanDuration string `json:"temp_ban_duration"`
SystemPrompts map[string]string `json:"system_prompts"`
TelegramToken string `json:"telegram_token"` // Telegram Bot Token
} }
func loadConfig(filename string) (Config, error) { func loadAllConfigs(dir string) ([]BotConfig, error) {
var config Config var configs []BotConfig
ids := make(map[string]bool)
tokens := make(map[string]bool)
files, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("failed to read config directory: %w", err)
}
for _, file := range files {
if filepath.Ext(file.Name()) == ".json" {
configPath := filepath.Join(dir, file.Name())
config, err := loadConfig(configPath)
if err != nil {
return nil, fmt.Errorf("failed to load config %s: %w", configPath, err)
}
// Validate that ID is present
if config.ID == "" {
return nil, fmt.Errorf("config %s is missing 'id' field", configPath)
}
// Check for unique ID
if _, exists := ids[config.ID]; exists {
return nil, fmt.Errorf("duplicate bot id '%s' found in %s", config.ID, configPath)
}
ids[config.ID] = true
// Validate Telegram Token
if config.TelegramToken == "" {
return nil, fmt.Errorf("config %s is missing 'telegram_token' field", configPath)
}
// Check for unique Telegram Token
if _, exists := tokens[config.TelegramToken]; exists {
return nil, fmt.Errorf("duplicate telegram_token '%s' found in %s", config.TelegramToken, configPath)
}
tokens[config.TelegramToken] = true
configs = append(configs, config)
}
}
return configs, nil
}
func loadConfig(filename string) (BotConfig, error) {
var config BotConfig
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
return config, err return config, fmt.Errorf("failed to open config file %s: %w", filename, err)
} }
defer file.Close() defer file.Close()
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
err = decoder.Decode(&config) if err := decoder.Decode(&config); err != nil {
return config, err return config, fmt.Errorf("failed to decode JSON from %s: %w", filename, err)
}
// Optionally override telegram_token with environment variable if set
// Uncomment the following lines if you choose to use environment variables for tokens
/*
if envToken := os.Getenv(fmt.Sprintf("TELEGRAM_TOKEN_%s", config.ID)); envToken != "" {
config.TelegramToken = envToken
}
*/
return config, nil
}
func (c *BotConfig) Reload(filename string) error {
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("failed to open config file %s: %w", filename, err)
}
defer file.Close()
decoder := json.NewDecoder(file)
if err := decoder.Decode(c); err != nil {
return fmt.Errorf("failed to decode JSON from %s: %w", filename, err)
}
return nil
} }

View File

@@ -1,6 +0,0 @@
{
"memory_size": 10,
"messages_per_hour": 20,
"messages_per_day": 100,
"temp_ban_duration": "24h"
}

14
config/default.json Normal file
View File

@@ -0,0 +1,14 @@
{
"id": "default_bot",
"telegram_token": "YOUR_TELEGRAM_BOT_TOKEN",
"memory_size": 10,
"messages_per_hour": 20,
"messages_per_day": 100,
"temp_ban_duration": "24h",
"system_prompts": {
"default": "You are a helpful assistant.",
"custom_instructions": "Please follow these guidelines:\n- Your name is Atom.\n- If a user asks about buying apples, inform them that we don't sell apples.\n- When asked for a joke, tell a clean, family-friendly joke about programming or technology.\n- If someone inquires about our services, explain that we offer AI-powered chatbot solutions.\n- For any questions about pricing, direct users to contact our sales team at sales@example.com.\n- If asked about your capabilities, be honest about what you can and cannot do.\nAlways maintain a friendly and professional tone.",
"continue_conversation": "Continuing our conversation. Remember previous context if relevant.",
"avoid_sensitive": "Avoid discussing sensitive topics or providing harmful information."
}
}

View File

@@ -27,7 +27,7 @@ func initDB() (*gorm.DB, error) {
return nil, fmt.Errorf("failed to connect to database: %w", err) return nil, fmt.Errorf("failed to connect to database: %w", err)
} }
err = db.AutoMigrate(&Message{}, &User{}, &Role{}) err = db.AutoMigrate(&BotModel{}, &ConfigModel{}, &Message{}, &User{}, &Role{})
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to migrate database schema: %w", err) return nil, fmt.Errorf("failed to migrate database schema: %w", err)
} }

View File

@@ -3,9 +3,11 @@ package main
import ( import (
"context" "context"
"log" "log"
"strings"
"github.com/go-telegram/bot" "github.com/go-telegram/bot"
"github.com/go-telegram/bot/models" "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) { 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 chatID := update.Message.Chat.ID
userID := update.Message.From.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) { if !b.checkRateLimits(userID) {
b.sendRateLimitExceededMessage(ctx, chatID) b.sendRateLimitExceededMessage(ctx, chatID)
return 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 := b.createMessage(chatID, userID, username, user.Role.Name, text, true)
userMessage.UserRole = string(anthropic.RoleUser) // Convert to string
b.storeMessage(userMessage) b.storeMessage(userMessage)
chatMemory := b.getOrCreateChatMemory(chatID) 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) 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.storeMessage(assistantMessage)
b.addMessageToChatMemory(chatMemory, assistantMessage) b.addMessageToChatMemory(chatMemory, assistantMessage)
} }
func (b *Bot) sendRateLimitExceededMessage(ctx context.Context, chatID int64) { func (b *Bot) sendRateLimitExceededMessage(ctx context.Context, chatID int64) {
_, err := b.tgBot.SendMessage(ctx, &bot.SendMessageParams{ b.sendResponse(ctx, chatID, "Rate limit exceeded. Please try again later.")
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)
}
} }

39
main.go
View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"sync"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
@@ -32,25 +33,41 @@ func main() {
log.Fatalf("Error initializing database: %v", err) log.Fatalf("Error initializing database: %v", err)
} }
// Load configuration // Load all bot configurations
config, err := loadConfig("config.json") configs, err := loadAllConfigs("config")
if err != nil { if err != nil {
log.Fatalf("Error loading configuration: %v", err) log.Fatalf("Error loading configurations: %v", err)
} }
// Create Bot instance // Create a WaitGroup to manage goroutines
b, err := NewBot(db, config) var wg sync.WaitGroup
if err != nil {
log.Fatalf("Error creating bot: %v", err)
}
// Set up context with cancellation // Set up context with cancellation
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel() defer cancel()
// Initialize and start each bot
for _, config := range configs {
wg.Add(1)
go func(cfg BotConfig) {
defer wg.Done()
// Create Bot instance with RealClock
realClock := RealClock{}
bot, err := NewBot(db, cfg, realClock)
if err != nil {
log.Printf("Error creating bot %s: %v", cfg.ID, err)
return
}
// Start the bot // Start the bot
log.Println("Starting bot...") log.Printf("Starting bot %s...", cfg.ID)
b.Start(ctx) bot.Start(ctx)
}(config)
}
// Wait for all bots to finish
wg.Wait()
} }
func initLogger() (*os.File, error) { func initLogger() (*os.File, error) {
@@ -64,7 +81,7 @@ func initLogger() (*os.File, error) {
} }
func checkRequiredEnvVars() { func checkRequiredEnvVars() {
requiredEnvVars := []string{"TELEGRAM_BOT_TOKEN", "ANTHROPIC_API_KEY"} requiredEnvVars := []string{"ANTHROPIC_API_KEY"}
for _, envVar := range requiredEnvVars { for _, envVar := range requiredEnvVars {
if os.Getenv(envVar) == "" { if os.Getenv(envVar) == "" {
log.Fatalf("%s environment variable is not set", envVar) log.Fatalf("%s environment variable is not set", envVar)

View File

@@ -6,8 +6,29 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
type BotModel struct {
gorm.Model
Identifier string `gorm:"uniqueIndex"` // Renamed from ID to Identifier
Name string
Configs []ConfigModel `gorm:"foreignKey:BotID;constraint:OnDelete:CASCADE"`
Users []User `gorm:"foreignKey:BotID;constraint:OnDelete:CASCADE"` // Added foreign key
Messages []Message `gorm:"foreignKey:BotID;constraint:OnDelete:CASCADE"`
}
type ConfigModel struct {
gorm.Model
BotID uint `gorm:"index"`
MemorySize int `json:"memory_size"`
MessagePerHour int `json:"messages_per_hour"`
MessagePerDay int `json:"messages_per_day"`
TempBanDuration string `json:"temp_ban_duration"`
SystemPrompts string `json:"system_prompts"` // Consider JSON string or separate table
TelegramToken string `json:"telegram_token"`
}
type Message struct { type Message struct {
gorm.Model gorm.Model
BotID uint
ChatID int64 ChatID int64
UserID int64 UserID int64
Username string Username string
@@ -29,7 +50,8 @@ type Role struct {
type User struct { type User struct {
gorm.Model gorm.Model
TelegramID int64 `gorm:"uniqueIndex"` BotID uint `gorm:"index"` // Added foreign key to BotModel
TelegramID int64 `gorm:"uniqueIndex"` // Consider composite unique index if TelegramID is unique per Bot
Username string Username string
RoleID uint RoleID uint
Role Role `gorm:"foreignKey:RoleID"` Role Role `gorm:"foreignKey:RoleID"`

View File

@@ -9,8 +9,10 @@ import (
type userLimiter struct { type userLimiter struct {
hourlyLimiter *rate.Limiter hourlyLimiter *rate.Limiter
dailyLimiter *rate.Limiter dailyLimiter *rate.Limiter
lastReset time.Time lastHourlyReset time.Time
lastDailyReset time.Time
banUntil time.Time banUntil time.Time
clock Clock
} }
func (b *Bot) checkRateLimits(userID int64) bool { func (b *Bot) checkRateLimits(userID int64) bool {
@@ -22,24 +24,39 @@ func (b *Bot) checkRateLimits(userID int64) bool {
limiter = &userLimiter{ limiter = &userLimiter{
hourlyLimiter: rate.NewLimiter(rate.Every(time.Hour/time.Duration(b.config.MessagePerHour)), b.config.MessagePerHour), hourlyLimiter: rate.NewLimiter(rate.Every(time.Hour/time.Duration(b.config.MessagePerHour)), b.config.MessagePerHour),
dailyLimiter: rate.NewLimiter(rate.Every(24*time.Hour/time.Duration(b.config.MessagePerDay)), b.config.MessagePerDay), dailyLimiter: rate.NewLimiter(rate.Every(24*time.Hour/time.Duration(b.config.MessagePerDay)), b.config.MessagePerDay),
lastReset: time.Now(), lastHourlyReset: b.clock.Now(),
lastDailyReset: b.clock.Now(),
clock: b.clock,
} }
b.userLimiters[userID] = limiter b.userLimiters[userID] = limiter
} }
now := time.Now() now := limiter.clock.Now()
// Check if the user is currently banned
if now.Before(limiter.banUntil) { if now.Before(limiter.banUntil) {
return false return false
} }
if now.Sub(limiter.lastReset) >= 24*time.Hour { // Reset hourly limiter if an hour has passed since the last reset
limiter.dailyLimiter = rate.NewLimiter(rate.Every(24*time.Hour/time.Duration(b.config.MessagePerDay)), b.config.MessagePerDay) if now.Sub(limiter.lastHourlyReset) >= time.Hour {
limiter.lastReset = now limiter.hourlyLimiter = rate.NewLimiter(rate.Every(time.Hour/time.Duration(b.config.MessagePerHour)), b.config.MessagePerHour)
limiter.lastHourlyReset = now
} }
// Reset daily limiter if 24 hours have passed since the last reset
if now.Sub(limiter.lastDailyReset) >= 24*time.Hour {
limiter.dailyLimiter = rate.NewLimiter(rate.Every(24*time.Hour/time.Duration(b.config.MessagePerDay)), b.config.MessagePerDay)
limiter.lastDailyReset = now
}
// Check if the message exceeds rate limits
if !limiter.hourlyLimiter.Allow() || !limiter.dailyLimiter.Allow() { if !limiter.hourlyLimiter.Allow() || !limiter.dailyLimiter.Allow() {
banDuration, _ := time.ParseDuration(b.config.TempBanDuration) banDuration, err := time.ParseDuration(b.config.TempBanDuration)
if err != nil {
// If parsing fails, default to a 24-hour ban
banDuration = 24 * time.Hour
}
limiter.banUntil = now.Add(banDuration) limiter.banUntil = now.Add(banDuration)
return false return false
} }

84
rate_limiter_test.go Normal file
View File

@@ -0,0 +1,84 @@
package main
import (
"testing"
"time"
)
// TestCheckRateLimits tests the checkRateLimits method of the Bot.
// It verifies that users are allowed or denied based on their message rates.
func TestCheckRateLimits(t *testing.T) {
// Create a mock clock starting at a fixed time
mockClock := &MockClock{
currentTime: time.Date(2023, 10, 1, 0, 0, 0, 0, time.UTC),
}
// Create a mock configuration with reduced timeframes for testing
config := BotConfig{
ID: "bot1",
MemorySize: 10,
MessagePerHour: 5, // Allow 5 messages per hour
MessagePerDay: 10, // Allow 10 messages per day
TempBanDuration: "1m", // Temporary ban duration of 1 minute for testing
SystemPrompts: make(map[string]string),
TelegramToken: "YOUR_TELEGRAM_BOT_TOKEN",
}
// Initialize the Bot with mock data and MockClock
bot := &Bot{
config: config,
userLimiters: make(map[int64]*userLimiter),
clock: mockClock,
}
userID := int64(12345)
// Helper function to simulate message sending
sendMessage := func() bool {
return bot.checkRateLimits(userID)
}
// Send 5 messages within the hourly limit
for i := 0; i < config.MessagePerHour; i++ {
if !sendMessage() {
t.Errorf("Expected message %d to be allowed", i+1)
}
}
// 6th message should exceed the hourly limit and trigger a ban
if sendMessage() {
t.Errorf("Expected message to be denied due to hourly limit exceeded")
}
// Attempt to send another message immediately, should still be banned
if sendMessage() {
t.Errorf("Expected message to be denied while user is banned")
}
// Fast-forward time by TempBanDuration to lift the ban
mockClock.Advance(time.Minute) // Banned for 1 minute
// Advance time to allow hourly limiter to replenish
mockClock.Advance(time.Hour) // Advance by 1 hour
// Send another message, should be allowed now
if !sendMessage() {
t.Errorf("Expected message to be allowed after ban duration")
}
// Send additional messages to reach the daily limit
for i := 0; i < config.MessagePerDay-config.MessagePerHour-1; i++ {
if !sendMessage() {
t.Errorf("Expected message %d to be allowed towards daily limit", i+1)
}
}
// Attempt to exceed the daily limit
if sendMessage() {
t.Errorf("Expected message to be denied due to daily limit exceeded")
}
}
// To ensure thread safety and avoid race conditions during testing,
// you can run the tests with the `-race` flag:
// go test -race -v