in progress
This commit is contained in:
62
telegram.go
Normal file
62
telegram.go
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user