附件上傳 API 可讓您上傳素材,以供之後透過訊息傳送,而不需要重複上傳常用的檔案。此 API 支援從網址和本機檔案系統儲存素材。
您也可以使用 Send API 來傳送含附件的訊息,並同時儲存附件供之後使用。如需詳細資訊,請參閱 Send API 參考資料。
若要上傳附件,請傳送 POST
要求至 /
Your-page-id
/message_attachment
端點,並包含具有 type
和 payload
的 message.attachment
。為了能夠在多則訊息中使用該素材,請將 payload.is_reusable
設為 true
。
採用方便閱讀的格式。將粗體、斜體值(例如 page_access_token)更換為您的值。
curl -X POST "https://graph.facebook.com/v19.0
/Your-page-id/message_attachment" \
-H "Content-Type: application/json" \
-d '{
"access_token":"Your_page_access_token",
"message":{
"attachment":{
"type":"image",
"payload":{
"url":"https.your-url.com/image.jp",
"is_reusable": true
}
}
}
}'
採用方便閱讀的格式。將粗體、斜體值(例如 page_access_token)更換為您的值。
curl -X POST "https://graph.facebook.com/v19.0
/Your-page-id/message_attachment" \
-H "Content-Type: application/json" \
-d '{
"access_token":"Your_page_access_token",
"message":{
"attachment":{
"type":"image"
}
},
"filedata":"@/path-to-your-file/image.png";"type":"image/png"
}'
成功後,應用程式將收到一個 JSON 物件,其 attachment_id
設定為訊息中所使用的附件編號。
{"attachment_id": "Your-attachment-ID"}
若要傳送包含已上傳素材的訊息(上傳時 message.attachment.payload.is_reusable
設為 true
),請傳送 POST
要求至 /
Your-page-id
/messages
端點並包含 recipient.id
,以及包含 type
和 payload.attachment_id
的 message.attachment
物件。
採用方便閱讀的格式。將粗體、斜體值(例如 page_access_token)更換為您的值。
curl -X POST "https://graph.facebook.com/v19.0
/Your-page-id/messages" \
-H "Content-Type: application/json" \
-d '{
"access_token":"Your_page_access_token",
"message":{
"attachment":{
"type":"image",
"payload":{
"attachment_id":"Your-attachment-ID"
}
}
}
}'
成功後,應用程式將收到一個 JSON 物件,其 success
設為 true
。
{"success": "true"}
若是從網址上傳的附件,請在要求內文中以 JSON 物件提供以下屬性。若是從檔案上傳的附件,請以表單資料傳送屬性。
message
Property | Type | Description |
---|---|---|
| Object | An object describing attachments to the message. |
message.attachment
屬性 | 類型 | 說明 |
---|---|---|
| 字串 | 附件的類型。必須為下列任一項:
|
| 物件 | 用來描述附件的 |
message.attachment.payload
屬性 | 類型 | 說明 |
---|---|---|
| 字串 | 選用。所要上傳之檔案的網址。圖像的檔案大小上限為 8MB,所有其他檔案類型為 25MB(編碼後)。影片的逾時設定為 75 秒,所有其他檔案類型為 10 秒。 |
| 布林值 | 選用。設為 |
錯誤代碼 | 子代碼 | 訊息 |
---|---|---|
100 | 2018074 | 可能是無效的編號,或您未擁有該附件。 |
100 | 2018008 | 無法從網址擷取檔案。請檢查網址是否有效(具有有效的 SSL 憑證、有效的檔案大小),以及伺服器回應的速度是否夠快,足以避免逾時。 |
100 | 2018294 | 影片上傳逾時或影片損壞。請注意,如果無法在 75 秒內擷取影片,則會逾時 |
100 | 2018047 | 上傳附件失敗。觸發此錯誤的常見原因是,所提供的媒體類型與網址中提供的檔案類型不符 |