2022-05-01 23:28:54 -04:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-05-02 13:24:29 -04:00
|
|
|
"time"
|
2022-05-01 23:28:54 -04:00
|
|
|
|
2022-06-17 18:23:06 -04:00
|
|
|
"github.com/go-logr/logr"
|
2022-05-31 11:49:07 -04:00
|
|
|
"go.balki.me/tss/limiter"
|
2022-05-01 23:28:54 -04:00
|
|
|
)
|
|
|
|
|
2022-06-17 18:23:06 -04:00
|
|
|
var log = logr.Discard()
|
|
|
|
|
2022-06-20 15:49:55 -04:00
|
|
|
func SetLogger(l logr.Logger) {
|
|
|
|
log = l
|
2022-06-17 18:23:06 -04:00
|
|
|
}
|
|
|
|
|
2022-05-01 23:28:54 -04:00
|
|
|
type TelegramSender interface {
|
2022-05-07 21:01:14 -04:00
|
|
|
SendLink(link, channel, rhash, title string) error
|
2022-05-01 23:28:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type telegramSender struct {
|
2022-05-02 13:24:29 -04:00
|
|
|
client *http.Client
|
|
|
|
authToken string
|
2022-05-31 11:49:07 -04:00
|
|
|
rateLimiterPerMin limiter.Limiter
|
|
|
|
rateLimiterPerSec limiter.Limiter
|
2022-05-01 23:28:54 -04:00
|
|
|
}
|
|
|
|
|
2022-05-07 21:01:14 -04:00
|
|
|
func (ts *telegramSender) SendLink(link, channel, rhash, title string) error {
|
2022-06-17 18:23:06 -04:00
|
|
|
log := log.WithValues("link", link, "channel", channel)
|
2022-05-07 21:01:14 -04:00
|
|
|
if title == "" {
|
|
|
|
title = "Link"
|
|
|
|
}
|
2022-05-01 23:28:54 -04:00
|
|
|
msg := struct {
|
2022-05-02 00:36:27 -04:00
|
|
|
ChatID string `json:"chat_id"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
ParseMode string `json:"parse_mode"`
|
2022-05-01 23:28:54 -04:00
|
|
|
}{
|
2022-05-02 00:36:27 -04:00
|
|
|
ChatID: channel,
|
2022-05-07 21:01:14 -04:00
|
|
|
Text: fmt.Sprintf(`<a href="%s">➢</a> <a href="%s">%s</a>`, genIVLink(link, rhash), link, title),
|
2022-05-02 00:36:27 -04:00
|
|
|
ParseMode: "HTML",
|
2022-05-01 23:28:54 -04:00
|
|
|
}
|
|
|
|
data, err := json.Marshal(msg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiUrl := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", ts.authToken)
|
2022-05-02 13:24:29 -04:00
|
|
|
|
2022-05-31 11:49:07 -04:00
|
|
|
ts.rateLimiterPerMin.Wait()
|
|
|
|
ts.rateLimiterPerSec.Wait()
|
2022-05-02 13:24:29 -04:00
|
|
|
|
2022-05-01 23:28:54 -04:00
|
|
|
res, err := ts.client.Post(apiUrl, "application/json", bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
responseText, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.StatusCode != http.StatusOK {
|
2022-06-17 18:23:06 -04:00
|
|
|
log.Error(nil, "telegram send failed", "status", res.Status, "request", data, "response", responseText)
|
2022-05-01 23:28:54 -04:00
|
|
|
return errors.New("telegram send failed")
|
|
|
|
}
|
2022-06-17 18:23:06 -04:00
|
|
|
log.Info("sent message on telegram", "response", responseText)
|
2022-05-01 23:28:54 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTelegramSender(transport http.RoundTripper, authToken string) TelegramSender {
|
|
|
|
return &telegramSender{
|
|
|
|
client: &http.Client{Transport: transport},
|
|
|
|
authToken: authToken,
|
2022-05-02 13:24:29 -04:00
|
|
|
// 20 requests per min with some buffer
|
2022-06-12 18:17:05 -04:00
|
|
|
rateLimiterPerMin: limiter.NewLimiter((60+15)*time.Second, 20),
|
2022-05-02 13:24:29 -04:00
|
|
|
// 1 msg per sec with some buffer
|
2022-06-12 18:17:05 -04:00
|
|
|
rateLimiterPerSec: limiter.NewLimiter(1100*time.Millisecond, 1),
|
2022-05-01 23:28:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func genIVLink(link, rhash string) string {
|
2022-05-03 18:32:23 -04:00
|
|
|
if rhash == "" {
|
|
|
|
return link
|
|
|
|
}
|
2022-05-01 23:28:54 -04:00
|
|
|
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()
|
|
|
|
}
|