Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send direct message from Page with Attachment via Facebook Graph API?

I can manage Page Conversations (read, write) in help with Facebook Graph API. But it seems to be impossible to add any attachments to my direct messages.

https://developers.facebook.com/docs/graph-api/reference/conversation/messages

I see only "message" parameter in documentation. So, is there any way how to do it?

like image 259
Jakub Mach Avatar asked Apr 16 '14 10:04

Jakub Mach


1 Answers

As @Niraj Shah mentioned above, the attachment sending feature is undocumented (for the time of this post, GraphAPI v2.12), but exists and works if you will post the source field:

PHP:

$fb =
    new Facebook([
        'app_id' => 'your app id',
        'app_secret' => 'your app secret',
        'default_graph_version' => 'v2.12',
        'default_access_token' => 'your page token',
    ]);

$response =
    $fb->post(
        "/{$conversationId}/messages",
        [
            'message' => '',
            'source' => $fb->fileToUpload($attachmentFileName),
        ]
    );

The message field can be empty to send an attachment only.

like image 183
Alexander Pravdin Avatar answered Nov 15 '22 05:11

Alexander Pravdin