public function send_reply($facebook_id, $message, $debug = false) { if ($this->facebook_account && $this->facebook_page && $this->social_facebook_message_id) { $access_token = $this->facebook_page->get('facebook_token'); // get the machine id from the parent facebook_account $machine_id = $this->facebook_account->get('machine_id'); if (!$facebook_id) { $facebook_id = $this->get('facebook_id'); } if ($debug) { echo "Sending a reply to facebook ID: {$facebook_id} <br>\n"; } if ($debug) { echo "Type: " . $this->get('type') . " <br>\n"; } $result = false; switch ($this->get('type')) { case 'conversation': // do we reply to the previous comment? // nah for now we just add it to the bottom of the list. $facebook = new ucm_facebook(); $result = $facebook->graph_post('' . $facebook_id . '/messages', array('message' => $message, 'access_token' => $access_token, 'machine_id' => $machine_id)); if ($debug) { echo "Graph Post Result: <br>\n" . var_export($result, true) . " <br>\n"; } // reload this message and comments from the graph api. $this->load_by_facebook_id($this->get('facebook_id'), false, $this->get('type'), $debug); break; break; default: // do we reply to the previous comment? // nah for now we just add it to the bottom of the list. $facebook = new ucm_facebook(); $result = $facebook->graph_post('' . $facebook_id . '/comments', array('message' => $message, 'access_token' => $access_token, 'machine_id' => $machine_id)); if ($debug) { echo "Graph Post Result: <br>\n" . var_export($result, true) . " <br>\n"; } // reload this message and comments from the graph api. $this->load_by_facebook_id($this->get('facebook_id'), false, $this->get('type'), $debug); break; } // hack to add the 'user_id' of who created this reply to the db for logging. if ($result && isset($result['id'])) { // find this comment id in our facebook comment database. $exists = get_single('social_facebook_message_comment', array('facebook_id', 'social_facebook_message_id'), array($result['id'], $this->social_facebook_message_id)); if ($exists && $exists['social_facebook_message_comment_id']) { // it really should exist after we've done the 'load_by_facebook_id' above. however with a post with lots of comments it may not appear without graph api pagination. // todo - pagination! update_insert('social_facebook_message_comment_id', $exists['social_facebook_message_comment_id'], 'social_facebook_message_comment', array('user_id' => module_security::get_loggedin_id())); } } } }