Initial commit
This commit is contained in:
commit
e6de45c095
|
@ -0,0 +1,3 @@
|
|||
*.json
|
||||
*.mmdb
|
||||
telegram_bot
|
|
@ -0,0 +1,27 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
"net"
|
||||
)
|
||||
|
||||
func HandleGeoIPCommand(ipAddress string, dbPath string) (string, error) {
|
||||
db, err := geoip2.Open(dbPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error opening GeoIP database: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
ip := net.ParseIP(ipAddress)
|
||||
record, err := db.City(ip)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error looking up IP address: %v", err)
|
||||
}
|
||||
|
||||
if record.Country.IsoCode != "" {
|
||||
return fmt.Sprintf("IP: %s, Country: %s", ipAddress, record.Country.IsoCode), nil
|
||||
}
|
||||
|
||||
return "Country not found", nil
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"bot_token": "ID:Secret",
|
||||
"chat_id": "USER_OR_CHAT_ID",
|
||||
"geoip_city": "geolite2-city.mmdb",
|
||||
"geoip_asn": "geolite2-asn.mmdb"
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
module telegram_bot
|
||||
|
||||
go 1.23.0
|
||||
|
||||
require (
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
|
||||
github.com/oschwald/geoip2-golang v1.11.0 // indirect
|
||||
github.com/oschwald/maxminddb-golang v1.13.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
)
|
|
@ -0,0 +1,8 @@
|
|||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
|
||||
github.com/oschwald/geoip2-golang v1.11.0 h1:hNENhCn1Uyzhf9PTmquXENiWS6AlxAEnBII6r8krA3w=
|
||||
github.com/oschwald/geoip2-golang v1.11.0/go.mod h1:P9zG+54KPEFOliZ29i7SeYZ/GM6tfEL+rgSn03hYuUo=
|
||||
github.com/oschwald/maxminddb-golang v1.13.0 h1:R8xBorY71s84yO06NgTmQvqvTvlS/bnYZrrWX1MElnU=
|
||||
github.com/oschwald/maxminddb-golang v1.13.0/go.mod h1:BU0z8BfFVhi1LQaonTwwGQlsHUEu9pWNdMfmq4ztm0o=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
|
@ -0,0 +1,66 @@
|
|||
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)
|
||||
|
||||
// negrice
|
||||
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
|
||||
if 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)
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
BotToken string `json:"bot_token"`
|
||||
ChatID string `json:"chat_id"`
|
||||
GeoIP_City string `json:"geoip_city"`
|
||||
GeoIP_ASN string `json:"geoip_asn"`
|
||||
}
|
||||
|
||||
func LoadConfig(file string) (Config, error) {
|
||||
var config Config
|
||||
|
||||
configFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return config, fmt.Errorf("error opening config file: %v", err)
|
||||
}
|
||||
defer configFile.Close()
|
||||
|
||||
err = json.NewDecoder(configFile).Decode(&config)
|
||||
if err != nil {
|
||||
return config, fmt.Errorf("error decoding config file: %v", err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
Loading…
Reference in New Issue