Public Access
Добавить пересылку коротких YouTube-роликов в MAX как видео.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+116
-1
@@ -5,9 +5,15 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxAPIBase = "https://platform-api.max.ru"
|
||||
@@ -98,10 +104,119 @@ func (b *Bridge) maxRequest(method, path string, query url.Values, body any) ([]
|
||||
|
||||
func (b *Bridge) SendToMAX(chatID int64, text string) error {
|
||||
query := url.Values{"chat_id": {strconv.FormatInt(chatID, 10)}}
|
||||
_, err := b.maxRequest(http.MethodPost, "/messages", query, map[string]string{"text": text})
|
||||
_, err := b.maxRequest(http.MethodPost, "/messages", query, map[string]any{"text": text})
|
||||
return err
|
||||
}
|
||||
|
||||
type maxUploadURLResponse struct {
|
||||
URL string `json:"url"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// SendVideoFileToMAX загружает локальный файл как video и отправляет в чат.
|
||||
func (b *Bridge) SendVideoFileToMAX(chatID int64, filePath, text string) error {
|
||||
query := url.Values{"type": {"video"}}
|
||||
data, err := b.maxRequest(http.MethodPost, "/uploads", query, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var up maxUploadURLResponse
|
||||
if err := json.Unmarshal(data, &up); err != nil {
|
||||
return err
|
||||
}
|
||||
if up.URL == "" || up.Token == "" {
|
||||
return fmt.Errorf("MAX /uploads: пустой url или token: %s", string(data))
|
||||
}
|
||||
|
||||
if err := b.uploadFileToMAXURL(up.URL, filePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
body := map[string]any{
|
||||
"text": text,
|
||||
"attachments": []map[string]any{
|
||||
{
|
||||
"type": "video",
|
||||
"payload": map[string]string{
|
||||
"token": up.Token,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
msgQuery := url.Values{"chat_id": {strconv.FormatInt(chatID, 10)}}
|
||||
|
||||
// После загрузки MAX может ещё обрабатывать файл — повторяем при attachment.not.ready
|
||||
var lastErr error
|
||||
for attempt := 0; attempt < 8; attempt++ {
|
||||
if attempt > 0 {
|
||||
wait := time.Duration(attempt*attempt) * time.Second
|
||||
log.Printf("[MAX] ожидание обработки видео %s…", wait)
|
||||
time.Sleep(wait)
|
||||
}
|
||||
_, lastErr = b.maxRequest(http.MethodPost, "/messages", msgQuery, body)
|
||||
if lastErr == nil {
|
||||
return nil
|
||||
}
|
||||
if !strings.Contains(lastErr.Error(), "attachment.not.ready") &&
|
||||
!strings.Contains(lastErr.Error(), "not.processed") {
|
||||
return lastErr
|
||||
}
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
func (b *Bridge) uploadFileToMAXURL(uploadURL, filePath string) error {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
pr, pw := io.Pipe()
|
||||
w := multipart.NewWriter(pw)
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
defer pw.Close()
|
||||
part, err := w.CreateFormFile("data", filepath.Base(filePath))
|
||||
if err != nil {
|
||||
errCh <- err
|
||||
_ = pw.CloseWithError(err)
|
||||
return
|
||||
}
|
||||
if _, err := io.Copy(part, f); err != nil {
|
||||
errCh <- err
|
||||
_ = pw.CloseWithError(err)
|
||||
return
|
||||
}
|
||||
errCh <- w.Close()
|
||||
}()
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, uploadURL, pr)
|
||||
if err != nil {
|
||||
_ = pr.Close()
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", w.FormDataContentType())
|
||||
if b.MAXToken != "" {
|
||||
req.Header.Set("Authorization", b.MAXToken)
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
writeErr := <-errCh
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, _ := io.ReadAll(resp.Body)
|
||||
if writeErr != nil {
|
||||
return writeErr
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return fmt.Errorf("MAX upload file: %d %s", resp.StatusCode, string(respBody))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bridge) GetMAXUpdates(marker *int64, timeout int) (*maxUpdatesResponse, error) {
|
||||
query := url.Values{
|
||||
"limit": {"100"},
|
||||
|
||||
Reference in New Issue
Block a user