diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fb7f0c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test_data \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4cfd846 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module git.gm6.ru/icewind/notify + +go 1.25.1 + +require ( + github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + gopkg.in/yaml.v2 v2.4.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c80f9bc --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +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= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/telegram.go b/telegram.go new file mode 100644 index 0000000..ff5e275 --- /dev/null +++ b/telegram.go @@ -0,0 +1,62 @@ +package notify + +import ( + "context" + "log" + "net" + "net/http" + "time" + + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" +) + +type Bot struct { + api *tgbotapi.BotAPI +} + +func NewTelegram(token string, disableIPV6 bool) (*Bot, error) { + dialer := &net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + } + + transport := &http.Transport{ + // Заменяем стандартный DialContext + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + // "tcp4" отключает IPv6 + return dialer.DialContext(ctx, "tcp4", addr) + }, + TLSHandshakeTimeout: 10 * time.Second, + } + + client := &http.Client{ + Transport: transport, + Timeout: 60 * time.Second, + } + + if disableIPV6 { + client.Transport = &http.Transport{ + // Заменяем стандартный DialContext + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { + // "tcp4" отключает IPv6 + return dialer.DialContext(ctx, "tcp4", addr) + }, + TLSHandshakeTimeout: 10 * time.Second, + } + } + + botApi, err := tgbotapi.NewBotAPIWithClient(token, tgbotapi.APIEndpoint, client) + if err != nil { + log.Fatalf("Telegram bot api error: %v", err) + } + bot := &Bot{ + api: botApi, + } + return bot, nil +} + +func (s *Bot) SendTextMessage(chatID int64, text string) (tgbotapi.Message, error) { + message := tgbotapi.NewMessage(chatID, text) + message.ParseMode = "Markdown" + return s.api.Send(message) +} diff --git a/telegram_test.go b/telegram_test.go new file mode 100644 index 0000000..43c98dd --- /dev/null +++ b/telegram_test.go @@ -0,0 +1,37 @@ +package notify_test + +import ( + "log" + "os" + "testing" + + "git.gm6.ru/icewind/notify" + "gopkg.in/yaml.v2" +) + +type ConfigTelegram struct { + GroupID int64 `yaml:"group_id"` + Token string `yaml:"token"` +} + +func TestMessage(t *testing.T) { + cData, err := os.ReadFile("test_data/telegram.yaml") + if err != nil { + t.Fatal(err) + } + var conf ConfigTelegram + if err = yaml.Unmarshal(cData, &conf); err != nil { + t.Fatal(err) + } + + log.Println(conf) + bot, err := notify.NewTelegram(conf.Token, true) + if err != nil { + t.Fatal(err) + } + m, err := bot.SendTextMessage(conf.GroupID, "Привет ))") + if err != nil { + t.Fatal(err) + } + t.Log(m) +}