Add prompt-based picture command
This commit is contained in:
@@ -14,6 +14,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type PhotoUpload struct {
|
||||
Filename string
|
||||
Data []byte
|
||||
Caption string
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
token string
|
||||
baseURL string
|
||||
@@ -205,6 +211,80 @@ func (c *Client) SendPhotoBytes(ctx context.Context, chatID int64, filename stri
|
||||
return decoded.Result, nil
|
||||
}
|
||||
|
||||
func (c *Client) SendPhotoGroupBytes(ctx context.Context, chatID int64, photos []PhotoUpload) ([]Message, error) {
|
||||
if len(photos) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if len(photos) == 1 {
|
||||
message, err := c.SendPhotoBytes(ctx, chatID, photos[0].Filename, photos[0].Data, photos[0].Caption)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []Message{message}, nil
|
||||
}
|
||||
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
if err := writer.WriteField("chat_id", fmt.Sprint(chatID)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
media := make([]map[string]string, 0, len(photos))
|
||||
for i, photo := range photos {
|
||||
name := fmt.Sprintf("photo%d", i)
|
||||
entry := map[string]string{
|
||||
"type": "photo",
|
||||
"media": "attach://" + name,
|
||||
}
|
||||
if photo.Caption != "" {
|
||||
entry["caption"] = photo.Caption
|
||||
}
|
||||
media = append(media, entry)
|
||||
}
|
||||
mediaJSON, err := json.Marshal(media)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := writer.WriteField("media", string(mediaJSON)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i, photo := range photos {
|
||||
name := fmt.Sprintf("photo%d", i)
|
||||
part, err := writer.CreateFormFile(name, filepath.Base(photo.Filename))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := part.Write(photo.Data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/sendMediaGroup", &body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, c.redactError(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
payload, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
return nil, fmt.Errorf("sendMediaGroup: telegram returned %s: %s", resp.Status, string(payload))
|
||||
}
|
||||
var decoded apiResponse[[]Message]
|
||||
if err := json.NewDecoder(resp.Body).Decode(&decoded); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !decoded.OK {
|
||||
return nil, fmt.Errorf("sendMediaGroup: telegram error %d: %s", decoded.ErrorCode, decoded.Description)
|
||||
}
|
||||
return decoded.Result, nil
|
||||
}
|
||||
|
||||
func (c *Client) SendDocumentBytes(ctx context.Context, chatID int64, filename string, data []byte, caption string) (Message, error) {
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
|
||||
Reference in New Issue
Block a user