tss/telegram/telegram.go
2022-06-21 21:32:16 -04:00

108 lines
2.3 KiB
Go

package telegram
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/go-logr/logr"
"go.balki.me/tss/limiter"
)
var log = logr.Discard()
func SetLogger(l logr.Logger) {
log = l
}
type Sender interface {
SendLink(link, channel, rhash, title string) error
}
type telegramSender struct {
client *http.Client
authToken string
rateLimiterPerMin limiter.Limiter
rateLimiterPerSec limiter.Limiter
}
func (ts *telegramSender) SendLink(link, channel, rhash, title string) error {
log := log.WithValues("link", link, "channel", channel)
if title == "" {
title = "Link"
}
msg := struct {
ChatID string `json:"chat_id"`
Text string `json:"text"`
ParseMode string `json:"parse_mode"`
}{
ChatID: channel,
Text: fmt.Sprintf(`<a href="%s">➢</a> <a href="%s">%s</a>`, genIVLink(link, rhash), link, title),
ParseMode: "HTML",
}
data, err := json.Marshal(msg)
if err != nil {
return err
}
apiURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", ts.authToken)
ts.rateLimiterPerMin.Wait()
ts.rateLimiterPerSec.Wait()
res, err := ts.client.Post(apiURL, "application/json", bytes.NewReader(data))
if err != nil {
return err
}
defer func() {
err := res.Body.Close()
if err != nil {
log.Error(err, "res.Body.Close() failed")
return
}
}()
responseText, err := io.ReadAll(res.Body)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
log.Error(nil, "telegram send failed", "status", res.Status, "request", data, "response", responseText)
return errors.New("telegram send failed")
}
log.Info("sent message on telegram", "response", responseText)
return nil
}
func NewSender(transport http.RoundTripper, authToken string) Sender {
return &telegramSender{
client: &http.Client{Transport: transport},
authToken: authToken,
// 20 requests per min with some buffer
rateLimiterPerMin: limiter.NewLimiter((60+15)*time.Second, 20),
// 1 msg per sec with some buffer
rateLimiterPerSec: limiter.NewLimiter(1100*time.Millisecond, 1),
}
}
func genIVLink(link, rhash string) string {
if rhash == "" {
return link
}
query := url.Values{}
query.Set("url", link)
query.Set("rhash", rhash)
u := url.URL{
Scheme: "https",
Host: "t.me",
Path: "iv",
RawQuery: query.Encode(),
}
return u.String()
}