Skip to content

Commit 3ad7daa

Browse files
faissalouxjoedixon
andauthored
Send telegram notification when new article submitted (#762)
* send telegram notification when new article submitted * apply StyleCI fixes * apply StyleCI fixes * apply StyleCI fixes * move telegram credentials to services.php * ArticleWasCreated event * pass AnonymousNotifiable in construct * apply StyleCI fixes * remove check env before event fire * Bump laravel-notification-channels/telegram to 0.9.0 * Send notification when article submitted * Update formatting * rename listener * apply StyleCI fixes Co-authored-by: Joe Dixon <hello@joedixon.co.uk>
1 parent 8c9daa7 commit 3ad7daa

File tree

12 files changed

+249
-1
lines changed

12 files changed

+249
-1
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ TWITTER_ACCESS_SECRET=
3333

3434
FLARE_KEY=
3535
MIX_FLARE_KEY="${FLARE_KEY}"
36+
37+
TELEGRAM_BOT_TOKEN=
38+
TELEGRAM_CHANNEL=
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Article;
6+
use Illuminate\Queue\SerializesModels;
7+
8+
class ArticleWasSubmittedForApproval
9+
{
10+
use SerializesModels;
11+
12+
public $article;
13+
14+
public function __construct(Article $article)
15+
{
16+
$this->article = $article;
17+
}
18+
}

app/Jobs/CreateArticle.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Jobs;
44

5+
use App\Events\ArticleWasSubmittedForApproval;
56
use App\Http\Requests\ArticleRequest;
67
use App\Models\Article;
78
use App\Models\User;
@@ -56,6 +57,10 @@ public function handle(): Article
5657
$article->authoredBy($this->author);
5758
$article->syncTags($this->tags);
5859

60+
if ($article->isAwaitingApproval()) {
61+
event(new ArticleWasSubmittedForApproval($article));
62+
}
63+
5964
return $article;
6065
}
6166
}

app/Jobs/UpdateArticle.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Jobs;
44

5+
use App\Events\ArticleWasSubmittedForApproval;
56
use App\Http\Requests\ArticleRequest;
67
use App\Models\Article;
78

@@ -50,9 +51,15 @@ public function handle(): Article
5051
'body' => $this->body,
5152
'original_url' => $this->originalUrl,
5253
'slug' => $this->title,
53-
'submitted_at' => $this->shouldUpdateSubmittedAt() ? now() : $this->article->submittedAt(),
5454
]);
5555

56+
if ($this->shouldUpdateSubmittedAt()) {
57+
$this->article->submitted_at = now();
58+
$this->article->save();
59+
60+
event(new ArticleWasSubmittedForApproval($this->article));
61+
}
62+
5663
$this->article->syncTags($this->tags);
5764

5865
return $this->article;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\ArticleWasSubmittedForApproval;
6+
use App\Notifications\ArticleSubmitted;
7+
use Illuminate\Notifications\AnonymousNotifiable;
8+
9+
final class SendNewArticleNotification
10+
{
11+
public function __construct(
12+
private AnonymousNotifiable $notifiable
13+
) {
14+
}
15+
16+
public function handle(ArticleWasSubmittedForApproval $event): void
17+
{
18+
$this->notifiable->notify(new ArticleSubmitted($event->article));
19+
}
20+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
use App\Models\Article;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Notifications\Notification;
9+
use NotificationChannels\Telegram\TelegramMessage;
10+
11+
class ArticleSubmitted extends Notification implements ShouldQueue
12+
{
13+
use Queueable;
14+
15+
private Article $article;
16+
17+
public function __construct(Article $article)
18+
{
19+
$this->article = $article;
20+
}
21+
22+
public function via($notifiable)
23+
{
24+
return ['telegram'];
25+
}
26+
27+
public function toTelegram($notifiable)
28+
{
29+
$url = route('articles.show', $this->article->slug());
30+
31+
return TelegramMessage::create()
32+
->to(config('services.telegram-bot-api.channel'))
33+
->content($this->content())
34+
->button('View Article', $url);
35+
}
36+
37+
private function content(): string
38+
{
39+
$content = "*New Article Submitted!*\n\n";
40+
$content .= 'Title: '.$this->article->title()."\n";
41+
$content .= 'By: [@'.$this->article->author()->username().']('.route('profile', $this->article->author()->username()).')';
42+
43+
return $content;
44+
}
45+
}

app/Providers/EventServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace App\Providers;
44

55
use App\Events\ArticleWasApproved;
6+
use App\Events\ArticleWasSubmittedForApproval;
67
use App\Events\ReplyWasCreated;
78
use App\Listeners\MarkLastActivity;
89
use App\Listeners\SendArticleApprovedNotification;
10+
use App\Listeners\SendNewArticleNotification;
911
use App\Listeners\SendNewReplyNotification;
1012
use App\Listeners\StoreTweetIdentifier;
1113
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -23,6 +25,9 @@ class EventServiceProvider extends ServiceProvider
2325
MarkLastActivity::class,
2426
SendNewReplyNotification::class,
2527
],
28+
ArticleWasSubmittedForApproval::class => [
29+
SendNewArticleNotification::class,
30+
],
2631
ArticleWasApproved::class => [
2732
SendArticleApprovedNotification::class,
2833
],

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"guzzlehttp/guzzle": "^7.0.1",
1818
"intervention/image": "^2.5",
1919
"intervention/imagecache": "^2.5",
20+
"laravel-notification-channels/telegram": "^0.9.0",
2021
"laravel-notification-channels/twitter": "^5.0",
2122
"laravel/framework": "8.66.0",
2223
"laravel/horizon": "^5.2",

composer.lock

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/services.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@
5151
'access_secret' => env('TWITTER_ACCESS_SECRET'),
5252
],
5353

54+
'telegram-bot-api' => [
55+
'token' => env('TELEGRAM_BOT_TOKEN'),
56+
'channel' => env('TELEGRAM_CHANNEL'),
57+
],
58+
5459
];

0 commit comments

Comments
 (0)