/** * Fetch user's tweets * * @return array|\Illuminate\View\View */ public function fetchTwitterStats() { $client = initTwitter(); $twitterId = $this->config->twitterId(); try { $tweets = $client->get('statuses/user_timeline.json?user_id=' . $twitterId); return json_decode($tweets->getBody(), true); } catch (\GuzzleHttp\Exception\ClientException $e) { getErrorMessage($e->getResponse()->getStatusCode()); return back(); } }
/** * Updates config record to a valid state after checks * * @return \Illuminate\Http\RedirectResponse */ public function launch() { if (!$this->config->exists()) { $config = new Configuration(); $config->save(); $this->validateTwitter->validTwitterSettings(); } else { $config = $this->config->first(); } $config->valid_credentials = 1; $config->save(); return redirect('/crm/help'); }
/** * Insert reaction in DB (eiter from a Facebook post or Tweet) * @param string $type * @param object $mention * @param integer $id * @param string $answer * * @return Reaction */ public function insertReaction($type, $mention, $id, $answer = null) { $reaction = new Reaction(); $reaction->publishment_id = $id; if ($answer != null) { $reaction->user_id = Auth::user()->id; } if ($type == 'twitter') { if ($mention['user']['id_str'] != Configuration::twitterId()) { $reaction->user_id = $mention['user']['id_str']; } $reaction->screen_name = $mention['user']['screen_name']; $reaction->tweet_id = $mention['id_str']; $reaction->tweet_reply_id = $mention['in_reply_to_status_id_str']; $reaction->message = $mention['text']; $reaction->post_date = changeDateFormat($mention['created_at']); } else { $reaction->fb_post_id = $mention->id; if ($answer == null) { $reaction->screen_name = $mention->from->name; $reaction->message = $mention->message; $reaction->post_date = changeFbDateFormat($mention->created_time); } else { $reaction->message = $answer; $reaction->post_date = Carbon::now(); } } $reaction->save(); return $reaction; }
/** * Update config file with followers * @return void */ public function updateTwitterDashboardStats() { $followers = $this->twitterContent->fetchFollowers(); $config = $this->config->first(); $config->twitter_followers = $followers['followers_count']; $config->save(); }
/** * Inserts Twitter id & screen name in configuration table * @param collection $verification * @return void */ public function insertTwitterId($verification) { $config = Configuration::first(); $config->twitter_screen_name = $verification['screen_name']; $config->twitter_id = $verification['id_str']; $config->linked_twitter = 1; $config->save(); }
/** * Deletes post * * @param object $post * * @return \Illuminate\View\View */ function deleteFbPost($post) { $token = $this->config->FbAccessToken(); $fb = initFb(); try { $fb->delete('/' . $post->fb_post_id, ['access_token' => $token]); Session::flash('flash_success', trans('crm-launcher::success.post_deleted')); } catch (Exception $e) { getErrorMessage($e->getCode()); } return back(); }
/** * Check if twitter settings are valid * @return boolean */ public function validTwitterSettings() { if ($this->config->exists() && $this->config->first()->linked_twitter) { return true; } try { $client = initTwitter(); $verification = $client->get('account/verify_credentials.json'); $verification = json_decode($verification->getBody(), true); if ($this->config->exists() && $this->config->first()->exists()) { $this->config->insertTwitterId($verification); } return true; } catch (\GuzzleHttp\Exception\ClientException $e) { if ($e->getCode() == self::TOO_MANY_REQUEST_CODE) { getErrorMessage($e->getResponse()->getStatusCode()); } return false; } }
/** * Get profile picture of user on Facebook * @param integer $id * @return view */ function getProfilePicture($id) { $fb = initFb(); $token = Configuration::FbAccessToken(); try { $picture = $fb->get('/' . $id . '/picture?redirect=false&type=large', $token); $picture = json_decode($picture->getBody()); return $picture->data->url; } catch (Exception $e) { getErrorMessage($e->getCode()); return back(); } }
/** * Execute the console command. * * @return mixed */ public function handle() { $config = $this->config->first(); $config->notified_today = 0; $config->save(); }