add telegram bot
This commit is contained in:
@@ -67,6 +67,43 @@ func (c *Client) ListMemos(ctx context.Context, filter string, pageSize int, pag
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// DownloadAttachment downloads the attachment data as bytes.
|
||||
func (c *Client) DownloadAttachment(ctx context.Context, att Attachment) ([]byte, error) {
|
||||
var reqURL string
|
||||
var needsAuth bool
|
||||
|
||||
if att.ExternalLink != "" {
|
||||
reqURL = att.ExternalLink
|
||||
} else {
|
||||
reqURL = fmt.Sprintf("%s/file/%s/%s", c.baseURL, att.Name, att.Filename)
|
||||
needsAuth = true
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
if needsAuth {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("do request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("download attachment %s: status %d", att.Name, resp.StatusCode)
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read body: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// GetRandomMemo fetches a single memo without any filter (for full fallback).
|
||||
func (c *Client) GetRandomMemo(ctx context.Context) (*Memo, error) {
|
||||
resp, err := c.ListMemos(ctx, "", 1, "")
|
||||
|
||||
Reference in New Issue
Block a user