mirror of
https://github.com/HugeFrog24/go-telegram-bot.git
synced 2026-03-02 08:24:34 +00:00
MVP
This commit is contained in:
28
bot.go
28
bot.go
@@ -209,6 +209,7 @@ func (b *Bot) sendResponse(ctx context.Context, chatID int64, text string, busin
|
|||||||
params := &bot.SendMessageParams{
|
params := &bot.SendMessageParams{
|
||||||
ChatID: chatID,
|
ChatID: chatID,
|
||||||
Text: text,
|
Text: text,
|
||||||
|
ParseMode: models.ParseModeMarkdown,
|
||||||
}
|
}
|
||||||
|
|
||||||
if businessConnectionID != "" {
|
if businessConnectionID != "" {
|
||||||
@@ -225,7 +226,7 @@ func (b *Bot) sendResponse(ctx context.Context, chatID int64, text string, busin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sendStats sends the bot statistics to the specified chat.
|
// sendStats sends the bot statistics to the specified chat.
|
||||||
func (b *Bot) sendStats(ctx context.Context, chatID int64, businessConnectionID string) {
|
func (b *Bot) sendStats(ctx context.Context, chatID int64, userID int64, username string, businessConnectionID string) {
|
||||||
totalUsers, totalMessages, err := b.getStats()
|
totalUsers, totalMessages, err := b.getStats()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error fetching stats: %v\n", err)
|
fmt.Printf("Error fetching stats: %v\n", err)
|
||||||
@@ -233,8 +234,29 @@ func (b *Bot) sendStats(ctx context.Context, chatID int64, businessConnectionID
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
statsMessage := fmt.Sprintf("📊 **Bot Statistics:**\n\n- Total Users: %d\n- Total Messages: %d", totalUsers, totalMessages)
|
// Do NOT manually escape hyphens here
|
||||||
b.sendResponse(ctx, chatID, statsMessage, businessConnectionID)
|
statsMessage := fmt.Sprintf(
|
||||||
|
"📊 *Bot Statistics:*\n\n"+
|
||||||
|
"\\- Total Users: %d\n"+
|
||||||
|
"\\- Total Messages: %d",
|
||||||
|
totalUsers,
|
||||||
|
totalMessages,
|
||||||
|
)
|
||||||
|
|
||||||
|
// Store the user's /stats command
|
||||||
|
userMessage := b.createMessage(chatID, userID, username, "user", "/stats", true)
|
||||||
|
if err := b.storeMessage(userMessage); err != nil {
|
||||||
|
log.Printf("Error storing user message: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send and store the bot's response
|
||||||
|
if err := b.sendResponse(ctx, chatID, statsMessage, businessConnectionID); err != nil {
|
||||||
|
log.Printf("Error sending stats message: %v", err)
|
||||||
|
}
|
||||||
|
assistantMessage := b.createMessage(chatID, 0, "", "assistant", statsMessage, false)
|
||||||
|
if err := b.storeMessage(assistantMessage); err != nil {
|
||||||
|
log.Printf("Error storing assistant message: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getStats retrieves the total number of users and messages from the database.
|
// getStats retrieves the total number of users and messages from the database.
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type BotConfig struct {
|
|||||||
TempBanDuration string `json:"temp_ban_duration"`
|
TempBanDuration string `json:"temp_ban_duration"`
|
||||||
Model anthropic.Model `json:"model"` // Changed from string to anthropic.Model
|
Model anthropic.Model `json:"model"` // Changed from string to anthropic.Model
|
||||||
SystemPrompts map[string]string `json:"system_prompts"`
|
SystemPrompts map[string]string `json:"system_prompts"`
|
||||||
|
Active bool `json:"active"` // New field to control bot activity
|
||||||
}
|
}
|
||||||
|
|
||||||
// Custom unmarshalling to handle anthropic.Model
|
// Custom unmarshalling to handle anthropic.Model
|
||||||
@@ -56,6 +57,12 @@ func loadAllConfigs(dir string) ([]BotConfig, error) {
|
|||||||
return nil, fmt.Errorf("failed to load config %s: %w", configPath, err)
|
return nil, fmt.Errorf("failed to load config %s: %w", configPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip inactive bots
|
||||||
|
if !config.Active {
|
||||||
|
fmt.Printf("Skipping inactive bot: %s\n", config.ID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Validate that ID is present
|
// Validate that ID is present
|
||||||
if config.ID == "" {
|
if config.ID == "" {
|
||||||
return nil, fmt.Errorf("config %s is missing 'id' field", configPath)
|
return nil, fmt.Errorf("config %s is missing 'id' field", configPath)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"id": "default_bot",
|
"id": "default_bot",
|
||||||
|
"active": false,
|
||||||
"telegram_token": "YOUR_TELEGRAM_BOT_TOKEN",
|
"telegram_token": "YOUR_TELEGRAM_BOT_TOKEN",
|
||||||
"memory_size": 10,
|
"memory_size": 10,
|
||||||
"messages_per_hour": 20,
|
"messages_per_hour": 20,
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module github.com/HugeFrog24/thatsky-telegram-bot
|
module github.com/HugeFrog24/go-telegram-bot
|
||||||
|
|
||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ func (b *Bot) handleUpdate(ctx context.Context, tgBot *bot.Bot, update *models.U
|
|||||||
command := strings.TrimSpace(message.Text[entity.Offset : entity.Offset+entity.Length])
|
command := strings.TrimSpace(message.Text[entity.Offset : entity.Offset+entity.Length])
|
||||||
switch command {
|
switch command {
|
||||||
case "/stats":
|
case "/stats":
|
||||||
b.sendStats(ctx, chatID, businessConnectionID)
|
b.sendStats(ctx, chatID, userID, message.From.Username, businessConnectionID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ type ConfigModel struct {
|
|||||||
TempBanDuration string `json:"temp_ban_duration"`
|
TempBanDuration string `json:"temp_ban_duration"`
|
||||||
SystemPrompts string `json:"system_prompts"` // Consider JSON string or separate table
|
SystemPrompts string `json:"system_prompts"` // Consider JSON string or separate table
|
||||||
TelegramToken string `json:"telegram_token"`
|
TelegramToken string `json:"telegram_token"`
|
||||||
|
Active bool `json:"active"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user