Skip to content

Commit 51bafcb

Browse files
committed
✨ ajout de la publication des articles sur Telegram
1 parent 2dc89eb commit 51bafcb

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\Article;
6+
use App\Notifications\PostArticleToTelegram as PostArticleToTelegramNotification;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Notifications\AnonymousNotifiable;
9+
10+
class PostArticleToTelegram extends Command
11+
{
12+
protected $signature = 'lcm:post-article-to-telegram';
13+
14+
protected $description = 'Posts the latest unshared article to Telegram';
15+
16+
public function handle(AnonymousNotifiable $notifiable): void
17+
{
18+
if ($article = Article::nexForSharingToTelegram()) {
19+
$notifiable->notify(new PostArticleToTelegramNotification($article));
20+
}
21+
}
22+
}

app/Models/Article.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,11 @@ public static function nextForSharing(): ?self
330330
->first();
331331
}
332332

333+
public static function nexForSharingToTelegram(): ?self
334+
{
335+
return self::shared()
336+
->published()
337+
->orderBy('submitted_at', 'asc')
338+
->first();
339+
}
333340
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
use App\Models\Article;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Notifications\Notification;
8+
use NotificationChannels\Telegram\TelegramChannel;
9+
use NotificationChannels\Telegram\TelegramMessage;
10+
11+
class PostArticleToTelegram extends Notification
12+
{
13+
use Queueable;
14+
15+
public function __construct(public Article $article)
16+
{
17+
}
18+
19+
public function via($notifiable)
20+
{
21+
return [TelegramChannel::class];
22+
}
23+
24+
public function toTelegram($notifiable)
25+
{
26+
return TelegramMessage::create()
27+
->to('@laravelcm')
28+
->content("{$this->article->title} " . route('articles.show', $this->article->slug()));
29+
}
30+
}

0 commit comments

Comments
 (0)