Files
notify/telegram.go
Maksimov V Vladimir 8331f1a95b исправление багов, документация
- telegram: флаг disableIPV6 не работал — IPv6 был отключён всегда
- telegram: log.Fatalf заменён на возврат ошибки
- tools: BuildTelegramTable не падает при строках разной длины
- README.md с примерами использования
- .gitignore обновлён
2026-04-01 16:45:17 +03:00

114 lines
2.9 KiB
Go

package notify
import (
"context"
"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{
TLSHandshakeTimeout: 10 * time.Second,
}
if disableIPV6 {
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
// "tcp4" отключает IPv6
return dialer.DialContext(ctx, "tcp4", addr)
}
}
client := &http.Client{
Transport: transport,
Timeout: 60 * time.Second,
}
botApi, err := tgbotapi.NewBotAPIWithClient(token, tgbotapi.APIEndpoint, client)
if err != nil {
return nil, err
}
bot := &Bot{
api: botApi,
}
return bot, nil
}
func (s *Bot) GetAPI() *tgbotapi.BotAPI {
return s.api
}
func (s *Bot) SendTextMessage(chatID int64, text string) (tgbotapi.Message, error) {
message := tgbotapi.NewMessage(chatID, text)
message.ParseMode = "Markdown"
return s.api.Send(message)
}
func (s *Bot) SendHTMLMessage(chatID int64, text string) (tgbotapi.Message, error) {
message := tgbotapi.NewMessage(chatID, text)
message.ParseMode = "HTML"
return s.api.Send(message)
}
func (s *Bot) SendPhotoFromFile(chatID int64, filePath string, caption string) (tgbotapi.Message, error) {
photo := tgbotapi.NewPhoto(chatID, tgbotapi.FilePath(filePath))
photo.Caption = caption
photo.ParseMode = "Markdown" // или "HTML"
return s.api.Send(photo)
}
func (s *Bot) SendPhotoFromURL(chatID int64, fileURL string, caption string) (tgbotapi.Message, error) {
photo := tgbotapi.NewPhoto(chatID, tgbotapi.FileURL(fileURL))
photo.Caption = caption
photo.ParseMode = "Markdown"
return s.api.Send(photo)
}
func (s *Bot) SendPhotoFromBytes(chatID int64, data []byte, filename string, caption string) (tgbotapi.Message, error) {
file := tgbotapi.FileBytes{
Name: filename,
Bytes: data,
}
photo := tgbotapi.NewPhoto(chatID, file)
photo.Caption = caption
photo.ParseMode = "Markdown"
return s.api.Send(photo)
}
func (s *Bot) SendDocumentFromFile(chatID int64, filePath string, caption string) (tgbotapi.Message, error) {
doc := tgbotapi.NewDocument(chatID, tgbotapi.FilePath(filePath))
doc.Caption = caption
doc.ParseMode = "Markdown"
return s.api.Send(doc)
}
func (s *Bot) SendDocumentFromURL(chatID int64, fileURL string, caption string) (tgbotapi.Message, error) {
doc := tgbotapi.NewDocument(chatID, tgbotapi.FileURL(fileURL))
doc.Caption = caption
doc.ParseMode = "Markdown"
return s.api.Send(doc)
}
func (s *Bot) SendDocumentFromBytes(chatID int64, data []byte, filename string, caption string) (tgbotapi.Message, error) {
file := tgbotapi.FileBytes{
Name: filename,
Bytes: data,
}
doc := tgbotapi.NewDocument(chatID, file)
doc.Caption = caption
doc.ParseMode = "Markdown"
return s.api.Send(doc)
}