79 lines
2 KiB
Go
79 lines
2 KiB
Go
package telegram
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
"github.com/s32x/httpclient"
|
|
)
|
|
|
|
type TG struct {
|
|
api *tgbotapi.BotAPI
|
|
video_links chan string
|
|
logger *slog.Logger
|
|
chat_id int64
|
|
}
|
|
|
|
func New(logger *slog.Logger, api_key string, chat_id int64, video_links chan string) (*TG, error) {
|
|
const op = "telegram.New"
|
|
bot, err := tgbotapi.NewBotAPI(api_key)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
bot.Debug = false
|
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
u.Timeout = 60
|
|
|
|
return &TG{
|
|
api: bot,
|
|
video_links: video_links,
|
|
logger: logger,
|
|
chat_id: chat_id,
|
|
}, nil
|
|
}
|
|
|
|
func (tg *TG) Run(count int) {
|
|
for i := 0; i < count; i++ {
|
|
go tg.new_tg_worker(i + 1)
|
|
}
|
|
}
|
|
|
|
const PCUserAgent string = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36"
|
|
const MobileUserAgent string = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Mobile Safari/537.36"
|
|
|
|
func (tg *TG) new_tg_worker(id int) {
|
|
for link := range tg.video_links {
|
|
tg.logger.Info(fmt.Sprintf("[%d]", id), "Получена ссылка, загружаю:", link)
|
|
// get video by link
|
|
var client *httpclient.Client
|
|
if strings.Contains(link, "VK_ANDROID") {
|
|
// create mobile client
|
|
client = httpclient.New().WithHeader("User-Agent", MobileUserAgent)
|
|
} else {
|
|
// create Desktop client
|
|
client = httpclient.New().WithHeader("User-Agent", PCUserAgent)
|
|
}
|
|
|
|
resp, err := client.Get(link).Do()
|
|
if err != nil {
|
|
tg.logger.Error("Error with get media", "err", err)
|
|
continue
|
|
}
|
|
body, err := resp.Bytes()
|
|
if err != nil {
|
|
tg.logger.Error("Error with get body", "err", err)
|
|
continue
|
|
}
|
|
msg := tgbotapi.NewVideo(tg.chat_id, tgbotapi.FileBytes{Name: "story", Bytes: body})
|
|
tg.logger.Info(fmt.Sprintf("[%d] Отправляю видео в телеграм...", id))
|
|
_, err = tg.api.Send(msg)
|
|
if err != nil {
|
|
tg.logger.Error("Error with send message", "err", err)
|
|
continue
|
|
}
|
|
}
|
|
}
|