I am trying to get my script to post a comment on all comments of a parent post that meet a certain requirement. I am able to get the text to post reliably but I can’t seem to get the photo attachment to show up. I’m programming in Python3 and using the Facebook-sdk library to assist.
When reading the Graph API documentation I found the following fields described on the comments edge:
attachment_id
An optional ID of a unpublished photo (see no_story field in /{user-id}/photos) uploaded to Facebook to include as a photo comment. One of attachment_url, attachment_id, message or source must be provided when publishing.
(string)attachment_url
The URL of an image to include as a photo comment. One of attachment_url, attachment_id, message or source must be provided when publishing.
(string)source
A photo, encoded as form data, to use as a photo comment. One of attachment_url, attachment_id, message or source must be provided when publishing.
(multipart/form-data)
My code is currently formatted as such (I’ve provided partial code relevant to this issue):
my_dict = {
0: ('file_1.JPG', "Some text for file 1"),
1: ('file_2.jpg', "Different text for file 2"),
2: ('file_3.JPG', "More different text for file 3"),
3: ('file_4.JPG', "A fourth bit of text for file 4.")
}
comments = api.get_object('PAGE.NUMBER.HERE?fields=posts{comments}')
com_index = comments['posts']['data'][0]['comments']['data']
photo_id = my_dict[x][0]
my_image = 'file:///Users/filepath/{}'.format(photo_id)
text = my_dict[x][1]
api.put_object(com_index[com_index]['id'], "comments/comments", source=(my_image, 'image/jpg'), message=text)
I have tried both with and without the ‘image/jpg’ argument in the source tuple.
Instead of using ‘source’ I’ve also tried:
attachment_url=card_image
attachment=card_image
When using attachment_url I get an invalid url error; when using the other parameters the text always is posted but the photo is not posted.
Lastly, I’ve tried changing the edge to be a /photos edge of the comment instead of the /comments edge of another comment, but still no luck (as below):
api.put_object(com_index[comment]['id'], "comments/photo", source=(my_image, 'image/jpg'), message=text)
What is the proper method to post a reply that has a photo attachment?
2
Answers
I’m not a Python guy, but perhaps this is working anyway 😉
I think
{objet-id}/comments
is depreciated since version 2.10. Check this.