diff --git a/app/Console/Commands/Twitter/WeeklyImpactReportTweet.php b/app/Console/Commands/Twitter/WeeklyImpactReportTweet.php index fcb19b86c..00df3c710 100644 --- a/app/Console/Commands/Twitter/WeeklyImpactReportTweet.php +++ b/app/Console/Commands/Twitter/WeeklyImpactReportTweet.php @@ -5,6 +5,7 @@ use App\Helpers\Twitter; use Illuminate\Console\Command; use Spatie\Browsershot\Browsershot; +use App\Services\InstagramService; class WeeklyImpactReportTweet extends Command { @@ -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); } } diff --git a/app/Http/Controllers/Reports/GenerateImpactReportController.php b/app/Http/Controllers/Reports/GenerateImpactReportController.php index 8368e4973..5c6458538 100644 --- a/app/Http/Controllers/Reports/GenerateImpactReportController.php +++ b/app/Http/Controllers/Reports/GenerateImpactReportController.php @@ -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(); diff --git a/app/Services/InstagramService.php b/app/Services/InstagramService.php new file mode 100644 index 000000000..aa0553ddf --- /dev/null +++ b/app/Services/InstagramService.php @@ -0,0 +1,64 @@ +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; + } +}