1
0
mirror of synced 2024-11-29 00:26:12 +03:00

Add AttachmentData method (#18)

This commit is contained in:
Trey Richards 2016-11-10 19:28:32 -08:00 committed by Harrison Shoebridge
parent 40fe2bc6f0
commit 12da86e59e

View File

@ -99,47 +99,13 @@ func (r *Response) TextWithReplies(message string, replies []QuickReply) error {
// Image sends an image. // Image sends an image.
func (r *Response) Image(im image.Image) error { func (r *Response) Image(im image.Image) error {
var b bytes.Buffer
w := multipart.NewWriter(&b)
data, err := w.CreateFormFile("fielddata", "meme.jpg")
if err != nil {
return err
}
imageBytes := new(bytes.Buffer) imageBytes := new(bytes.Buffer)
err = jpeg.Encode(imageBytes, im, nil) err := jpeg.Encode(imageBytes, im, nil)
if err != nil { if err != nil {
return err return err
} }
_, err = io.Copy(data, imageBytes) return r.AttachmentData(ImageAttachment, "meme.jpg", imageBytes)
if err != nil {
return err
}
w.WriteField("recipient", fmt.Sprintf(`{"id":"%v"}`, r.to.ID))
w.WriteField("message", `{"attachment":{"type":"image", "payload":{}}}`)
req, err := http.NewRequest("POST", SendMessageURL, &b)
if err != nil {
return err
}
req.URL.RawQuery = "access_token=" + r.token
req.Header.Set("Content-Type", w.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
var res bytes.Buffer
res.ReadFrom(resp.Body)
fmt.Println(res.String(), "DONE!")
return nil
} }
// Attachment sends an image, sound, video or a regular file to a chat. // Attachment sends an image, sound, video or a regular file to a chat.
@ -177,6 +143,44 @@ func (r *Response) Attachment(dataType AttachmentType, url string) error {
return err return err
} }
// AttachmentData sends an image, sound, video or a regular file to a chat via an io.Reader.
func (r *Response) AttachmentData(dataType AttachmentType, filename string, filedata io.Reader) error {
var b bytes.Buffer
w := multipart.NewWriter(&b)
data, err := w.CreateFormFile("filedata", filename)
if err != nil {
return err
}
_, err = io.Copy(data, filedata)
if err != nil {
return err
}
w.WriteField("recipient", fmt.Sprintf(`{"id":"%v"}`, r.to.ID))
w.WriteField("message", fmt.Sprintf(`{"attachment":{"type":"%v", "payload":{}}}`, dataType))
req, err := http.NewRequest("POST", SendMessageURL, &b)
if err != nil {
return err
}
req.URL.RawQuery = "access_token=" + r.token
req.Header.Set("Content-Type", w.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
var res bytes.Buffer
res.ReadFrom(resp.Body)
return nil
}
// ButtonTemplate sends a message with the main contents being button elements // ButtonTemplate sends a message with the main contents being button elements
func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error { func (r *Response) ButtonTemplate(text string, buttons *[]StructuredMessageButton) error {
m := SendStructuredMessage{ m := SendStructuredMessage{