1
votes

I'm trying to retrieve the href attribute configured in the Facebook comments plugin from the plugin_comment webhook, in that way I can know the comment origin and trigger a notification to the owner of the content where the comment was made.

I've reviewed the facebook documentation of the comments webhook and the comments data returned by the graph API, but I couldn't find a hint about how to get the source URL of the comment.

Is it possible to get that value?

Note: The owners of the content are not part of our company, so I can not use the moderation tool unfortunately.

1

1 Answers

0
votes

FINAL UPDATE: Ok, now! here it is! lol...

you can have a webhook file that looks something like this:

<?php
    if ( $_GET['hub_verify_token'] === '[email protected]!') 
    {
        echo $_GET['hub_challenge'];
    }

    $filename = dirname( dirname( __FILE__ ) ) . '/fb_webook/log.json';

    if ( file_exists( $filename ) )
    {
        $data = file_get_contents( $filename );
        $data = json_decode( $data , true );
    }
    else
    {
        $data = array();
    }

    $new = file_get_contents( 'php://input' );
    $new = json_decode( $new , true );

    $new['href'] = get_fb_comment_url( $new );

    $data[] = $new;

    file_put_contents(
        $filename , json_encode( $data , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
    );

    exit();
?>

Now... for the get_fb_comment_url() function, here it is:

<?php

function get_fb_comment_url( $data )
{
    $comment_id = $data['entry'][0]['changes'][0]['value']['id'];
    $comment_id = explode( '_' , $comment_id );
    $comment_id = $comment_id[1];

    $client_id = 'yourappclientid';
    $client_secret = 'yourappclientsecret';

    //GET ACCESS TOKEN
    $url = 'https://graph.facebook.com/oauth/access_token?client_id=' . 
        $client_id . '&client_secret=' . $client_secret . '&grant_type=client_credentials';

    $ch = curl_init();

    curl_setopt( $ch , CURLOPT_URL , $url );
    curl_setopt( $ch , CURLOPT_RETURNTRANSFER , 1 );

    $result = curl_exec( $ch );
    curl_close( $ch );

//RESPONSE LOOKS LIKE THIS
//{"access_token":"[access-token]","token_type":"bearer"}
    $access_token_data = json_decode( $result , true );

    $url = 'https://graph.facebook.com/' . 
        $comment_id . '?fields=permalink_url&access_token=' . $access_token_data['access_token'];

    $ch = curl_init();

    curl_setopt( $ch , CURLOPT_URL , $url );
    curl_setopt( $ch , CURLOPT_RETURNTRANSFER , 1 );

    $result = curl_exec( $ch );
    curl_close( $ch );

/*RESPONSE LOOKS LIKE THIS
{
    "permalink_url": "https://l.facebook.com/l.php?u=https\u00253A\u00252F\u00252Fmiplaylist.fun\u00252FyRz3RyE8314Q\u00252F\u00253Ffb_comment_id\u00253D2249651171827020_2250200588438745\u002526comment_id\u00253D2250200588438745&h=AT1z4LqCDM_CUg_0zvt1m5fKEPeCEQqrjvH8t27Wepuy3y_gFbwG6FaFY-bSHBH1-Ypfji7R-59HL0yBIeRrXuqBCRVWosvRGURvc3j55gG1iu4ClZQ51oFqouxWbh3-CNupERMQ-NvmstLXF3N_d_vejS2NzXXCZJE&s=1",
    "id": "2250200588438745"
}
*/

    $data = json_decode( $result , true );

    $permalink_url = explode( '?' , $data['permalink_url'] );
    parse_str( $permalink_url[1] , $data );

    $href = explode( '?' , $data['u'] );
    $href = $href[0];
    return $href;
}

?>

ENJOY! :D

ORIGINAL ANSWER:

Ok, here it is...
Was looking for the answer myself I think I figured it out.

Webhook sends an object like this:

{"object": "application", "entry": [{"id": "325011391698942", "time": 1569257207, "changes": [{"value": {"id": "2249651171827020_2250207455104725", "from": {"name": "Daniel Morales Lira", "id": "10161627783545224"}, "message": "New Reply", "created_time": "2019-09-23T16:46:46+0000", "parent": {"created_time": "2019-09-23T16:46:17+0000", "from": {"name": "Daniel Morales Lira", "id": "10161627783545224"}, "message": "New Comment", "id": "2249651171827020_2250206608438143"}}, "field": "plugin_comment_reply"}]}]}

In the changes field, the value id is: 2249651171827020_2250207455104725

take the number after the underscore: 2250207455104725

And run a FB API Call like this:

FB.api(
    "/2250207455104725/" ,
    {
        'fields' : 'permalink_url'
    } ,
    function ( response ) 
    {
        if( window.console ) console.log( response );
        myfb.page_info = response;
    }
);

This is from the javascript sdk. It might look different on the server. I will update later on that.

You get a response like this:

{"permalink_url":"https://l.facebook.com/l.php?u=https%3A%2F%2Fmiplaylist.fun%2FyRz3RyE8314Q%2F%3Ffb_comment_id%3D2249651171827020_2250207455104725%26comment_id%3D2250206608438143%26reply_comment_id%3D2250207455104725&h=AT3eO0qIkDRuNpTWhP650IQ9DKVJREdcQyYUknRo2CJdNFtnDDT8YgvOqfFxVph04FWkTI80rBaiehHllACgoZdSqilIFPlA-Jq8FK4Hy6YlrelNUtQRKIXCqNPmRgp_HsT0GbovLfV45uhI&s=1","id":"2250207455104725"}

Embeded in the permalink_url is the url where the comment was posted, url encoded in the u parameter.

UPDATE: You can make an API call from the server side to this URL:

https://graph.facebook.com/2250200588438745?fields=permalink_url&access_token=youraccesstoken

(Replace 2250200588438745 for the number you got after the underscore from the webhook notification)