Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically post on facebook page #30

Open
wants to merge 19 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=


INVISIBLE_RECAPTCHA_SITEKEY=6LfdQSMUAAAAAOP31dkmdZJeNvDghlBfqJsHT5M-
INVISIBLE_RECAPTCHA_SECRETKEY=6LfdQSMUAAAAAAT5cKXa7lI3koiFy4c5BP7BX3m8

NOCAPTCHA_SECRET=6LfdQSMUAAAAAAT5cKXa7lI3koiFy4c5BP7BX3m8
NOCAPTCHA_SITEKEY=6LfdQSMUAAAAAOP31dkmdZJeNvDghlBfqJsHT5M-

FB_APP_ID=424974257871315
FB_APP_SECRET=94e6ed7204eb94082bc87feeb7bea7ca
FB_PAGE_TOKEN=EAAGCgwet2dMBAJJsZBgVTZCb4jdgTVnJD8TjvsOQ5JKyxEYWc7d6xSeosfZBqjITY95qC0jQ66RsBRZBlprHCbE5YYQl6cwaKHZAR4MXLZA977jtCaOP3Q5ZCXY7UrBJCV6IOcIAbmsBPFNoyecLgn6qVMecdxQrspbd31AoxvZBz4bZBZCOPSC6Cb
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ chmod -R 777 storage/
chmod 777 bootstrap/cache
```

#### Facebook intergration

Facebook intergration automatically post need requests to a facebook page. A facebook app and administrative rights to a page is required. Using them an access token to page should be generated to put in .env file. The app does not need to be approved if the page admin has has administrative rights to the app.

![](http://d.pr/i/R3WrWj+)

![](http://d.pr/i/othvIj+)
Click "Get Access Token"

![](http://d.pr/i/JL3vnV+)

The access token is short lived one (2 hours), now we need to convert it to long-lived.

Visit below url and it will give you the access token

`https://graph.facebook.com/oauth/access_token?
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN `

Refer to .env.sample to place the credentials.

### Pull requests

Send all the PRs to `dev` branch. We keep `master` and `prod` branches only for final releases and all the development works on the `dev`.
24 changes: 20 additions & 4 deletions app/Http/Controllers/NeedsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Validator;
use App\Services\FacebookNotification;
use DomainException;

class NeedsController extends Controller
{
private $need;

private $fbNotifier;

/**
* NeedsController constructor.
* @param NeedsRepository $needsRepository
* @param FacebookNotification $fbNotifier
*/
public function __construct(NeedsRepository $needsRepository)
public function __construct(NeedsRepository $needsRepository, FacebookNotification $fbNotifier)
{
$this->need = $needsRepository;
$this->fbNotifier = $fbNotifier;
}

/**
Expand Down Expand Up @@ -68,17 +74,27 @@ public function save(Request $request)
], $messages);

if ($validator->fails()) {

return redirect('/needs/add')
->with('isSuccess', false)
->with('errors', $validator->errors()->all())
->withInput();
} else {
$response = $this->need->addNeed($request->all());
if ($response) {

try {

$need = $this->need->addNeed($request->all());

//need is being created first and if fb posting fail rest of the logic
//is executed
$fbId = $this->fbNotifier->publishNeed($need);
$this->need->updateNeed($need->id, ['fb_post_id' => $fbId]);

return redirect('/needs')
->with('isSuccess', true)
->with('message', 'සාර්ථකව ඇතුලත්කරන ලදී.');
} else {

} catch (DomainException $e) {
return redirect('/needs/add')
->with('isSuccess', false)
->with('errors', ['ඇතුලත්කිරීම දෝෂ සහිතය.'])
Expand Down
2 changes: 1 addition & 1 deletion app/Need.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ class Need extends Model
* @var array
*/
protected $fillable = [
'name', 'telephone', 'address', 'city', 'needs', 'heads', 'source'
'name', 'telephone', 'address', 'city', 'needs', 'heads', 'source', 'fb_post_id'
];
}
17 changes: 14 additions & 3 deletions app/Repositories/NeedsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Need;
use App\Repositories\Contracts\NeedsInterface;
use Illuminate\Support\Facades\Log;
use DomainException;

class NeedsRepository implements NeedsInterface
{
Expand All @@ -23,10 +24,20 @@ class NeedsRepository implements NeedsInterface
public function addNeed($input)
{
try {
Need::create($input);
return true;
return Need::create($input);
} catch (\Exception $e) {
Log::error($e->getMessage());
throw new DomainException('Unable to create need.');
}
}

public function updateNeed($id, array $input)
{
$need = $this->findNeed($id);

if ($need) {
$need->fill($input);
return $need->save();
} else {
return false;
}
}
Expand Down
51 changes: 51 additions & 0 deletions app/Services/FacebookNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace App\Services;

use App\Need;
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use DomainException;
use Config;
use Log;

class FacebookNotification
{
protected $fb;

public function __construct()
{
//TODO: should come from the IoC container
//hardcoding to validate the feature
$this->fb = new Facebook([
'app_id' => Config::get('services.facebook.app_id'),
'app_secret' => Config::get('services.facebook.app_secret'),
'default_graph_version' => 'v2.9',
'default_access_token' => Config::get('services.facebook.page_token')
]);
}

public function publishNeed(Need $need)
{
$content = [];
$content[] = $need->needs;
$content[] = $need->address;
$content[] = $need->city;
$content[] = "People - ". $need->heads;
$content[] = $need->name.' - '.$need->telephone;

try {

$response = $this->fb->post('/me/feed', ['message' => implode($content, "\n")]);
$response->decodeBody();
return array_get($response->getDecodedBody(), 'id');

} catch(FacebookResponseException $e) {
Log::error($e);
return false;
} catch(FacebookSDKException $e) {
Log::error($e);
return false;
}
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type": "project",
"require": {
"php": ">=5.6.4",
"facebook/graph-sdk": "^5.5",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"albertcht/invisible-recaptcha": "^1.3"
Expand Down
6 changes: 6 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@
'secret' => env('STRIPE_SECRET'),
],

'facebook' => [
'app_id' => env('FB_APP_ID'),
'app_secret' => env('FB_APP_SECRET'),
'page_token' => env('FB_PAGE_TOKEN')
]

];
32 changes: 32 additions & 0 deletions database/migrations/2017_05_29_003241_add_fb_post_id_to_needs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddFbPostIdToNeeds extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('needs', function (Blueprint $table) {
$table->string("fb_post_id", 100)->after("heads")->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('needs', function (Blueprint $table) {
$table->dropColumn("needs");
});
}
}