Skip to content
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
9 changes: 7 additions & 2 deletions app/Console/Commands/Twitter/WeeklyImpactReportTweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Helpers\Twitter;
use Illuminate\Console\Command;
use Spatie\Browsershot\Browsershot;
use App\Services\InstagramService;

class WeeklyImpactReportTweet extends Command
{
Expand Down Expand Up @@ -37,11 +38,15 @@ public function handle()
$msg = "Weekly Impact Report for week $week of $year. Join us at openlittermap.com #litter #citizenscience #impact #openlittermap";

// Tweet the image
Twitter::sendTweetWithImage($msg, $path);
// Twitter::sendTweetWithImage($msg, $path);

// Post to Instagram bot
$instagramService = new InstagramService();
$instagramService->postToInstagram($path, $msg);

$this->info("Tweet sent");

// Delete the image
unlink($path);
// unlink($path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ protected function getStartEndDates ($period, $year, $monthOrWeek): array
if ($period === 'weekly')
{
if ($year === null || $monthOrWeek === null) {
$start = now()->startOfWeek()->toDateTimeString();
$end = now()->endOfWeek()->toDateTimeString();
$start = now()->subWeek()->startOfWeek()->toDateTimeString();
$end = now()->subWeek()->endOfWeek()->toDateTimeString();
} else {
$start = Carbon::now()->setISODate($year, $monthOrWeek)->startOfWeek()->toDateTimeString();
$end = Carbon::now()->setISODate($year, $monthOrWeek)->endOfWeek()->toDateTimeString();
Expand Down
64 changes: 64 additions & 0 deletions app/Services/InstagramService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class InstagramService
{
protected string $accessToken;
protected string $instagramAccountId;

public function __construct ()
{
$this->accessToken = env('INSTAGRAM_ACCESS_TOKEN');
$this->instagramAccountId = env('INSTAGRAM_APP_ID');
}

/**
* Step 1: Create a media container with the image URL and caption.
*/
public function createMediaContainer ($imageUrl, $caption)
{
$endpoint = "https://graph.facebook.com/v17.0/{$this->instagramAccountId}/media";

$response = Http::post($endpoint, [
'image_url' => $imageUrl,
'caption' => $caption,
'access_token' => $this->accessToken,
]);

return $response->json();
}

/**
* Step 2: Publish the media container as a post on Instagram.
*/
public function publishMedia ($creationId)
{
$endpoint = "https://graph.facebook.com/v17.0/{$this->instagramAccountId}/media_publish";

$response = Http::post($endpoint, [
'creation_id' => $creationId,
'access_token' => $this->accessToken,
]);

return $response->json();
}

/**
* Wrapper function to create and publish a post.
*/
public function postToInstagram ($imageUrl, $caption)
{
// Create media container
$container = $this->createMediaContainer($imageUrl, $caption);

if (isset($container['id'])) {
return $this->publishMedia($container['id']);
}

// Return error if container creation fails
return $container;
}
}