Skip to content

Commit d58b4d4

Browse files
committed
send telegram notification when new article submitted
1 parent 237c730 commit d58b4d4

File tree

6 files changed

+126
-1
lines changed

6 files changed

+126
-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_LARAVELIO_CHANNEL=

app/Jobs/CreateArticle.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use App\Http\Requests\ArticleRequest;
66
use App\Models\Article;
77
use App\Models\User;
8+
use App\Notifications\ArticleSubmitted;
9+
use Illuminate\Notifications\AnonymousNotifiable;
10+
use Illuminate\Support\Facades\App;
811

912
final class CreateArticle
1013
{
@@ -56,6 +59,10 @@ public function handle(): Article
5659
$article->authoredBy($this->author);
5760
$article->syncTags($this->tags);
5861

62+
if (App::environment() !== 'testing'){
63+
(new AnonymousNotifiable())->notify(new ArticleSubmitted($article));
64+
}
65+
5966
return $article;
6067
}
6168
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
return TelegramMessage::create()
31+
->to(env("TELEGRAM_LARAVELIO_CHANNEL"))
32+
->content($this->content())
33+
->button('View article', $url);
34+
}
35+
36+
private function content(): string
37+
{
38+
$content = "*New article submitted!*\n\n";
39+
$content .= "Title: " . $this->article->title() . "\n";
40+
$content .= "By: [@" . $this->article->author()->username() . "](" . route('profile', $this->article->author()->username()) . ")";
41+
42+
return $content;
43+
}
44+
}

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.8.0",
2021
"laravel-notification-channels/twitter": "^5.0",
2122
"laravel/framework": "8.66.0",
2223
"laravel/horizon": "^5.2",

composer.lock

Lines changed: 67 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/services.php

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

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

0 commit comments

Comments
 (0)