telegram_bot/main.go

76 lines
1.8 KiB
Go

package main
import (
"fmt"
"log"
"strings"
"strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"telegram_bot/commands"
"telegram_bot/utils"
)
func main() {
// Load configuration
config, err := utils.LoadConfig("config.json")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// Initialize the Telegram bot
bot, err := tgbotapi.NewBotAPI(config.BotToken)
if err != nil {
log.Fatalf("Failed to create bot: %v", err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
// Set up an update config to filter messages from the chat
updateConfig := tgbotapi.NewUpdate(0)
updateConfig.Timeout = 60
updates := bot.GetUpdatesChan(updateConfig)
// Convert Chat ID from String to Int64
chatID, err := strconv.ParseInt(config.ChatID, 10, 64)
if err != nil {
log.Fatalf("Rip conversion: %v", err)
}
// Listen for updates
for update := range updates {
if update.Message != nil { // If we got a message
if update.Message.Chat.ID != chatID {
continue // Ignore messages from other chats
}
text := update.Message.Text
switch {
case strings.HasPrefix(text, "/geoip"):
args := strings.Fields(text)
if len(args) != 2 {
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Usage: /geoip <IP>"))
continue
}
ipAddress := args[1]
response, err := commands.HandleGeoIPCommand(ipAddress, config.GeoIP_City, config.GeoIP_ASN)
if err != nil {
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("Error: %v", err)))
} else {
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, response))
}
case strings.HasPrefix(text, "/suamae"):
response := commands.HandleSuaMae()
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, response))
default:
fmt.Printf("%s.\n", text)
}
}
}
}