messenger-platform-go-sdk
messenger-platform-go-sdk copied to clipboard
Send Attachment Example
Good afternoon.
Could someone be so kind as to show an example for sending attachments?
All of my files are local HD as *.jpeg, *.mp4 and *.doc
I'm trying to code from scratch but i receive this error:
{"error":{"message":"(#100) Upload attachment failure.","type":"OAuthException","code":100,"error_subcode":2018047,"fbtrace_id":"A7CV8v0-pIMQXv1PLhxV96F"}}
The code:
func fbTest(token string) { url := fmt.Sprintf("https://graph.facebook.com/v6.0/me/messages?access_token=%s", token) method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
_ = writer.WriteField("recipient", "{\"id\":\"3294739867219042\"}")
_ = writer.WriteField("message", "{\"attachment\":{\"type\":\"image\", \"payload\":{\"is_reusable\":true}}}")
fileName := "/tmp/download.png"
filePath := fileName
file, _ := os.Open(filePath)
defer file.Close()
part, _ := writer.CreateFormFile("filedata", filepath.Base(file.Name()))
io.Copy(part, file)
writer.Close()
fmt.Printf("%+v\n", string(payload.Bytes()))
r, _ := http.NewRequest(method, url, payload)
r.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
res, err := client.Do(r)
if err != nil {
fmt.Printf("%+v\n", err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
//req.Header.Add("Content-Type", writer.FormDataContentType())
}
using another lib i can send uploads
when i have free time, i will make a version of this code for messenger-platform-go-sdk
import ( fb "github.com/huandu/facebook" )
func sendFileToFacebook(h *models.WhatsAppConnHandler, facebookID string, fileType string, file io.Reader, fileName string) { recipient := fmt.Sprintf("{"id":"%s"}", toFacebookSenderID(facebookID)) message := fmt.Sprintf("{"attachment":{"type":"%s", "payload":{"is_reusable":false}}}", fileType) r, err := fb.Post("me/messages", fb.Params{ "access_token": h.Session, "recipient": recipient, "message": message, "filedata": fb.Data(fileName, file), }) if err != nil {
fmt.Println("Err", err)
return
}
fmt.Println(r)
}
Hello, this library is no longer maintained. Sorry.