Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send Attachment Example #36

Open
Valdenirmezadri opened this issue Feb 5, 2020 · 3 comments
Open

Send Attachment Example #36

Valdenirmezadri opened this issue Feb 5, 2020 · 3 comments

Comments

@Valdenirmezadri
Copy link

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

@Valdenirmezadri
Copy link
Author

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())

}

@Valdenirmezadri
Copy link
Author

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)

}

@maciekmm
Copy link
Owner

Hello, this library is no longer maintained. Sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants