//go:build plus package mediasenders import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/iwind/TeaGo/types" "net/http" "net/url" "time" ) // TelegramMedia Telegram媒介 type TelegramMedia struct { Token string `yaml:"token" json:"token"` ProxyURL string `yaml:"proxyURL" json:"proxyURL"` } // NewTelegramMedia 获取新对象 func NewTelegramMedia() *TelegramMedia { return &TelegramMedia{} } // Send 发送消息 func (this *TelegramMedia) Send(user string, subject string, body string, productName string, datetime string) (respBytes []byte, err error) { var proxyFunc func(req *http.Request) (*url.URL, error) if len(this.ProxyURL) > 0 { proxyFunc = func(req *http.Request) (*url.URL, error) { return url.Parse(this.ProxyURL) } } var httpClient = &http.Client{ Timeout: 10 * time.Second, Transport: &http.Transport{ MaxIdleConns: 4096, MaxIdleConnsPerHost: 32, MaxConnsPerHost: 32, IdleConnTimeout: 2 * time.Minute, ExpectContinueTimeout: 1 * time.Second, TLSHandshakeTimeout: 0, Proxy: proxyFunc, }, } defer httpClient.CloseIdleConnections() bot, err := tgbotapi.NewBotAPIWithClient(this.Token, httpClient) if err != nil { return nil, err } bot.Client = httpClient var text string if len(productName) > 0 { text += productName if len(datetime) > 0 { text += " AT " + datetime } text += "\n----\n" } else if len(datetime) > 0 { text += "AT " + datetime text += "\n----\n" } text += subject + "\n" + body var msg = tgbotapi.NewMessage(types.Int64(user), text) _, err = bot.Send(msg) return nil, err } // RequireUser 是否需要用户标识 func (this *TelegramMedia) RequireUser() bool { return true }