Attaching Facebook messenger bot to all pages of the user logging in












3












$begingroup$


I have to create open source code that allows a user to login to Facebook through an app and attaches that app's messenger bot to the user's pages. I have never written open source code before so I would like suggestions on how to improve the code.



<?php
require_once("/home/vjukebox/public_html/vendor/autoload.php");

define("FB_APP_ID",'App Id Goes Here');
define("FB_APP_SECRET",'App Secret Goes Here');
define("WIT_API_KEY",'Wit API key goes here');

use FacebookFacebookRequest;

$fb = new FacebookFacebook([
'app_id' => FB_APP_ID,//insert app id
'app_secret' => FB_APP_SECRET,//app secret
'default_graph_version' => 'v2.10',
]);
$fbApp = $fb->getApp();
if (isset($_POST['data'],$_POST['userToken'])) {
$userToken = $_POST['userToken'];
foreach ($_POST['data'] as $pageData) {
$pageToken = $pageData["access_token"];

$request = $fb->request('POST', '/me/subscribed_apps');//subscribe the user to you app
$request->setAccessToken($pageToken);
try {
$response = $fb->getClient()->sendRequest($request);
} catch (FacebookExceptionsFacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookExceptionsFacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}

/*exchange user's short term access token for the user's
long term access token*/
$request = $fb->request('POST', '/oauth/access_token');
$request->setAccessToken($userToken);
$request->setParams(array(
'grant_type' => 'fb_exchange_token',
'client_id' => FB_APP_ID,
'client_secret' => FB_APP_SECRET,
'fb_exchange_token' => $userToken
));
try {
$response = $fb->getClient()->sendRequest($request);
} catch (FacebookExceptionsFacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookExceptionsFacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$responseArr = $response->getDecodedBody();
$longUserToken = $responseArr["access_token"];

//get user's page data
$request = $fb->request('GET', '/me/accounts');
$request->setAccessToken($longUserToken);
try {
$response = $fb->getClient()->sendRequest($request);
} catch (FacebookExceptionsFacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookExceptionsFacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$responseArr = $response->getDecodedBody();

//iterate through pages, save data to database
foreach ($responseArr["data"] as $pageData) {
$pageName = $pageData["name"];
$facebookPageId = $pageData["id"];
$longPageToken = $pageData["access_token"];

//Attach your wit api key if using NLP
$request = $fb->request('POST', '/me/nlp_configs');
$request->setAccessToken($longPageToken);
$request->setParams(array(
'nlp_enabled' => 'true',
'custom_token' =>WIT_API_KEY
));

try {
$response = $fb->getClient()->sendRequest($request);
} catch (FacebookExceptionsFacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookExceptionsFacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$responseArr = $response->getDecodedBody();

/*
Store the data into a database of your chosing
*/
}
die();
}

?>

<body>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<? echo FB_APP_ID ?>',
autoLogAppEvents : true,
xfbml : true,
version : 'v2.10'
});
FB.AppEvents.logPageView();
};

function checkLoginState() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
var userToken = FB.getAccessToken();
FB.api('/me/accounts', function(accountResponse) {
//accountResponse.data.access_token;
var pageAccess = accountResponse.data[0].access_token;
$.ajax({
type:"POST",
url: "facebookpages.php",
data:{
userToken:userToken,
data:accountResponse.data
},
success:function(data){
alert(data) //for debugging
},
error:function(data){
}
});
});
}
else {
FB.login();
}
});
}

(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<!--chose the scopes you want the user to log in with-->
<fb:login-button scope="public_profile,manage_pages,pages_messaging,pages_messaging_subscriptions,pages_show_list,pages_manage_cta" onlogin="checkLoginState();">
</fb:login-button>

</body>









share|improve this question











$endgroup$

















    3












    $begingroup$


    I have to create open source code that allows a user to login to Facebook through an app and attaches that app's messenger bot to the user's pages. I have never written open source code before so I would like suggestions on how to improve the code.



    <?php
    require_once("/home/vjukebox/public_html/vendor/autoload.php");

    define("FB_APP_ID",'App Id Goes Here');
    define("FB_APP_SECRET",'App Secret Goes Here');
    define("WIT_API_KEY",'Wit API key goes here');

    use FacebookFacebookRequest;

    $fb = new FacebookFacebook([
    'app_id' => FB_APP_ID,//insert app id
    'app_secret' => FB_APP_SECRET,//app secret
    'default_graph_version' => 'v2.10',
    ]);
    $fbApp = $fb->getApp();
    if (isset($_POST['data'],$_POST['userToken'])) {
    $userToken = $_POST['userToken'];
    foreach ($_POST['data'] as $pageData) {
    $pageToken = $pageData["access_token"];

    $request = $fb->request('POST', '/me/subscribed_apps');//subscribe the user to you app
    $request->setAccessToken($pageToken);
    try {
    $response = $fb->getClient()->sendRequest($request);
    } catch (FacebookExceptionsFacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
    } catch (FacebookExceptionsFacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
    }
    }

    /*exchange user's short term access token for the user's
    long term access token*/
    $request = $fb->request('POST', '/oauth/access_token');
    $request->setAccessToken($userToken);
    $request->setParams(array(
    'grant_type' => 'fb_exchange_token',
    'client_id' => FB_APP_ID,
    'client_secret' => FB_APP_SECRET,
    'fb_exchange_token' => $userToken
    ));
    try {
    $response = $fb->getClient()->sendRequest($request);
    } catch (FacebookExceptionsFacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
    } catch (FacebookExceptionsFacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
    }
    $responseArr = $response->getDecodedBody();
    $longUserToken = $responseArr["access_token"];

    //get user's page data
    $request = $fb->request('GET', '/me/accounts');
    $request->setAccessToken($longUserToken);
    try {
    $response = $fb->getClient()->sendRequest($request);
    } catch (FacebookExceptionsFacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
    } catch (FacebookExceptionsFacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
    }
    $responseArr = $response->getDecodedBody();

    //iterate through pages, save data to database
    foreach ($responseArr["data"] as $pageData) {
    $pageName = $pageData["name"];
    $facebookPageId = $pageData["id"];
    $longPageToken = $pageData["access_token"];

    //Attach your wit api key if using NLP
    $request = $fb->request('POST', '/me/nlp_configs');
    $request->setAccessToken($longPageToken);
    $request->setParams(array(
    'nlp_enabled' => 'true',
    'custom_token' =>WIT_API_KEY
    ));

    try {
    $response = $fb->getClient()->sendRequest($request);
    } catch (FacebookExceptionsFacebookResponseException $e) {
    // When Graph returns an error
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
    } catch (FacebookExceptionsFacebookSDKException $e) {
    // When validation fails or other local issues
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
    }
    $responseArr = $response->getDecodedBody();

    /*
    Store the data into a database of your chosing
    */
    }
    die();
    }

    ?>

    <body>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script>
    window.fbAsyncInit = function() {
    FB.init({
    appId : '<? echo FB_APP_ID ?>',
    autoLogAppEvents : true,
    xfbml : true,
    version : 'v2.10'
    });
    FB.AppEvents.logPageView();
    };

    function checkLoginState() {
    FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
    console.log('Logged in.');
    var userToken = FB.getAccessToken();
    FB.api('/me/accounts', function(accountResponse) {
    //accountResponse.data.access_token;
    var pageAccess = accountResponse.data[0].access_token;
    $.ajax({
    type:"POST",
    url: "facebookpages.php",
    data:{
    userToken:userToken,
    data:accountResponse.data
    },
    success:function(data){
    alert(data) //for debugging
    },
    error:function(data){
    }
    });
    });
    }
    else {
    FB.login();
    }
    });
    }

    (function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
    </script>
    <!--chose the scopes you want the user to log in with-->
    <fb:login-button scope="public_profile,manage_pages,pages_messaging,pages_messaging_subscriptions,pages_show_list,pages_manage_cta" onlogin="checkLoginState();">
    </fb:login-button>

    </body>









    share|improve this question











    $endgroup$















      3












      3








      3





      $begingroup$


      I have to create open source code that allows a user to login to Facebook through an app and attaches that app's messenger bot to the user's pages. I have never written open source code before so I would like suggestions on how to improve the code.



      <?php
      require_once("/home/vjukebox/public_html/vendor/autoload.php");

      define("FB_APP_ID",'App Id Goes Here');
      define("FB_APP_SECRET",'App Secret Goes Here');
      define("WIT_API_KEY",'Wit API key goes here');

      use FacebookFacebookRequest;

      $fb = new FacebookFacebook([
      'app_id' => FB_APP_ID,//insert app id
      'app_secret' => FB_APP_SECRET,//app secret
      'default_graph_version' => 'v2.10',
      ]);
      $fbApp = $fb->getApp();
      if (isset($_POST['data'],$_POST['userToken'])) {
      $userToken = $_POST['userToken'];
      foreach ($_POST['data'] as $pageData) {
      $pageToken = $pageData["access_token"];

      $request = $fb->request('POST', '/me/subscribed_apps');//subscribe the user to you app
      $request->setAccessToken($pageToken);
      try {
      $response = $fb->getClient()->sendRequest($request);
      } catch (FacebookExceptionsFacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
      } catch (FacebookExceptionsFacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
      }
      }

      /*exchange user's short term access token for the user's
      long term access token*/
      $request = $fb->request('POST', '/oauth/access_token');
      $request->setAccessToken($userToken);
      $request->setParams(array(
      'grant_type' => 'fb_exchange_token',
      'client_id' => FB_APP_ID,
      'client_secret' => FB_APP_SECRET,
      'fb_exchange_token' => $userToken
      ));
      try {
      $response = $fb->getClient()->sendRequest($request);
      } catch (FacebookExceptionsFacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
      } catch (FacebookExceptionsFacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
      }
      $responseArr = $response->getDecodedBody();
      $longUserToken = $responseArr["access_token"];

      //get user's page data
      $request = $fb->request('GET', '/me/accounts');
      $request->setAccessToken($longUserToken);
      try {
      $response = $fb->getClient()->sendRequest($request);
      } catch (FacebookExceptionsFacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
      } catch (FacebookExceptionsFacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
      }
      $responseArr = $response->getDecodedBody();

      //iterate through pages, save data to database
      foreach ($responseArr["data"] as $pageData) {
      $pageName = $pageData["name"];
      $facebookPageId = $pageData["id"];
      $longPageToken = $pageData["access_token"];

      //Attach your wit api key if using NLP
      $request = $fb->request('POST', '/me/nlp_configs');
      $request->setAccessToken($longPageToken);
      $request->setParams(array(
      'nlp_enabled' => 'true',
      'custom_token' =>WIT_API_KEY
      ));

      try {
      $response = $fb->getClient()->sendRequest($request);
      } catch (FacebookExceptionsFacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
      } catch (FacebookExceptionsFacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
      }
      $responseArr = $response->getDecodedBody();

      /*
      Store the data into a database of your chosing
      */
      }
      die();
      }

      ?>

      <body>
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script>
      window.fbAsyncInit = function() {
      FB.init({
      appId : '<? echo FB_APP_ID ?>',
      autoLogAppEvents : true,
      xfbml : true,
      version : 'v2.10'
      });
      FB.AppEvents.logPageView();
      };

      function checkLoginState() {
      FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
      console.log('Logged in.');
      var userToken = FB.getAccessToken();
      FB.api('/me/accounts', function(accountResponse) {
      //accountResponse.data.access_token;
      var pageAccess = accountResponse.data[0].access_token;
      $.ajax({
      type:"POST",
      url: "facebookpages.php",
      data:{
      userToken:userToken,
      data:accountResponse.data
      },
      success:function(data){
      alert(data) //for debugging
      },
      error:function(data){
      }
      });
      });
      }
      else {
      FB.login();
      }
      });
      }

      (function(d, s, id){
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) {return;}
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));
      </script>
      <!--chose the scopes you want the user to log in with-->
      <fb:login-button scope="public_profile,manage_pages,pages_messaging,pages_messaging_subscriptions,pages_show_list,pages_manage_cta" onlogin="checkLoginState();">
      </fb:login-button>

      </body>









      share|improve this question











      $endgroup$




      I have to create open source code that allows a user to login to Facebook through an app and attaches that app's messenger bot to the user's pages. I have never written open source code before so I would like suggestions on how to improve the code.



      <?php
      require_once("/home/vjukebox/public_html/vendor/autoload.php");

      define("FB_APP_ID",'App Id Goes Here');
      define("FB_APP_SECRET",'App Secret Goes Here');
      define("WIT_API_KEY",'Wit API key goes here');

      use FacebookFacebookRequest;

      $fb = new FacebookFacebook([
      'app_id' => FB_APP_ID,//insert app id
      'app_secret' => FB_APP_SECRET,//app secret
      'default_graph_version' => 'v2.10',
      ]);
      $fbApp = $fb->getApp();
      if (isset($_POST['data'],$_POST['userToken'])) {
      $userToken = $_POST['userToken'];
      foreach ($_POST['data'] as $pageData) {
      $pageToken = $pageData["access_token"];

      $request = $fb->request('POST', '/me/subscribed_apps');//subscribe the user to you app
      $request->setAccessToken($pageToken);
      try {
      $response = $fb->getClient()->sendRequest($request);
      } catch (FacebookExceptionsFacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
      } catch (FacebookExceptionsFacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
      }
      }

      /*exchange user's short term access token for the user's
      long term access token*/
      $request = $fb->request('POST', '/oauth/access_token');
      $request->setAccessToken($userToken);
      $request->setParams(array(
      'grant_type' => 'fb_exchange_token',
      'client_id' => FB_APP_ID,
      'client_secret' => FB_APP_SECRET,
      'fb_exchange_token' => $userToken
      ));
      try {
      $response = $fb->getClient()->sendRequest($request);
      } catch (FacebookExceptionsFacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
      } catch (FacebookExceptionsFacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
      }
      $responseArr = $response->getDecodedBody();
      $longUserToken = $responseArr["access_token"];

      //get user's page data
      $request = $fb->request('GET', '/me/accounts');
      $request->setAccessToken($longUserToken);
      try {
      $response = $fb->getClient()->sendRequest($request);
      } catch (FacebookExceptionsFacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
      } catch (FacebookExceptionsFacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
      }
      $responseArr = $response->getDecodedBody();

      //iterate through pages, save data to database
      foreach ($responseArr["data"] as $pageData) {
      $pageName = $pageData["name"];
      $facebookPageId = $pageData["id"];
      $longPageToken = $pageData["access_token"];

      //Attach your wit api key if using NLP
      $request = $fb->request('POST', '/me/nlp_configs');
      $request->setAccessToken($longPageToken);
      $request->setParams(array(
      'nlp_enabled' => 'true',
      'custom_token' =>WIT_API_KEY
      ));

      try {
      $response = $fb->getClient()->sendRequest($request);
      } catch (FacebookExceptionsFacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
      } catch (FacebookExceptionsFacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
      }
      $responseArr = $response->getDecodedBody();

      /*
      Store the data into a database of your chosing
      */
      }
      die();
      }

      ?>

      <body>
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script>
      window.fbAsyncInit = function() {
      FB.init({
      appId : '<? echo FB_APP_ID ?>',
      autoLogAppEvents : true,
      xfbml : true,
      version : 'v2.10'
      });
      FB.AppEvents.logPageView();
      };

      function checkLoginState() {
      FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
      console.log('Logged in.');
      var userToken = FB.getAccessToken();
      FB.api('/me/accounts', function(accountResponse) {
      //accountResponse.data.access_token;
      var pageAccess = accountResponse.data[0].access_token;
      $.ajax({
      type:"POST",
      url: "facebookpages.php",
      data:{
      userToken:userToken,
      data:accountResponse.data
      },
      success:function(data){
      alert(data) //for debugging
      },
      error:function(data){
      }
      });
      });
      }
      else {
      FB.login();
      }
      });
      }

      (function(d, s, id){
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) {return;}
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));
      </script>
      <!--chose the scopes you want the user to log in with-->
      <fb:login-button scope="public_profile,manage_pages,pages_messaging,pages_messaging_subscriptions,pages_show_list,pages_manage_cta" onlogin="checkLoginState();">
      </fb:login-button>

      </body>






      javascript php html api facebook






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 28 '18 at 23:21









      Sᴀᴍ Onᴇᴌᴀ

      9,92162166




      9,92162166










      asked Nov 16 '17 at 16:18









      lazersquids mcgeelazersquids mcgee

      162




      162






















          1 Answer
          1






          active

          oldest

          votes


















          2












          $begingroup$

          Overall this feels like a lot of code thrown together. It isn't totally unorganized but feels like a lot to process when reading it. I do notice that most every try/catch block has the same exception handlers.



          One improvement might be to make a controller class that can accept POST data and then have methods like subscribeUserToApp(), getAccessToken(), getAccounts() and then getNLPConfigs(). And each method could utilize a common method for making the request and catching the exceptions. Then those methods could be called from the page (facebookpages.php?) in series unless one throws an exception.



          The controller could also store the constants (e.g. FB_APP_ID) for use in its methods.





          Let's look at this loop:




          foreach ($_POST['data'] as $pageData) {
          $pageToken = $pageData["access_token"];

          $request = $fb->request('POST', '/me/subscribed_apps');//subscribe the user to you app



          Does the call to $fb->request() really need to be inside the foreach loop? If that can be called once before the loop, then it would not be called once for each item in the data list (though it isn't clear what that list looks like - perhaps it only has one item). But I understand that maybe calling that request and then calling setAccessToken() multiple times (in the foreach loop) would not succeed when it should.






          share|improve this answer











          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f180610%2fattaching-facebook-messenger-bot-to-all-pages-of-the-user-logging-in%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2












            $begingroup$

            Overall this feels like a lot of code thrown together. It isn't totally unorganized but feels like a lot to process when reading it. I do notice that most every try/catch block has the same exception handlers.



            One improvement might be to make a controller class that can accept POST data and then have methods like subscribeUserToApp(), getAccessToken(), getAccounts() and then getNLPConfigs(). And each method could utilize a common method for making the request and catching the exceptions. Then those methods could be called from the page (facebookpages.php?) in series unless one throws an exception.



            The controller could also store the constants (e.g. FB_APP_ID) for use in its methods.





            Let's look at this loop:




            foreach ($_POST['data'] as $pageData) {
            $pageToken = $pageData["access_token"];

            $request = $fb->request('POST', '/me/subscribed_apps');//subscribe the user to you app



            Does the call to $fb->request() really need to be inside the foreach loop? If that can be called once before the loop, then it would not be called once for each item in the data list (though it isn't clear what that list looks like - perhaps it only has one item). But I understand that maybe calling that request and then calling setAccessToken() multiple times (in the foreach loop) would not succeed when it should.






            share|improve this answer











            $endgroup$


















              2












              $begingroup$

              Overall this feels like a lot of code thrown together. It isn't totally unorganized but feels like a lot to process when reading it. I do notice that most every try/catch block has the same exception handlers.



              One improvement might be to make a controller class that can accept POST data and then have methods like subscribeUserToApp(), getAccessToken(), getAccounts() and then getNLPConfigs(). And each method could utilize a common method for making the request and catching the exceptions. Then those methods could be called from the page (facebookpages.php?) in series unless one throws an exception.



              The controller could also store the constants (e.g. FB_APP_ID) for use in its methods.





              Let's look at this loop:




              foreach ($_POST['data'] as $pageData) {
              $pageToken = $pageData["access_token"];

              $request = $fb->request('POST', '/me/subscribed_apps');//subscribe the user to you app



              Does the call to $fb->request() really need to be inside the foreach loop? If that can be called once before the loop, then it would not be called once for each item in the data list (though it isn't clear what that list looks like - perhaps it only has one item). But I understand that maybe calling that request and then calling setAccessToken() multiple times (in the foreach loop) would not succeed when it should.






              share|improve this answer











              $endgroup$
















                2












                2








                2





                $begingroup$

                Overall this feels like a lot of code thrown together. It isn't totally unorganized but feels like a lot to process when reading it. I do notice that most every try/catch block has the same exception handlers.



                One improvement might be to make a controller class that can accept POST data and then have methods like subscribeUserToApp(), getAccessToken(), getAccounts() and then getNLPConfigs(). And each method could utilize a common method for making the request and catching the exceptions. Then those methods could be called from the page (facebookpages.php?) in series unless one throws an exception.



                The controller could also store the constants (e.g. FB_APP_ID) for use in its methods.





                Let's look at this loop:




                foreach ($_POST['data'] as $pageData) {
                $pageToken = $pageData["access_token"];

                $request = $fb->request('POST', '/me/subscribed_apps');//subscribe the user to you app



                Does the call to $fb->request() really need to be inside the foreach loop? If that can be called once before the loop, then it would not be called once for each item in the data list (though it isn't clear what that list looks like - perhaps it only has one item). But I understand that maybe calling that request and then calling setAccessToken() multiple times (in the foreach loop) would not succeed when it should.






                share|improve this answer











                $endgroup$



                Overall this feels like a lot of code thrown together. It isn't totally unorganized but feels like a lot to process when reading it. I do notice that most every try/catch block has the same exception handlers.



                One improvement might be to make a controller class that can accept POST data and then have methods like subscribeUserToApp(), getAccessToken(), getAccounts() and then getNLPConfigs(). And each method could utilize a common method for making the request and catching the exceptions. Then those methods could be called from the page (facebookpages.php?) in series unless one throws an exception.



                The controller could also store the constants (e.g. FB_APP_ID) for use in its methods.





                Let's look at this loop:




                foreach ($_POST['data'] as $pageData) {
                $pageToken = $pageData["access_token"];

                $request = $fb->request('POST', '/me/subscribed_apps');//subscribe the user to you app



                Does the call to $fb->request() really need to be inside the foreach loop? If that can be called once before the loop, then it would not be called once for each item in the data list (though it isn't clear what that list looks like - perhaps it only has one item). But I understand that maybe calling that request and then calling setAccessToken() multiple times (in the foreach loop) would not succeed when it should.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 7 hours ago

























                answered Aug 28 '18 at 23:22









                Sᴀᴍ OnᴇᴌᴀSᴀᴍ Onᴇᴌᴀ

                9,92162166




                9,92162166






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f180610%2fattaching-facebook-messenger-bot-to-all-pages-of-the-user-logging-in%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to make a Squid Proxy server?

                    第一次世界大戦

                    Touch on Surface Book